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();
		}
	}
最后终于运行成功了:

时间: 2024-10-25 00:09:35

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

Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User

Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User user where user.name=?0 and user.pass=?1] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180

ERROR: Field 'PostId' doesn't have a default value Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement

例子: Post p = new Post(); p.setPostId(3); p.setPostName("技术"); 在执行数据保持时提示session.save(p); 的错误. INFO: HHH000232: Schema update complete Hibernate: insert into Post (postName) values (?) 八月 19, 2018 5:07:17 下午 org.hibernate.engine.jdbc.spi.SqlExcep

Java中调用c/c++语言出现Exception in thread "main" java.lang.UnsatisfiedLinkError: Test.testPrint(Ljava/lang/String;)V...错误

错误: Exception in thread "main" java.lang.UnsatisfiedLinkError: Test.testPrint(Ljava/lang/String;)V at Test.testPrint(Native Method) at Test.main(Test.java:8) 解决方法: 昨天一天都在想办法解决这个问题,操作步骤按照教程来的,怎么会出错呢?想了半天,觉得还是dll文件生成的不对.所以,最后决定把codeblocks换成visual

在Java中进行api调用时,出现“Exception in thread "main" java.net.SocketTimeoutException: timeout”,“Caused by: java.net.SocketTimeoutException: Read timed out”的错误

在本地使用 OkHttpClient 创建请求客户端,调试api接口时,出现了表示中所述的错误,先贴出完整错误代码 Exception in thread "main" java.net.SocketTimeoutException: timeout at okio.Okio$4.newTimeoutException(Okio.java:230) at okio.AsyncTimeout.exit(AsyncTimeout.java:285) at okio.AsyncTimeout

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/crack

执行一个jar文件的时候抛异常了 Exception in thread "main" java.lang.UnsupportedClassVersionError: com/crack p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Andale Mono"; color: #29f914; background-color: #000000 } span.s1 { color: #afad24 } sp

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/ma ven/cli/Maven

安装maven 中出现如下异常: Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/ma ven/cli/MavenCli : Unsupported major.minor version 51.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCo

Exception in thread "main" brut.androlib.AndrolibException: Could not decode arsc file at brut.androlib.res.decoder.ARSCDecoder.decode

使用ApkIDE反编译出现如下错误: Exception in thread "main" brut.androlib.AndrolibException: Could not decode arsc file at brut.androlib.res.decoder.ARSCDecoder.decode(ARSCDecoder.java:56) 解决方法: 1.从 apktool 官网:https://ibotpeaches.github.io/Apktool/下载最新版本的apkt

Exception in thread "main" java.lang.UnsupportedClassVersionError

在用自定义Classloader加载class文件时,出现了这个错误: Exception in thread "main" java.lang.UnsupportedClassVersionError: Hello : Unsupported major.minor version 52.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLo

Exception in thread "main" java.lang.NoClassDefFoundError: Demo (wrong name: com/zhangyun/Demo)解决办法

介绍一个Java初学者可能会遇到的问题 首先,创建一个类,如下: 文件名:Demo.java package com.zhangyun; public class Demo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hello World!"); } } 然后我找到对应在磁盘的位置,如下: E