Hibernate性能优化

使用dynamic-insert与dynamic-update

动态的添加与更新



      1)<property>元素 insert属性:设置为false,在insert语句中不包含这个字段,表示永远不会被插入,默认true
      2)<property>元素 update属性:设置为false,在update语句中不包含这个字段,表示永远不会被修改,默认true
      3)<class>元素 mutable属性:设置为false就是把所有的<property>元素的update属性设置为了false,说明这个对象不会被更新,默认true
      4)<property>元素 dynamic-insert属性:设置为true,表示insert对象的时候,生成动态的insert语句,如果这个字段的值是null就不会加入到insert语句当中.默认false
      5)<property>元素 dynamic-update属性,设置为true,表示update对象的时候,生成动态的update语句,如果这个字段的值是null就不会被加入到update语句中,默认false
      6)<class>元素 dynamic-insert属性:设置为true,表示把所有的<property>元素的dynamic-insert属性设置为true,默认false
      7)<class>元素 dynamic-update属性:设置为true,表示把所有的<property>元素的dynamic-update属性设置为true,默认false




Xml代码
1.<class name="com.rbh,examples.Testbook" table="TESTBOOK" 
2.    dynamic-insert="true" dynamic-update="true"> 
3.</class> 
4.</hibernate-mapping> 
<class name="com.rbh,examples.Testbook" table="TESTBOOK"
dynamic-insert="true" dynamic-update="true">
</class>
</hibernate-mapping>






延迟加载(Lazy Loading)

Hibernate为了避免在关联查询中所带来的无谓的性能开销,使用了延迟加载技术,就是在真正需要读取数据的时候,才向数据库执行数据库的读取加载操作.



1.持久化对象的延迟加载(默认都为true)
Xml代码
1.<hibernate-mapping> 
2.    <class name="com.rbh.examples.Category" table="CATEGORY" lazy="true"> 
3.    </class> 
4.</hibernate-mapping> 
<hibernate-mapping>
<class name="com.rbh.examples.Category" table="CATEGORY" lazy="true">
</class>
</hibernate-mapping>

2.集合对象的延迟加载(默认都为true)

Xml代码
1.<class name="com.rbh.examples.Category" table="CATEGORY" lazy="true"> 
2.    <set name="products" cascade="save-update" inverse="true" lazy="true"> 
3.        <key column="category_id" /> 
4.        <one-to-many class="com.rbh.examples.Product"/> 
5.    </set> 
6.</class> 
<class name="com.rbh.examples.Category" table="CATEGORY" lazy="true">
<set name="products" cascade="save-update" inverse="true" lazy="true">
<key column="category_id" />
<one-to-many class="com.rbh.examples.Product"/>
</set>
</class>

3.属性的延迟加载(默认为false)还要对持久化的类做曾强处理


Xml代码
1.<class name="com.rbh.examples.Category" table="CATEGORY" lazy="true"> 
2.    <property name="image" type=java.sql.Clob" column="IMAGE" lazy="true"> 
3.</class> 
<class name="com.rbh.examples.Category" table="CATEGORY" lazy="true">
<property name="image" type=java.sql.Clob" column="IMAGE" lazy="true">
</class>




解决org.hibernate.LazyInitializationException

1.取消延迟加载(忽略)
2.使用Hibernate.initialize()
3.使用Open Session In view 设计模式



集合对象的抓取策略(Fetching strategies)



查询抓取

Xml代码
1.<set name="products" cascade="save-update" inverse="true" fetch="select"> 
2.        <key column="category_id" /> 
3.        <one-to-many class="com.rbh.examples.Product"/> 
4.</set> 
<set name="products" cascade="save-update" inverse="true" fetch="select">
<key column="category_id" />
<one-to-many class="com.rbh.examples.Product"/>
</set>

Java代码
1.// fetch=select ,lazy=true  
2.Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();  
3.Transaction tx = session.beginTransaction();  
4.Category category = (Category) session.load(Category.class, new Integer(1));  
5.System.out.println(category.getName());  
6. 
7.Iterator<Product> it = category.getProducts().iterator();  
8.Product p = null;  
9.while (it.hasNext()) {  
10.    p = it.next();  
11.    System.out.println(p.getName());  
12.}  
13.tx.commit(); 
// fetch=select ,lazy=true
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
Category category = (Category) session.load(Category.class, new Integer(1));
System.out.println(category.getName());

Iterator<Product> it = category.getProducts().iterator();
Product p = null;
while (it.hasNext()) {
p = it.next();
System.out.println(p.getName());
}
tx.commit(); 使用HQL语句或者Criteria对象重载抓取策略

