线程 "main" org.hibernate.MappingException 中的异常:未知实体:bitronix.examples.hibernate.entities.User
Exception in thread "main" org.hibernate.MappingException: Unknown entity: bitronix.examples.hibernate.entities.User
我在 eclipse 控制台上遇到以下异常
Exception in thread "main" org.hibernate.MappingException: Unknown entity: bitronix.examples.hibernate.entities.User
我的hibernate.cfg.xml是这样的:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">postgres</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/bitronixH4</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="bitronix/examples/hibernate/entities/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
我的Test.javaclass就像
package bitronix.examples.hibernate.entities;
public class Test {
public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sf1 = new Configuration()
.buildSessionFactory(serviceRegistry);
Session session = sf1.openSession();
session.beginTransaction();
User user = new User();
user.setName("rrr");
session.save(user);
session.getTransaction().commit();
}
}
如有任何建议或帮助,我们将不胜感激。
编辑:我的 User.java 是这样的
public class User {
private Integer id;
private String name;
//getters and setters
}
我的User.hbm.xml是这样的
<hibernate-mapping >
<class name="bitronix.examples.hibernate.entities.User" table="test_user" >
<id name="id" type="int">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" />
</property>
</class>
</hibernate-mapping>
问题显然与配置代码有关。您创建了 Configuration
两次。
就这样
SessionFactory sf1 = new Configuration().configure().buildSessionFactory();
我知道,它在 Hibernate 4 中已被弃用。但对于 Hibernate 5,这是一个好方法。这种 Hibernate 4 方法不适用于 Hibernate 5。
Hibernate 5 :- org.hibernate.MappingException: Unknown entity
我在 eclipse 控制台上遇到以下异常
Exception in thread "main" org.hibernate.MappingException: Unknown entity: bitronix.examples.hibernate.entities.User
我的hibernate.cfg.xml是这样的:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">postgres</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/bitronixH4</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="bitronix/examples/hibernate/entities/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
我的Test.javaclass就像
package bitronix.examples.hibernate.entities;
public class Test {
public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sf1 = new Configuration()
.buildSessionFactory(serviceRegistry);
Session session = sf1.openSession();
session.beginTransaction();
User user = new User();
user.setName("rrr");
session.save(user);
session.getTransaction().commit();
}
}
如有任何建议或帮助,我们将不胜感激。
编辑:我的 User.java 是这样的
public class User {
private Integer id;
private String name;
//getters and setters
}
我的User.hbm.xml是这样的
<hibernate-mapping >
<class name="bitronix.examples.hibernate.entities.User" table="test_user" >
<id name="id" type="int">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" />
</property>
</class>
</hibernate-mapping>
问题显然与配置代码有关。您创建了 Configuration
两次。
就这样
SessionFactory sf1 = new Configuration().configure().buildSessionFactory();
我知道,它在 Hibernate 4 中已被弃用。但对于 Hibernate 5,这是一个好方法。这种 Hibernate 4 方法不适用于 Hibernate 5。
Hibernate 5 :- org.hibernate.MappingException: Unknown entity