在Hibernate 5.0.1上运行Hibernate程序时遇到以下错误:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.abhinav.hibernate.UserDetails
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1542)
at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:225)
at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:499)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:99)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:778)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:751)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:756)
at com.abhinav.hibernate.main.HibernateTest.main(HibernateTest.java:26)
以下是配置文件:
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<property name="connection.username">postgres</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</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>
<mapping resource="com.abhinav.hibernate.UserDetails"/>
</session-factory>
主要课程:
package com.abhinav.hibernate.main;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import com.abhinav.hibernate.UserDetails;
public class HibernateTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
UserDetails userDetails = new UserDetails();
userDetails.setUserId(1);
userDetails.setUserName("Abhinav");
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
Session session = sessionFactory.openSession();
session.getTransaction();
System.out.println("Session: "+session);
// session.save(userDetails);
session.persist(userDetails);
System.out.println("Session after save: "+session+" User details: "+userDetails.getUserId()+" "+userDetails.getUserName());
session.getTransaction().commit();
}
实体类: UserDetails
包含字段userId
和userName
。
答案 0 :(得分:1)
有两个问题
您需要使用
<mapping class="com.abhinav.hibernate.UserDetails"/>
如果使用注释进行映射(resource
用于xml映射)。
您无法使用Hibernate 4方式为Hibernate 5构建会话工厂。正确的方法:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
参考详情
Hibernate 5 :- org.hibernate.MappingException: Unknown entity