您的位置:首页 > 其它

初学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".
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: