在之前接触过Hibernate,不太熟练并且一直很排斥它,认为其封装太多,不利于真正的掌握sql或数据库,又比较复杂繁琐, 一直没有很好的掌握它。最近项目要用到hibernate,没想到写了第一个例子,就出了N多错误, 随手记录一下。
1.配置Hibernate的连接池
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.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">update</property>
<mapping resource="com/longcheer/domain/Event.xml"/>
</session-factory>
</hibernate-configuration>
2.异常整理
[1].缺antlr-2.7.6.jar
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at $Proxy0.createQuery(Unknown Source)
at com.longcheer.manager.EventManager.listEvents(EventManager.java:40)
at com.longcheer.manager.EventManager.main(EventManager.java:16)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345)
... 3 more
Caused by: java.lang.NoClassDefFoundError: antlr/ANTLRException
at org.hibernate.hql.ast.ASTQueryTranslatorFactory.createQueryTranslator(ASTQueryTranslatorFactory.java:58)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:98)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
... 8 more
Caused by: java.lang.ClassNotFoundException: antlr.ANTLRException
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 15 more
[2].缺commons-collections-3.1.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/map/LRUMap
at org.hibernate.util.SimpleMRUCache.init(SimpleMRUCache.java:71)
at org.hibernate.util.SimpleMRUCache.<init>(SimpleMRUCache.java:55)
at org.hibernate.engine.query.QueryPlanCache.<init>(QueryPlanCache.java:76)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:239)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
at com.longcheer.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:10)
at com.longcheer.util.HibernateUtil.<clinit>(HibernateUtil.java:7)
at com.longcheer.manager.EventManager.createAndStoreEvent(EventManager.java:18)
at com.longcheer.manager.EventManager.main(EventManager.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.map.LRUMap
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 9 more
[3].缺ejb3-persistence.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96)
at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1385)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
at com.longcheer.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:10)
at com.longcheer.util.HibernateUtil.<clinit>(HibernateUtil.java:7)
at com.longcheer.manager.EventManager.createAndStoreEvent(EventManager.java:18)
at com.longcheer.manager.EventManager.main(EventManager.java:13)
Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListeners
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 8 more
[4].缺jta-1.1.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/Synchronization
at org.hibernate.impl.SessionFactoryImpl.buildCurrentSessionContext(SessionFactoryImpl.java:1242)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:395)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
at com.longcheer.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:10)
at com.longcheer.util.HibernateUtil.<clinit>(HibernateUtil.java:7)
at com.longcheer.manager.EventManager.createAndStoreEvent(EventManager.java:18)
at com.longcheer.manager.EventManager.main(EventManager.java:13)
Caused by: java.lang.ClassNotFoundException: javax.transaction.Synchronization
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 7 more
[5].缺javassist-3.12.0.GA.jar
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.longcheer.manager.EventManager.createAndStoreEvent(EventManager.java:18)
at com.longcheer.manager.EventManager.main(EventManager.java:13)
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:108)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:133)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:322)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:485)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:133)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:286)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
at com.longcheer.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:10)
at com.longcheer.util.HibernateUtil.<clinit>(HibernateUtil.java:7)
... 2 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:105)
... 12 more
Caused by: java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter
at org.hibernate.bytecode.javassist.BytecodeProviderImpl.getProxyFactoryFactory(BytecodeProviderImpl.java:49)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactoryInternal(PojoEntityTuplizer.java:205)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:183)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:167)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:77)
... 17 more
Caused by: java.lang.ClassNotFoundException: javassist.util.proxy.MethodFilter
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 22 more
分享到:
相关推荐
这篇随记将探讨哈希函数的基本概念、性质以及在实际应用中的重要性。 哈希函数,也称为散列函数,是一种特殊的算法,它将任意长度的输入(也称为预映射或消息)转化为固定长度的输出,这个输出通常被称为哈希值或...
互联网公司实习日记随记参考.pdf互联网公司实习日记随记参考.pdf互联网公司实习日记随记参考.pdf互联网公司实习日记随记参考.pdf互联网公司实习日记随记参考.pdf互联网公司实习日记随记参考.pdf
【标题】:“随记_电气_”提示我们这是一份关于电气工程领域的个人笔记或学习心得,可能包含了一些作者在学习或实践中积累的电气知识。 【描述】:“电气相关的知识,随手写的,不知道行不行11111”表明这份文档...
linux net 管理随记,留着个人备忘
面试随记???随时更新
git随记 git
### C++ 随记知识点总结 #### 一、内存管理与 new/delete 操作符 在 C++ 中,`new` 和 `delete` 是用于动态内存分配的关键字。`new` 用于在堆区分配内存,`delete` 用于释放之前通过 `new` 分配的内存。使用 `new`...
重点小学生寒假记随记大全.doc
随记App是一款集成微博与博客功能的毕业设计项目,旨在为用户提供一个统一的平台来管理和分享他们的日常思考和生活点滴。这个项目分为客户端和服务端两部分,分别实现了用户交互界面和后台数据处理。 在Android...
公司法务实习日记随记三篇.pdf
会计专业毕业实习日记随记篇.pdf
重点小学生母亲节日记随记大全.doc
计算机网络原理随记 在本资源中,我们将讨论计算机网络的基本概念、发展历程、网络架构、协议栈、数据传输、网络互通、安全性等方面的知识点。 计算机网络的基本概念 计算机网络是指将多个计算机设备连接起来,以...
C#是一种由微软开发的面向对象的编程语言,它结合了C++的高效性和Java的简洁性,被誉为最优秀的编程语言之一。C#是专为.NET框架设计的,因此在.NET环境中具有极高的兼容性和可操作性。.NET Framework包含了公共语言...
小学三年级秋游日记随记五篇.pdf
读书笔记:Java并发编程之美阅读随记
**EHCache的使用随记** EHCache是一款广泛应用于Java环境中的高效、易用且功能丰富的内存缓存系统。它能够显著提升应用性能,通过将常用数据存储在内存中,避免了反复从数据库读取,降低了I/O延迟。本文将探讨...
【辛辛那提1000维修随记】是一份关于数控系统维修的珍贵文档,主要涉及美国辛辛那提·米拉克龙公司的ACRAMATIC系列数控装置,特别是1994年推出的先进CNC控制系统——A2100。这篇随记详细记录了从1950年代早期的数控...
如果文件内容确实如描述中所示,全为“活动随记一次‘出色’的朗诵会.pdf”和一些不明确的符号,那么我将无法从空白或无关的信息中提取知识点。请提供实际的文字内容,以便我进行分析并创作出丰富的内容。
这篇随记内容涵盖了多个教育和德育相关的知识点,深入探讨了教育的本质、电子智慧教育的定义、学生文化的理解以及班主任的角色。以下是对这些知识点的详细解释: 1. **教育理念**: - "精准,精减,精彩":这强调...