Hibernate IdentifierLoadAccess and Session.byId() Example

By Arvind Rai, February 12, 2015
In this page, we will learn Hibernate IdentifierLoadAccess and Session.byId() example. Hibernate 4 has introduced IdentifierLoadAccess class that loads entity by using primary identifier. In Hibernate, a method byId() has been introduced in Session class to initialize IdentifierLoadAccess as
IdentifierLoadAccess identifier = session.byId(Student.class); 

IdentifierLoadAccess

org.hibernate.IdentifierLoadAccess loads an entity using the primary identifier . This class has different methods to access entity.

getReference(Serializable id): We pass the primary identifier and returns the object and if there is no value corresponding to given identifier, it throws exception.
load(Serializable id) : We pass the primary identifier and returns the object and if there is no value corresponding to given identifier, it does not throw exception but returns null value.
with(LockOptions options) : This method returns same as calling instance that is IdentifierLoadAccess but with lock option like SKIP_LOCKED, NO_WAIT, NONE etc.

Session.byId()

org.hibernate.Session.byId() returns the instance of IdentifierLoadAccess. We can pass identifier either entity class name as Class instance or entity name as a String.

Complete Example

Find the complete example for the demo of Hibernate 4 IdentifierLoadAccess and Session.byId().

Input Data

Find the print screen of database table which we are using as an input for our demo.
Hibernate 4 IdentifierLoadAccess and Session.byId() Example

Main Class

Find the main class to access data.
IdDemo.java
package com.concretepage;
import org.hibernate.IdentifierLoadAccess;
import org.hibernate.Session;
public class IdDemo {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		IdentifierLoadAccess identifier = session.byId(Student.class);
		//Access entity for existing identifier using getReference()
		Student student = (Student)identifier.getReference(new Integer(1));
		System.out.println("Name:"+ student.getName()+", Age:"+ student.getAge());
		//Using load
		student = (Student)identifier.load(new Integer(2));
		System.out.println("Name:"+ student.getName()+", Age:"+ student.getAge());
		//Access entity for non-existing identifier
		student = (Student)identifier.load(new Integer(3));
		System.out.println(student);
    	        session.close();
	}
} 

Entity

Find the entity being used in the example.
Student.java
package com.concretepage;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
	@Id
	@Column(name="id")
	private int id;
	@Column(name="age")
	private int age;
	@Column(name="name")
	private String name;
	public Student(){}
	public Student(int id, int age, String name){
		this.id = id;
		this.age = age;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
} 

Output

Find the output.
Hibernate: select student0_.id as id1_0_0_, student0_.age as age2_0_0_, student0_.name as name3_0_0_ from student student0_ where student0_.id=?
Name:Rahim, Age:23
Hibernate: select student0_.id as id1_0_0_, student0_.age as age2_0_0_, student0_.name as name3_0_0_ from student student0_ where student0_.id=?
Name:Mahesh, Age:25
Hibernate: select student0_.id as id1_0_0_, student0_.age as age2_0_0_, student0_.name as name3_0_0_ from student student0_ where student0_.id=?
null 

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us