Hibernate-04 Lazy Loading

assignment

  • lazy loading
  • Open Session In View mode

 

lazy loading

Lazy load (lazy load) is to execute SQL statements for query only when data is really needed, avoiding unnecessary performance overhead.

The settings of the lazy loading strategy are divided into: class-level query strategy, one-to-many and many-to-many association query strategy, and many-to-one association query strategy.

Hibernate 3.X and later versions use the lazy loading strategy by default.

The lazy attribute used to set the lazy loading feature is shown in the following table:

level lazy property value
class level The optional values ​​for the lazy attribute in the <class> element are true (lazy loading) and false (immediate loading). The default value is true.
One-to-many association level The optional values ​​of the lazy attribute in the <set> element are true (lazy loading), extra (enhanced lazy loading), and fasle (immediate loading). The default value is true.
Many-to-one association level The optional values ​​for the lazy attribute in the <many-to-one> element are proxy (lazy loading), no-proxy (no proxy lazy loading), and false (immediate loading). Default is proxy.

 

Class-level query strategy

Class-level optional strategies are: immediate loading and lazy loading. Default is lazy loading.

1. Load immediately

 1.Dept.hbm.xml file

	<class name="com.etc.entity.Dept" table="`DEPT`" schema="scott"
		dynamic-update="true" lazy="false">

  

2. DAO code

return (Dept) this.getSession().load(Dept.class, id);

  

3.BIZ test method

	@Test
	public void load() {
		Transaction tx=null;
		try{
			tx=deptDao.getSession().beginTransaction();
			Dept dept=deptDao.load(new Short("10"));
			tx.commit();		
			System.out.println(dept.getDeptName());//The transaction is closed, confirm that the dept attributes are all loaded, and output the department name
		}catch (Exception e) {
			e.printStackTrace ();
			if(tx!=null){
				tx.rollback();
			}
		}
	}

  

4. Test results

Hibernate:
    select
        dept0_."DEPTNO" as DEPTNO1_0_0_,
        dept0_."DNAME" as DNAME2_0_0_,
        dept0_."LOC" as LOC3_0_0_
    from
        scott."DEPT" dept0_
    where
        dept0_."DEPTNO"=?

  

Remarks: The load() method loads OID by default, not entity attributes. If you modify the lazy attribute value of the class to true, if you want to output the entity attribute after the session is closed (exception: accessing OID will not cause database query and no error will be reported), the following error will be reported:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

  

Also, the get() and list() methods are loaded immediately, regardless of whether the class-level setting is lazy loading.

 

2. Lazy Loading

1. Mapping file lazy loading settings

	<class name="com.etc.entity.Dept" table="`DEPT`" schema="scott"
		dynamic-update="true" lazy="true">

  or:

	<class name="com.etc.entity.Dept" table="`DEPT`" schema="scott"
		dynamic-update="true">

  

 

//to be continued

One-to-many and many-to-many association query strategies

Many-to-one association query strategy

 

 

Open Session In View

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325486505&siteId=291194637