我在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.java 类就像
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>
答案 0 :(得分:1)
显然,问题在于配置代码。您创建了Configuration
两次。
只做
SessionFactory sf1 = new Configuration().configure().buildSessionFactory();
我知道,它在Hibernate 4中已被弃用。但对于Hibernate 5来说,这是一个很好的方法。这种Hibernate 4方法不适用于Hibernate 5。
Hibernate 5 :- org.hibernate.MappingException: Unknown entity