Hibernate 5.X 版本中Exception in thread "main" org.hibernate.MappingException: Unknown entity: cn.xxx.p

最近开始使用最新版本的Hibernate5,所有的配置感觉都正确却还是没有结果。

使用如下:

	public static void main(String[] args) throws Exception {
		Configuration cfg = new Configuration().configure();
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
		SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);
		Session session = sf.openSession();
		Transaction transaction = session.beginTransaction();
		User user = new User();
		user.setName("xxx");
		user.setPassword("pswdddd");
		session.save(user);
		transaction.commit();
		session.close();
		sf.close();
		

	}

运行结果老是报错:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: cn.xxx.pojo.User
	at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
	at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1533)
	at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
	at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
	at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
	at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:682)
	at org.hibernate.internal.SessionImpl.save(SessionImpl.java:674)
	at org.hibernate.internal.SessionImpl.save(SessionImpl.java:669)
	at cn.xxx.test.Test.main(Test.java:24)
后来看下官方文档, Hibernate 5 启用配置的方式跟原来的不一样了,正确应该如下: 
 

直接从官方文档是copy过来的

public static void setUp() throws Exception {
		// A SessionFactory is set up once for an application!
		 StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
				.configure() // configures settings from hibernate.cfg.xml
				.build();
		try {
			SessionFactory sf = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
			Session session = sf.openSession();
			Transaction transaction = session.beginTransaction();
			User user = new User();
			user.setName("笑笑熊");
			user.setPassword("0719");
			session.save(user);
			transaction.commit();
			session.close();
			sf.close();
			
		}
		catch (Exception e) {
			StandardServiceRegistryBuilder.destroy( registry );
			e.printStackTrace();
		}
	}

最后终于运行成功了:Hibernate 5.X 版本中Exception in thread "main" org.hibernate.MappingException: Unknown entity: cn.xxx.p_第1张图片


Hibernate 5.X 版本中Exception in thread "main" org.hibernate.MappingException: Unknown entity: cn.xxx.p_第2张图片

你可能感兴趣的:(java,Hibernate)