Java Hibernate中的映射异常-未知实体
我只是为hibernate的演示编写了一个简单的程序。但我得到了一个例外 hibernate.cfg.xml位于src目录下Java Hibernate中的映射异常-未知实体,java,hibernate,intellij-idea,Java,Hibernate,Intellij Idea,我只是为hibernate的演示编写了一个简单的程序。但我得到了一个例外 hibernate.cfg.xml位于src目录下 <?xml version='1.0' encoding='utf-8'?> <!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or lat
<?xml version='1.0' encoding='utf-8'?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the annotated entity class -->
<mapping class="hibernateProject.com.himal.hibernate.HibernateTest"/>
</session-factory>
</hibernate-configuration>
我的测试班是
package hibernateProject.com.himal.hibernate;
import hibernateProject.com.himal.hibernatePractise.dto.UserDetails;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Created by Himal Acharya on 2016-09-01.
*/
public class HibernateTest {
public static void main(String[] args) {
//Initiate object of userDetails
UserDetails user=new UserDetails();
//setting value
user.setUserId(1);
user.setUserName("Himal");
//get the configuration
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(user);
//Ending Transcation
session.getTransaction().commit();
}
}
但我犯了以下错误:
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.hibernate.MappingException: Unknown entity: hibernateProject.com.himal.hibernatePractise.dto.UserDetails
错误位于hibernateProject.com.himal.hibernate.HibernateTest.main,如cosole in line session.save(用户)中所述 您使用的是JPA实体而不是hibernate
@org.hibernate.annotations.entity
,因此请替换此
import javax.persistence.Entity;
import javax.persistence.Id;
在JPA
由此
import org.hibernate.*;
在您的hibernate-config.xml中
不是映射测试类,而是通过添加
<mapping class="hibernateProject.com.himal.hibernatePractise.dto.UserDetails"/>
遵循包的命名约定,如
com.himal.hibernatepracise.dto.User详细信息
在hibernate.cfg.xml中,您必须映射实体:
你能试着换成Yeah吗?换成这个行吗改变只行了。@HimalAcharya当然行
<mapping class="hibernateProject.com.himal.hibernatePractise.dto.UserDetails"/>