1.数据库方言设置
<property name=”dialect”>org.hibernate.dialect.MySQL5Dialect</property>
在3.3版本中连接MySQL数据库只需要指明MySQLDialect即可。在4.1版本中可以指出MySQL5Dialect
2.buildSessionFactory
4.1版本中buildSessionFactory()已经被buildSessionFactory(ServiceRegistry ServiceRegistry)取代
解决办法:
Configuration cfg = new Configuration().configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
3.annotation
org.hibernate.cfg.AnnotationConfiguration;这个注解读取配置的class已经废弃,现在读取配置不需要特别注明是注解,
直接用Configuration cfg = new Configuration();就可以读取注解。
Hibernate4.1版本中推荐使用annotation配置,所以在引进jar包时把requested里面的包全部引进来就已经包含了annotation必须包了
Hibernate4 环境搭建:
导入必须的Hibernate4 JAR包
${hibernate_home}\hibernate-release-4.0.0.Final\lib\required\*
数据库驱动包(Oracle)
ojdbc14.jar
缓存(可选)
${hibernate_home}\hibernate-release-4.0.0.Final\lib\optional\ehcache\*
${hibernate_home}\hibernate-distribution-3.6.0.Final\lib\required\slf4j-api-1.6.1.jar
C3P0连接池(可选)
${hibernate_home\hibernate-release-4.0.0.Final\lib\optional\c3p0\*
编写hibernate.cfg.xml
<?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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">none</property>
<property name="hibernate.jdbc.fetch_size">100</property>
<property name="hibernate.jdbc.batch_size">30</property>
<!-- 配置二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<!-- Hibernate4 这里和Hibernate3不一样 要特别注意!!!-->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
<!-- Hibernate3 -->
<!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> -->
<!-- 配置C3P0 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_statements">3</property>
<property name="hibernate.c3p0.timeout">30</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_periodt">10</property>
<mapping class="com.many2manay.Student" />
<mapping class="com.many2manay.teacher" />
<mapping class="com.one2many.Dept" />
<mapping class="com.one2many.Emp" />
<mapping class="com.one2one.Dept" />
<mapping class="com.one2one.Emp" />
</session-factory>
</hibernate-configuration>
hibernate使用版本是:hibernate-release-4.2.5.Final
需要的jar包:hibernate-release-4.2.5.Final\lib\required下所有jar包
ehcache jar包:hibernate-release-4.2.5.Final\lib\optional\ehcache下所有包
junit:junit-4.10.jar和mysql-connector-java-5.1.15-bin.jar
注:hibernate 4.2.5版本ehcache缓存不依赖commons-logging-1.1.1.jar,需要的是slf4j-api-1.6.1.jar
===============================获取Session=============================================:
Session session = sessionFactory.getCurrentSession();//推荐使用
getCurrentSession()与openSession()的区别
1、getCurrentSession () 使用当前的session openSession() 重新建立一个新的session
* 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建的session则不会
* 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,
而采用openSession()创建的session必须手动关闭
2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
* 如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事务(jta事务)java分布式事务管理(多数据库访问)
jta由中间件提供(jboss WebLogic等,tomcat不支持)
<property name="hibernate.current_session_context_class">jta</property>
getCurrentSession()就是把事务和session绑定在了一起,所以就算是查询这些操作也得开启事务。
否则报:org.hibernate.HibernateException: get is not valid without active transaction
getCurrentSession()使用方法
例子之一:
Session session=HibernateSessionFactory.getSessionFactory().getCurrentSession();
session.beginTransaction();
StudentVo vo=(StudentVo)session.get(StudentVo.class, 1L);
session.getTransaction().commit();
例子之二:
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
openSession()使用方法
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(t);
tx.commit();
session.close();
<property name=”dialect”>org.hibernate.dialect.MySQL5Dialect</property>
在3.3版本中连接MySQL数据库只需要指明MySQLDialect即可。在4.1版本中可以指出MySQL5Dialect
2.buildSessionFactory
4.1版本中buildSessionFactory()已经被buildSessionFactory(ServiceRegistry ServiceRegistry)取代
解决办法:
Configuration cfg = new Configuration().configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
3.annotation
org.hibernate.cfg.AnnotationConfiguration;这个注解读取配置的class已经废弃,现在读取配置不需要特别注明是注解,
直接用Configuration cfg = new Configuration();就可以读取注解。
Hibernate4.1版本中推荐使用annotation配置,所以在引进jar包时把requested里面的包全部引进来就已经包含了annotation必须包了
Hibernate4 环境搭建:
导入必须的Hibernate4 JAR包
${hibernate_home}\hibernate-release-4.0.0.Final\lib\required\*
数据库驱动包(Oracle)
ojdbc14.jar
缓存(可选)
${hibernate_home}\hibernate-release-4.0.0.Final\lib\optional\ehcache\*
${hibernate_home}\hibernate-distribution-3.6.0.Final\lib\required\slf4j-api-1.6.1.jar
C3P0连接池(可选)
${hibernate_home\hibernate-release-4.0.0.Final\lib\optional\c3p0\*
编写hibernate.cfg.xml
<?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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">none</property>
<property name="hibernate.jdbc.fetch_size">100</property>
<property name="hibernate.jdbc.batch_size">30</property>
<!-- 配置二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<!-- Hibernate4 这里和Hibernate3不一样 要特别注意!!!-->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
<!-- Hibernate3 -->
<!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> -->
<!-- 配置C3P0 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_statements">3</property>
<property name="hibernate.c3p0.timeout">30</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_periodt">10</property>
<mapping class="com.many2manay.Student" />
<mapping class="com.many2manay.teacher" />
<mapping class="com.one2many.Dept" />
<mapping class="com.one2many.Emp" />
<mapping class="com.one2one.Dept" />
<mapping class="com.one2one.Emp" />
</session-factory>
</hibernate-configuration>
hibernate使用版本是:hibernate-release-4.2.5.Final
需要的jar包:hibernate-release-4.2.5.Final\lib\required下所有jar包
ehcache jar包:hibernate-release-4.2.5.Final\lib\optional\ehcache下所有包
junit:junit-4.10.jar和mysql-connector-java-5.1.15-bin.jar
注:hibernate 4.2.5版本ehcache缓存不依赖commons-logging-1.1.1.jar,需要的是slf4j-api-1.6.1.jar
===============================获取Session=============================================:
Session session = sessionFactory.getCurrentSession();//推荐使用
getCurrentSession()与openSession()的区别
1、getCurrentSession () 使用当前的session openSession() 重新建立一个新的session
* 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建的session则不会
* 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,
而采用openSession()创建的session必须手动关闭
2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
* 如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事务(jta事务)java分布式事务管理(多数据库访问)
jta由中间件提供(jboss WebLogic等,tomcat不支持)
<property name="hibernate.current_session_context_class">jta</property>
getCurrentSession()就是把事务和session绑定在了一起,所以就算是查询这些操作也得开启事务。
否则报:org.hibernate.HibernateException: get is not valid without active transaction
getCurrentSession()使用方法
例子之一:
Session session=HibernateSessionFactory.getSessionFactory().getCurrentSession();
session.beginTransaction();
StudentVo vo=(StudentVo)session.get(StudentVo.class, 1L);
session.getTransaction().commit();
例子之二:
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
openSession()使用方法
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(t);
tx.commit();
session.close();