☰ See All Chapters |
JPA @Access Annotation - AccessType.FIELD and AccessType.PROPERTY
JPA provides @Access annotation for overriding the default behavior, by using AccessType.FIELD and AccessType.PROPERTY. If you set @Access on the class/entity level, JPA accesses all properties of the class according to the selected strategy irrespective of place of Id/EmeddedId annotation. You can also use the @Access annotation to override the access strategy of individual properties. Means Access annotation is class level and as well as member level annotation. @Access annotation has mandatory parameter. We can pass AccessType.FIELD or AccessType.PROPERTY values.
AccessType.FIELD: Access will be direct
AccessType.PROPERTY: Access will be through setters and getters
JPA can access the fields of Entity class, Embeddable class and Id class or any other class which is in the JPA scope, directly or through setters and getters. By default the access type is defined by the place where you put your mandatory id property of the POJO with Id or EmbeddedId. So when you placed your Id or EmbeddedId annotation on a field, all access will happen through the fields, when you apply it on the getter/setter methods, all fields will use the getter/setter methods. Sometimes you might want JPA to access the fields through setters/getters even Id or EmbeddedId annotation is kept on a field and not on setters/getters. E.g.: you want to have some arbitrary logic like encrypting and decrypting the fields before and after they persisted and fetched to/from database. This encryption/decryption logic can be kept in setters and getters.
Now let see an example for @Access annotation.
JPA @Access Annotation Example
Database script
CREATE TABLE STUDENT ( ID INT NOT NULL, FIRSTNAME VARCHAR(20) DEFAULT NULL, LASTNAME VARCHAR(20) DEFAULT NULL, CITY VARCHAR(20) DEFAULT NULL, STATE VARCHAR(20) DEFAULT NULL, COUNTRY VARCHAR(20) DEFAULT NULL, PRIMARY KEY (ID) ); |
pom.xml
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.manum.hassan</groupId> <artifactId>AccessAnnotationExample</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>AccessAnnotationExample</name> <url>https://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>javax.persistence</artifactId> <version>2.0.0</version> </dependency>
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.2.8.Final</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> </dependencies> </project> |
persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="https://java.sun.com/xml/ns/persistence" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://java.sun.com/xml/ns/persistence https://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="StudentPU"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/study" /> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> <property name="hibernate.connection.username" value="root" /> <property name="hibernate.connection.password" value="root" /> <property name="hibernate.archive.autodetection" value="class" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hbm2ddl.auto" value="update" /> </properties> </persistence-unit> </persistence> |
StudentAdsress.java
package com.java4coding;
import javax.persistence.Access; import javax.persistence.AccessType; import javax.persistence.Embeddable;
@Embeddable @Access(value=AccessType.PROPERTY) public class StudentAddress { private String city; private String state; private String country;
public String getCity() { System.out.println("~~~~~~~~getCity~~~~~~~~~"); return city; } public void setCity(String city) { this.city = city; } public String getState() { System.out.println("~~~~~~~~getState~~~~~~~~~"); return state; } public void setState(String state) { this.state = state; } public String getCountry() { System.out.println("~~~~~~~~getCountry~~~~~~~~~"); return country; } public void setCountry(String country) { this.country = country; } } |
Student.java
package com.java4coding;
import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table;
@Entity @Table(name = "STUDENT") public class Student {
@Id private int id;
private String firstName;
private String lastName;
@Embedded private StudentAddress studentAddress;
public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public StudentAddress getStudentAddress() { return studentAddress; } public void setStudentAddress(StudentAddress studentAddress) { this.studentAddress = studentAddress; } } |
Test.java
package com.java4coding;
import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence;
public class Test { public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("StudentPU"); EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
StudentAddress studentAddress = new StudentAddress(); studentAddress.setCity("Bangalore"); studentAddress.setState("Karnataka"); studentAddress.setCountry("India");
Student student = new Student(); student.setFirstName("Manu"); student.setLastName("Manjunatha"); student.setId(1); student.setStudentAddress(studentAddress);
em.persist(student); em.getTransaction().commit();
em.getTransaction().begin(); em.remove(student); em.getTransaction().commit(); } } |
Project Directory structure
Output:
~~~~~~~~getCity~~~~~~~~~ ~~~~~~~~getCountry~~~~~~~~~ ~~~~~~~~getState~~~~~~~~~ ~~~~~~~~getCity~~~~~~~~~ ~~~~~~~~getCountry~~~~~~~~~ ~~~~~~~~getState~~~~~~~~~ ~~~~~~~~getCity~~~~~~~~~ ~~~~~~~~getCountry~~~~~~~~~ ~~~~~~~~getState~~~~~~~~~ ~~~~~~~~getCity~~~~~~~~~ ~~~~~~~~getCountry~~~~~~~~~ ~~~~~~~~getState~~~~~~~~~ ~~~~~~~~getCity~~~~~~~~~ ~~~~~~~~getCity~~~~~~~~~ ~~~~~~~~getCountry~~~~~~~~~ ~~~~~~~~getCountry~~~~~~~~~ ~~~~~~~~getState~~~~~~~~~ ~~~~~~~~getState~~~~~~~~~ Hibernate: insert into STUDENT (firstName, lastName, city, country, state, id) values (?, ?, ?, ?, ?, ?) ~~~~~~~~getCity~~~~~~~~~ ~~~~~~~~getCountry~~~~~~~~~ ~~~~~~~~getState~~~~~~~~~ ~~~~~~~~getCity~~~~~~~~~ ~~~~~~~~getCountry~~~~~~~~~ ~~~~~~~~getState~~~~~~~~~ ~~~~~~~~getCity~~~~~~~~~ ~~~~~~~~getCountry~~~~~~~~~ ~~~~~~~~getState~~~~~~~~~ ~~~~~~~~getCity~~~~~~~~~ ~~~~~~~~getCountry~~~~~~~~~ ~~~~~~~~getState~~~~~~~~~ ~~~~~~~~getCity~~~~~~~~~ ~~~~~~~~getCity~~~~~~~~~ ~~~~~~~~getCountry~~~~~~~~~ ~~~~~~~~getCountry~~~~~~~~~ ~~~~~~~~getState~~~~~~~~~ ~~~~~~~~getState~~~~~~~~~ Hibernate: delete from STUDENT where id=? |
Output when Access is set to AccessType.FIELD
StudentAdsress.java
package com.java4coding;
import javax.persistence.Access; import javax.persistence.AccessType; import javax.persistence.Embeddable;
@Embeddable @Access(value=AccessType.FIELD) public class StudentAddress { private String city; private String state; private String country;
public String getCity() { System.out.println("~~~~~~~~getCity~~~~~~~~~"); return city; } public void setCity(String city) { this.city = city; } public String getState() { System.out.println("~~~~~~~~getState~~~~~~~~~"); return state; } public void setState(String state) { this.state = state; } public String getCountry() { System.out.println("~~~~~~~~getCountry~~~~~~~~~"); return country; } public void setCountry(String country) { this.country = country; } } |
Output
Hibernate: insert into STUDENT (firstName, lastName, city, country, state, id) values (?, ?, ?, ?, ?, ?) Hibernate: delete from STUDENT where id=? |
All Chapters