初学hibernate错误

参照《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".

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值