初学hibernate错误
2009-04-30 10:34
190 查看
参照《hibernate_reference.pdf3.3.1》学习第一个例子,遇到一些诡异的问题。
1. org.hibernate.HibernateException: No CurrentSessionContext configured!
原因:Session session = HibernateUtil.getSessionFactory().getCurrentSession();
这行代码有问题,修改成
Session session = HibernateUtil.getSessionFactory().openSession();
实际原因不知,为什么CurrentSession的初始数量没有定义?如何定义?
2. java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder
将hibernate-distribution-3.3.1.GA/lib/required目录下的所有jar文件和hibernate文件都加到build path还是报错ClassNotFoundException,令人发指!!!
解决:还需要下载hibernate-entitymanager-3.4.0.GA.zip,将hibernate-entitymanager-3.4.0.GA/lib/test目录下的slf4j-log4j12.jar添加到build path。
评价:大爷您不要这样好不好啊?
3.avax.naming.noinitialcontextexception need to specify class name in environment or system property or as an applet parameter or in an application resource file java.naming.factory.initial
解决:修改hibernate.cfg.xml文件,将红色部分去掉。
<session-factory name="sessionFactory">
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</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="events/Event.hbm.xml" />
</session-factory>
原因:多了一个name属性,hibernate会试图把这个sessionfacotry注册到jndi中去,就出现了这样的错误。
4. hibernate插入新记录会覆盖mysql的旧记录
删除行代码
<property name="hbm2ddl.auto">create</property>
hibernate.cfg.xml file. Usually you only leave it turned on in continuous unit testing, but another run of hbm2ddl would drop everything you have stored - the create configuration setting actually translates into "drop all tables from the schema, then re-create all tables, when the SessionFactory is build".
1. org.hibernate.HibernateException: No CurrentSessionContext configured!
原因:Session session = HibernateUtil.getSessionFactory().getCurrentSession();
这行代码有问题,修改成
Session session = HibernateUtil.getSessionFactory().openSession();
实际原因不知,为什么CurrentSession的初始数量没有定义?如何定义?
2. java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder
将hibernate-distribution-3.3.1.GA/lib/required目录下的所有jar文件和hibernate文件都加到build path还是报错ClassNotFoundException,令人发指!!!
解决:还需要下载hibernate-entitymanager-3.4.0.GA.zip,将hibernate-entitymanager-3.4.0.GA/lib/test目录下的slf4j-log4j12.jar添加到build path。
评价:大爷您不要这样好不好啊?
3.avax.naming.noinitialcontextexception need to specify class name in environment or system property or as an applet parameter or in an application resource file java.naming.factory.initial
解决:修改hibernate.cfg.xml文件,将红色部分去掉。
<session-factory name="sessionFactory">
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</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="events/Event.hbm.xml" />
</session-factory>
原因:多了一个name属性,hibernate会试图把这个sessionfacotry注册到jndi中去,就出现了这样的错误。
4. hibernate插入新记录会覆盖mysql的旧记录
删除行代码
<property name="hbm2ddl.auto">create</property>
hibernate.cfg.xml file. Usually you only leave it turned on in continuous unit testing, but another run of hbm2ddl would drop everything you have stored - the create configuration setting actually translates into "drop all tables from the schema, then re-create all tables, when the SessionFactory is build".
相关文章推荐
- 初学HIBERNATE,错误改正
- Hibernate初学时遇见错误:Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListene
- hibernate初学常见错误
- 初学Hibernate中出现的一些错误
- Hibernate初学抽取工具类错误
- Hibernate JPA —— could not insert 或 detach entity passed to persist 或Found shared references 错误
- 手工配置Hibernate后采用Hibernate Reverse Engineering工具的错误分析
- Hibernate Dialect must be explicitly set 错误信息解决方法
- Hibernate继承映射的“Could not format discriminator value to SQL string”错误解决方法
- 初学java心得及 所犯的错误
- Hibernate createSQLQuery 对于带union的SQL报db2 sqlcode :-421 错误
- Hibernate出现javax.naming.NoInitialContextException 错误的解决办法
- json-lib 解析 Hibernate pojo 对象错误...
- hibernate 延迟加载的错误 failed to lazily initialize a collection of role
- hibernate createClob() java.lang.reflect.InvocationTargetException错误处理
- hibernate错误解决方案(持续更新)
- hql not mapped hibernate语句查询错误
- Hibernate 延迟加载的错误 failed to lazily initialize a collection of role [Hibernate]
- 初学Hibernate