hibernate3.6基础配置

来源:互联网 发布:圆通快递淘宝价格表 编辑:程序博客网 时间:2024/06/05 01:59

    最近一段时间学习hibernate3.6。发现的一些问题,但是最终还是解决了,现在把使用hibernate3.6的
基本步骤在这里跟大家分享一下:
    首先需要基本的jar包:
cglib-2.2.jar
commons-collection-3.2.jar
dom4j-1.6.1.jar
hibernate3.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
slf4j-nop-1.6.1.jar
antlr-2.7.6.jar
使用annotation时还需要下面的jar包:
hibernate-jpa-2.0.api.1.0.0.Final.jar

hibernate3.6已经把annotation完全融入进来,所以没有必要再去下载hibernate-annotation3.4。GA
关于hibernate.cfg.xml的配置就不多说了,重要的是里面的名称要对应。否则会出现
类似于:
Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
这样的错误。我使用的工具类HibernateSessionFactory如下:


import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {
 private static final SessionFactory sessionFactory = buildSessionFactory();

 private static SessionFactory buildSessionFactory() {
  try {
   // Create the SessionFactory from hibernate.cfg.xml
   return new Configuration().configure().buildSessionFactory();
   
  } catch (Throwable ex) {
   // Make sure you log the exception, as it might be swallowed
   System.err.println("出错了Initial SessionFactory creation failed." + ex);
   throw new ExceptionInInitializerError(ex);
  }
 }

 public static SessionFactory getSessionFactory() {
  return sessionFactory;
 }

}
    这里需要说明的是,由于hibernate3.6已经把annotation集成进来,所以我们是new Configuration(),而不是new AnnotationConfiguration()。因为后者在3.6版本里面已经不被推荐使用了,或者说已经废止了。
     接下来需要如果使用我想就不用跟大家细说了,按照原来的规则去写代码就是了。其实最重要的还是hibernate.cfg.cml的配置和model层里annotation的对应关系一定要正确。否则会出现这样或者那样的错误。