Java代码
1.// fetch=select ,lazy=true,HQL重载  
2.Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();  
3.Transaction tx = session.beginTransaction();  
4. 
5.Query query = session  
6.        .createQuery("select c from Category c inner join fetch c.products where c.id=? ");  
7.query.setInteger(0, new Integer(1));  
8.Category category = (Category) query.uniqueResult();  
9. 
10.System.out.println(category.getName());  
11. 
12.Iterator<Product> it = category.getProducts().iterator();  
13.Product p = null;  
14.while (it.hasNext()) {  
15.    p = it.next();  
16.    System.out.println(p.getName());  
17.}  
18. 
19.tx.commit(); 
// fetch=select ,lazy=true,HQL重载
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();

Query query = session
.createQuery("select c from Category c inner join fetch c.products where c.id=? ");
query.setInteger(0, new Integer(1));
Category category = (Category) query.uniqueResult();

System.out.println(category.getName());

Iterator<Product> it = category.getProducts().iterator();
Product p = null;
while (it.hasNext()) {
p = it.next();
System.out.println(p.getName());
}

tx.commit();


子查询抓取

Xml代码
1.<set name="products" cascade="save-update" inverse="true" fetch="subselect"> 
2.        <key column="category_id" /> 
3.        <one-to-many class="com.rbh.examples.Product"/> 
4.</set> 
<set name="products" cascade="save-update" inverse="true" fetch="subselect">
<key column="category_id" />
<one-to-many class="com.rbh.examples.Product"/>
</set>

Java代码
1.// fetch=subselect ,lazy=true  
2.Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();  
3.Transaction tx = session.beginTransaction();  
4. 
5.List categorys = session.createQuery("from Category").list();  
6.for (Iterator<Category> it = categorys.iterator(); it.hasNext();) {  
7.    Category category = it.next();  
8.    System.out.println(category.getName());  
9.    for (Iterator<Product> it2 = category.getProducts().iterator(); it2.hasNext();) {  
10.        Product product = it2.next();  
11.        System.out.println(product.getName());  
12.    }  
13.}  
14.tx.commit(); 
// fetch=subselect ,lazy=true
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();

List categorys = session.createQuery("from Category").list();
for (Iterator<Category> it = categorys.iterator(); it.hasNext();) {
Category category = it.next();
System.out.println(category.getName());
for (Iterator<Product> it2 = category.getProducts().iterator(); it2.hasNext();) {
Product product = it2.next();
System.out.println(product.getName());
}
}
tx.commit();连接查询抓取

Xml代码
1.<set name="products" cascade="save-update" inverse="true" fetch="join"> 
2.        <key column="category_id" /> 
3.        <one-to-many class="com.rbh.examples.Product"/> 
4.</set> 
<set name="products" cascade="save-update" inverse="true" fetch="join">
<key column="category_id" />
<one-to-many class="com.rbh.examples.Product"/>
</set>

Java代码
1.// fetch=join ,lazy=true  
2.Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();  
3.Transaction tx = session.beginTransaction();  
4.Category category = (Category) session.load(Category.class, new Integer(1));  
5.System.out.println(category.getName());  
6. 
7.Iterator<Product> it = category.getProducts().iterator();  
8.Product p = null;  
9.while (it.hasNext()) {  
10.    p = it.next();  
11.    System.out.println(p.getName());  
12.}  
13.tx.commit(); 
// fetch=join ,lazy=true
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
Category category = (Category) session.load(Category.class, new Integer(1));
System.out.println(category.getName());

Iterator<Product> it = category.getProducts().iterator();
Product p = null;
while (it.hasNext()) {
p = it.next();
System.out.println(p.getName());
}
tx.commit();批量抓取

Xml代码
1.<set name="products" cascade="save-update" inverse="true" batch-size="5"> 
2.        <key column="category_id" /> 
3.        <one-to-many class="com.rbh.examples.Product"/> 
4.</set> 
<set name="products" cascade="save-update" inverse="true" batch-size="5">
<key column="category_id" />
<one-to-many class="com.rbh.examples.Product"/>
</set>

Java代码
1.// fetch=batch ,lazy=true  
2.Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();  
3.Transaction tx = session.beginTransaction();  
4. 
5.List<Category> categorys = session.createQuery("from Category").list();  
6.System.out.println(categorys.get(3).getProducts());  
7.System.out.println(categorys.get(0).getProducts());  
8. 
9.tx.commit(); 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值