Hibernate NaturalIdLoadAccess and Session.byNaturalId()

By Arvind Rai, February 12, 2015
In this page, we will learn Hibernate NaturalIdLoadAccess and Session.byNaturalId() example. Hibernate has introduced NaturalIdLoadAccess class that loads entity by using natural identifier. In Hibernate, a method byNaturalId () has been introduced in Session class to initialize NaturalIdLoadAccess as
NaturalIdLoadAccess naturalIdentifier = session.byNaturalId(Car.class); 

NaturalIdLoadAccess

org.hibernate.NaturalIdLoadAccess loads an entity for the given natural identifier. It has methods to assign natural identifier attribute and loading entity.

getReference(): Returns the entity for existing natural identifier.
load() : Loads the entity for existing natural identifier and null if does not exist.
using(String attributeName, Object value) : This method assign the natural identifier attribute to NaturalIdLoadAccess instance.
with(LockOptions Options) : This method returns same as calling instance that is NaturalIdLoadAccess but with lock option like SKIP_LOCKED, NO_WAIT, NONE etc.

Session.byNaturalId

org.hibernate.Session.byNaturalId() returns the instance of NaturalIdLoadAccess . 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 NaturalIdLoadAccess and Session.byNaturalId().

Input Data

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

Main Class

Find the main class to access data.
IdDemo.java
package com.concretepage;
import org.hibernate.NaturalIdLoadAccess;
import org.hibernate.Session;
public class IdDemo {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		NaturalIdLoadAccess naturalIdentifier = session.byNaturalId(Car.class);
		naturalIdentifier.using("vehicleRegNum", "UP65 BN 2343");
		//using load()
		Car car = (Car)naturalIdentifier.load();
		System.out.println("Name:"+ car.getId()+", Vehicle No."+ car.getVehicleRegNum());
		//using getReference()
		car = (Car)naturalIdentifier.getReference();
	        System.out.println("Name:"+ car.getId()+", Vehicle No."+ car.getVehicleRegNum());
		session.close();
	}
} 

Entity

Find the entity being used in the example.
Car.java
package com.concretepage;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.NaturalId;
@Entity
@Table(name="car")  
public class Car implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue
	private int id;
	@NaturalId
	private String vehicleRegNum; 	
	@Column(name="name")
	private String name;
	public Car(){}
	public Car(String vehicleRegNum,String name) {
        this.vehicleRegNum=vehicleRegNum;
        this.name=name;
	}
	public String getVehicleRegNum() {
		return vehicleRegNum;
	}
	public void setVehicleRegNum(String vehicleRegNum) {
		this.vehicleRegNum = vehicleRegNum;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
}  

Output

Find the output.
Hibernate: select car_.id as id1_0_ from car car_ where car_.vehicleRegNum=?
Hibernate: select car0_.id as id1_0_0_, car0_.name as name2_0_0_, car0_.vehicleRegNum as vehicleR3_0_0_ from car car0_ where car0_.id=?
Name:1, Vehicle No.UP65 BN 2343 
Hibernate: select car_.id as id1_0_ from car car_ where car_.vehicleRegNum=?
Name:1, Vehicle No.UP65 BN 2343  

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE












©2024 concretepage.com | Privacy Policy | Contact Us