Hibernate.3.3.2 GA 基础环境设置指南

 Hibernate 3.3.2 GA版本的基本环境搭建指南:
基本类库设定:
 解压Hibernate后其lib下面的bytecode, required全部加入到本设置目录下并且需要加入slf4j-nop-1.5.2.jar,这个package在lib下不存在,google一下获得;
 另外是数据库驱动程序类库的加入;
 log4j类库的加入;
Hibernate配置文件的基本写法:
 <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
 <property name="show_sql">true</property>
 <property name="hibernate.use_sql_comments">false</property>
 
 <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
 <property name="hibernate.connection.url">jdbc:mysql://localhost/sw</property>
 <property name="hibernate.connection.username">root</property>
 <property name="hibernate.connection.password">root</property>
 <property name="hibernate.connection.autocommit">false</property>
 <property name="hibernate.connection.isolation">2</property>
 
 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 
 <property name="current_session_context_class">thread</property>
 
 <property name="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>
 <property name="use_outer_join">true</property>
 
 <property name="hibernate.c3p0.min_size">5</property>
 <property name="hibernate.c3p0.max_size">20</property>
 <property name="hibernate.c3p0.timeout">3600</property>
 <property name="hibernate.c3p0.idle_test_period">600</property>
 <property name="hibernate.c3p0.max_statements">50</property>
 
 <property name="hibernate.statement_cache.size">25</property>
 <property name="jdbc.fetch_size">50</property>
 <property name="jdbc.batch_size">50</property>
 
 <property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
 <property name="hibernate.cache.use_query_cache">false</property>
 <property name="hibernate.cache.use_second_level_cache">false</property>
 
 <property name="hibernate.cglib.use_reflection_optimizer">true</property>
 <property name="jdbc.use_scrollable_resultset">true</property>
 
</session-factory>

</hibernate-configuration>
此新版本中跟老版本的主要区别是加入了 <property name="current_session_context_class">thread</property> 设定;

Hibernate要实现顺利地装载成功,主要是2个基本配置的设定即hibernate的设置以及O-R Mapping配置文件的配置,这里主要以2种组合类型来讲解整个配置的搭建以及HibernateUtil的编写;
基本映射实体类名EventPO,定义了如下3个基本属性:
 private long id = 0;
 private String title = null;
 private Date timeCreated = null;

配置一:采用Hibernate默认的配置文件名:
 Hibernate配置文件名为hibernate.cfg.xml,放置于classes目录下;
 O-R Mapping配置文件名为EventPO.hbm.xml,放置于classes目录的EventPO.class同一个目录下;

public class HibernateUtil {

 private static final SessionFactory sessionFactory = buildSessionFactory();
 
 /**
  * build session factory instance
  *
  * @return
  */
 private static SessionFactory buildSessionFactory() {
  try {
   Configuration config = new Configuration();
   config.configure();
   config.addClass(com.sw.persistent.po.EventPO.class);
   return config.buildSessionFactory();
  } catch (Throwable ex) {
   System.err.println("Initial SessionFactory creation failed." + ex);
   throw new ExceptionInInitializerError(ex);
  }
 }
 
 /**
  * get current session factory instance
  *
  * @return
  */
 public static SessionFactory getSessionFactory() {
  return sessionFactory;
 }
 
}
 
配置二:配置文件名全部自定义:
 Hibernate配置文件名为tmp.hibernate.cfg.xml,放置于com下;
 O-R Mapping配置文件名为Event.hbm.xml,放置于com.sw.persistent.po下;
此情况下HibernateUtil的写法如下:
public class HibernateUtil {

 private static final SessionFactory sessionFactory = buildSessionFactory();
 
 /**
  * build session factory instance
  *
  * @return
  */
 private static SessionFactory buildSessionFactory() {
  try {
   Configuration config = new Configuration();
   config.configure("/com/tmp.hibernate.cfg.xml");
   config.addResource("com/sw/persistent/po/Event.hbm.xml");
   return config.buildSessionFactory();
  } catch (Throwable ex) {
   System.err.println("Initial SessionFactory creation failed." + ex);
   throw new ExceptionInInitializerError(ex);
  }
 }
 
 /**
  * get current session factory instance
  *
  * @return
  */
 public static SessionFactory getSessionFactory() {
  return sessionFactory;
 }
 
}

如果需要动态加载以及替换Hibernate configuration的值,可以在config.configure()之后调用config.setProperty(string, string)函数;
另外config.configure()同样支持内存XML文件的注入config.configure(XML stream);

加完上述代码后,接下来就是具体的调用STA层的代码了:
public final class EventManager {
  public static void main(String[] args) {
  EventManager mgr = new EventManager();
  
  mgr.createAndStoreEvent("My Event", new Date());
  
  HibernateUtil.getSessionFactory().close();
 }

public void createAndStoreEvent(String title, Date timeCreated) {
  Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  session.beginTransaction();
  
  EventPO theEvent = new EventPO();
  theEvent.setTitle(title);
  theEvent.setTimeCreated(timeCreated);
  session.save(theEvent);
  
  session.getTransaction().commit();
 }
}
至此整个Hibernate调用搭建环境的过程结束;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值