使用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
<class name="com.rbh,examples.Testbook" table="TESTBOOK" dynamic-insert="true" dynamic-update="true"> </class> </hibernate-mapping>
延迟加载(Lazy Loading)
Hibernate为了避免在关联查询中所带来的无谓的性能开销,使用了延迟加载技术,就是在真正需要读取数据的时候,才向数据库执行数据库的读取加载操作.
1.持久化对象的延迟加载(默认都为true)
<hibernate-mapping> <class name="com.rbh.examples.Category" table="CATEGORY" lazy="true"> </class> </hibernate-mapping>
2.集合对象的延迟加载(默认都为true)
<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)还要对持久化的类做曾强处理
<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)
查询抓取
<set name="products" cascade="save-update" inverse="true" fetch="select"> <key column="category_id" /> <one-to-many class="com.rbh.examples.Product"/> </set>
// 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对象重载抓取策略
// 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();
子查询抓取
<set name="products" cascade="save-update" inverse="true" fetch="subselect"> <key column="category_id" /> <one-to-many class="com.rbh.examples.Product"/> </set>
// 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();
连接查询抓取
<set name="products" cascade="save-update" inverse="true" fetch="join"> <key column="category_id" /> <one-to-many class="com.rbh.examples.Product"/> </set>
// 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();
批量抓取
<set name="products" cascade="save-update" inverse="true" batch-size="5"> <key column="category_id" /> <one-to-many class="com.rbh.examples.Product"/> </set>
// fetch=batch ,lazy=true
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
List<Category> categorys = session.createQuery("from Category").list();
System.out.println(categorys.get(3).getProducts());
System.out.println(categorys.get(0).getProducts());
tx.commit();
本文深入探讨了Hibernate中动态插入与更新、延迟加载等特性,包括配置参数解释、延迟加载的默认设置及如何解决相关异常,同时介绍了集合对象的抓取策略,如查询抓取、子查询抓取、连接查询抓取和批量抓取,并通过实例代码展示了不同抓取策略的应用。

被折叠的 条评论
为什么被折叠?



