JPA

  • Java Persistance API(JPA) is used to abstract Database Layer of Java Applications
  • We do all kinds of database operations in Java application.
  • To simplify database operations JPA uses the concept of ORM.
  • It does this by creating a Java class which maps to the database tables.
  • ORM takes care to keep DBA objects and Tables in sync.
  • Java classes store the data from table directly mapped to their attributes in memory.
  • JPA provides API's to manage relational data in Java Applications.
  • Java objects are mapped to database tables.
  • JPA is the specification and Hibernate is the most popular way to implement JPA.
  • Spring provides Spring Data JPA to work with JPA and simplifies database interaction.
One to One Relationship(Composite Key)
Entity Class
 package com.springimplant.userapi.entity;  
 import java.math.BigInteger;  
 import javax.persistence.Entity;  
 import javax.persistence.Id;  
 import javax.persistence.IdClass;  
 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;  
 @Entity  
 @IdClass(UserId.class)  
 @JsonIgnoreProperties({"hibernateLazyInitializer","handler"})  
 public class User {  
      @Id  
      private BigInteger userid;  
      @Id  
      private BigInteger courseid;  
      private String username;  
      public User()   
      {  
      }  
      public BigInteger getUserid() {  
           return userid;  
      }  
      public void setUserid(BigInteger userid) {  
           this.userid = userid;  
      }  
      public BigInteger getCourseid() {  
           return courseid;  
      }  
      public void setCourseid(BigInteger courseid) {  
           this.courseid = courseid;  
      }  
      public String getUsername() {  
           return username;  
      }  
      public void setUsername(String username) {  
           this.username = username;  
      }  
 }  

UserId Class
 package com.springimplant.userapi.entity;  
 import java.io.Serializable;  
 import java.math.BigInteger;  
 public class UserId implements Serializable  
 {  
      private BigInteger userid;  
      private BigInteger courseid;  
      public BigInteger getUserid()   
      {  
           return userid;  
      }  
      public void setUserid(BigInteger userid)   
      {  
           this.userid = userid;  
      }  
      public BigInteger getCourseid()   
      {  
           return courseid;  
      }  
      public void setCourseid(BigInteger courseid)   
      {  
           this.courseid = courseid;  
      }  
 }  
JPA Projections
  • When we use DTO as classes, the DTO should have same variable as that of domain model.
    • There should be a time, lysed, constructor, getter methods, and tostring method.
  • When we use DTO as interfaces, we need to have property names as that of the domain model
    • We only have abstract getter methods of properties here.
    • The implementing class for this is generated by JPA at run time.
    • These interfaces are called as projections

No comments:

Post a Comment