Last active
August 31, 2015 02:24
-
-
Save dungdm93/bd85c2f053004d894ebd to your computer and use it in GitHub Desktop.
[Java][JPA] Field vs. Property Access
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
[EL Fine]: sql: 2015-08-30 10:11:24.497--ServerSession(1622006612)--Connection(530410619)--SELECT ID, NAME, SALARY FROM EMPLOYEE WHERE (ID = ?) | |
bind => [1 parameter bound] | |
Employee.setName(KiT) // when manager.find(Employee.class, id); | |
Employee.getName() => KiT | |
Employee.setName(KiT) | |
Employee.getName() => KiT | |
Employee.setName(KiT) | |
Employee #1: KiT (12345) | |
Employee.getName() => KiT // when transaction.commit(); | |
Employee.getName() => KiT | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
@Entity | |
@Access(AccessType.FIELD) // necessary for mixed fields and properties access (except Hibernate) | |
public class Employee { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private int id; | |
private String name; // Hibernate can define @Access(PROPERTY) here | |
private long salary; | |
@Basic | |
@Access(AccessType.PROPERTY) | |
public String getName() { | |
System.out.printf("Employee.getName() => %s%n", name); | |
return name; | |
} | |
public void setName(String name) { | |
System.out.printf("Employee.setName(%s)%n", name); | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return String.format("Employee #%d: %s (%d)", id, name, salary); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
Hibernate: | |
select | |
employee0_.id as id1_0_0_, | |
employee0_.name as name2_0_0_, | |
employee0_.salary as salary3_0_0_ | |
from | |
Employee employee0_ | |
where | |
employee0_.id=? | |
Employee.setName(KiT) | |
Employee #1: KiT (12345) | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
1693 pu TRACE [main] openjpa.jdbc.SQL - <t 1270038388, conn 1973233403> executing prepstmnt 1806431167 SELECT t0.name, t0.salary FROM Employee t0 WHERE t0.id = ? [params=?] | |
1694 pu TRACE [main] openjpa.jdbc.SQL - <t 1270038388, conn 1973233403> [1 ms] spent | |
Employee.setName(null) // when manager.find(Employee.class, id); | |
Employee.setName(KiT) | |
Employee.getName() => KiT | |
Employee #1: KiT (12345) | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
public class ServiceJPA { | |
public static void main(String[] args) { | |
EntityManagerFactory factory = Persistence.createEntityManagerFactory("pu"); | |
EntityManager manager = factory.createEntityManager(); | |
EntityTransaction transaction = manager.getTransaction(); | |
transaction.begin(); | |
Employee employee = manager.find(Employee.class, 1); | |
System.out.println(employee); | |
transaction.commit(); | |
manager.close(); | |
factory.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
External references: