lazy loading with hibernate
Written by Mottola Michele - Italy - Reggio Emilia   
Monday, 28 April 2014 10:04
Last Updated on Monday, 22 December 2014 10:38
AddThis Social Bookmark Button

Lazy load is an implementation of design pattern proxy. Here it is used to reduce memory consumation when we load collection of object. For example when we have two relational class Impiegato and Dipartimento

Impiegato.java

package org.intellynet.com.model;

import java.io.Serializable;

@Entity
@Table(name = "Impiegato")
public class Impiegato implements Serializable {

	private static final long serialVersionUID = 1L;

	public Impiegato() {
	}

	@Id
	private long id;
	private String nome;
	@OneToOne
	private Dipartimento dipartimento;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String param) {
		this.nome = param;
	}

	public Dipartimento getDipartimento() {
	    return dipartimento;
	}

	public void setDipartimento(Dipartimento param) {
	    this.dipartimento = param;
	}

}

Dipartimento.java

package org.intellynet.com.model;

import java.io.Serializable;

@Entity
@Table(name = "Dipartimento")
public class Dipartimento implements Serializable {

	private static final long serialVersionUID = 1L;

	public Dipartimento() {
	}

	@Id
	private long id;
	private String nome;
	private String citta;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String param) {
		this.nome = param;
	}

	public String getCitta() {
		return citta;
	}

	public void setCitta(String param) {
		this.citta = param;
	}

}

when we do a select statement, we load all object of Impiegato but also all object of Dipartimento.

With lazy load instead we load all object of Impiegato and the proxy object of Dipartimento. Only when we need access to object Dipartimento, the proxy object load the real object of Dipartimento.

To do that we need to tell hibernate that we want use lazy loading. This may be done whit jpa annotation

Impiegato.java

package org.intellynet.com.model;

import java.io.Serializable;

@Entity
@Table(name = "Impiegato")
public class Impiegato implements Serializable {

	private static final long serialVersionUID = 1L;

	public Impiegato() {
	}

	@Id
	private long id;
	private String nome;
	@OneToOne(fetch = FetchType.LAZY)
	private Dipartimento dipartimento;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String param) {
		this.nome = param;
	}

	public Dipartimento getDipartimento() {
	    return dipartimento;
	}

	public void setDipartimento(Dipartimento param) {
	    this.dipartimento = param;
	}

}

note that now we have

  	@OneToOne(fetch = FetchType.LAZY)

Let's try to execute query

package org.intellynet.com.query;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import org.hibernate.proxy.HibernateProxy;
import org.intellynet.com.model.Dipartimento;
import org.intellynet.com.model.Impiegato;

public class QueryTest2 {

	public static void main(String[] args) {
		
		
		EntityManager entityManager=Persistence.createEntityManagerFactory("firstHibernateJpa").createEntityManager();
		entityManager.getTransaction().begin();
		
		
		Impiegato imp= entityManager.createQuery("from Impiegato",Impiegato.class).getResultList().get(0);
		
		System.out.println(imp.getNome());
		
		Dipartimento dip = imp.getDipartimento();
		
		
		

		entityManager.getTransaction().commit();
		entityManager.close();	
	}

}

here dip object of Dipartimento is a proxy. Only when we want execute method of dip

  System.out.println(dip.getNome());

hibernate load real object

Note: this is my first english article. I don't know english so good, so if you find some error please report me.

 

Comments  

 
#1 Massivematch.io 2020-04-21 16:21
Wow, superb blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your web site is
wonderful, as well as the content!
 

Add comment

Security code
Refresh