@Access in Hibernate Annotation

By Arvind Rai, May 14, 2013
@Access in hibernate is used to force access hierarchy of an entity methods and fields. In class when we use @Id or @Embedded on field, then field is persisted and used to access data by setter and getter. If @Id or @Embedded is used on property then access hierarchy will be property.
In some case we need to force access type. @Access can force to the access type. There are two types of access type AccessType.PROPERTY and AccessType.FIELD. AccessType.PROPERTY can be applied on the field to force access type from property and AccessType.FIELD is applied on property to force access type field.

State.java
package com.concretepage.persistence;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "state")
public class State {

	private int id;

	@Column(name = "name")
	@Access(AccessType.FIELD)
	private String name;
		
	@Id
	@Column(name = "id")
	@Access(AccessType.PROPERTY)
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us