Spring Data JPA
Spring Data JPA
Spring Data JPA
The Java Persistence API (JPA) is the specification of Java that is used to persist data
between Java object and relational database. JPA acts as a bridge between object-
oriented domain models and relational database systems. As JPA is just a
specification, it doesn't perform any operation by itself. It requires an
implementation. Therefore, ORM tools like Hibernate, TopLink, and iBatis implements
JPA specifications for data persistence. The first version of the Java Persistence API,
JPA 1.0 was released in 2006 as a part of EJB 3.0 specification.
o Hibernate
o TopLink
o ORMLite
o iBATIS
o JPOX
EntityManagerFactory emf=Persistence.createEntityManagerFactory("St
udent_details");
EntityManager em=emf.createEntityManager();
em.getTransaction().begin();
em.persist(s1);
em.getTransaction().commit();
emf.close();
em.close();
package com.javatpoint.jpa.student;
import javax.persistence.*;
@Entity
@Table(name="student")
public class Student {
@Id
private String s_name;
public StudentEntity(String s_name) {
super();
this.s_name = s_name;
}
public StudentEntity() {
super();
}
public String getS_name() {
return s_name;
}
public void setS_name(String s_name) {
this.s_name = s_name;
}
}
<persistence>
<persistence-unit name="Student_details">
<class>com.javatpoint.jpa.student.StudentEntity</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Dri
ver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://
localhost:3306/studentdata"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="SEVERE"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-
tables"/>
</properties>
</persistence-unit>
</persistence>
o Create a persistence class named as PersistStudent.java under
com.javatpoint.jpa.persist package to persist the entity object with data
package com.javatpoint.jpa.persist;
import com.javatpoint.jpa.student.*;
import javax.persistence.*;
public class PersistStudent {
public static void main(String args[])
{
EntityManagerFactory emf=Persistence.createEntityManagerFactory("
Student_details");
EntityManager em=emf.createEntityManager();
em.getTransaction().begin();
StudentEntity s1=new StudentEntity();
s1.setS_name("Gaurav");
em.persist(s1);
em.getTransaction().commit();
emf.close();
em.close();
}
}
1. package com.javatpoint.jpa.student;
2. import javax.persistence.*;
3.
4. @Entity
5. @Table(name="student")
6. public class StudentEntity {
7.
8. @Id
9. private String s_name;
10. private int s_id;
11. public StudentEntity(String s_name, int s_id) {
12. super();
13. this.s_name = s_name;
14. this.s_id = s_id;
15. }
16.
17. public StudentEntity() {
18. super();
19. }
20.
21.
22. public String getS_id() {
23. return s_id;
24. }
25.
26. public void setS_id(int s_id) {
27. this.s_name = s_id;
28. }
29. public String getS_name() {
30. return s_name;
31. }
32.
33. public void setS_name(String s_name) {
34. this.s_name = s_name;
35. }
36. }
<persistence>
<persistence-unit name="Student_details">
<class>com.javatpoint.jpa.student.StudentEntity</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Dri
ver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://
localhost:3306/studentdata"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="SEVERE"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-
tables"/>
</properties>
</persistence-unit>
</persistence>
package com.javatpoint.jpa.find;
import javax.persistence.*;
import com.javatpoint.jpa.student.*;
public class FindStudent {
public static void main(String args[])
{
EntityManagerFactory emf=Persistence.createEntityManagerFactory("
Student_details");
EntityManager em=emf.createEntityManager();
StudentEntity s=em.find(StudentEntity.class,101);
System.out.println("Student id = "+s.getS_id());
System.out.println("Student Name = "+s.getS_name());
System.out.println("Student Age = "+s.getS_age());
}
}
StudentEntity.java
package com.javatpoint.jpa.student;
import javax.persistence.*;
@Entity
@Table(name="student")
public class StudentEntity {
@Id
private String s_name;
private int s_id;
public StudentEntity(String s_name, int s_id) {
super();
this.s_name = s_name;
this.s_id = s_id;
}
public StudentEntity() {
super();
}
public String getS_id() {
return s_id;
}
public void setS_id(int s_id) {
this.s_name = s_id;
}
public String getS_name() {
return s_name;
}
public void setS_name(String s_name) {
this.s_name = s_name;
}
}
Now, map the entity class and other databases configuration in Persistence.xml
file.
Persistence.xml
<persistence>
<persistence-unit name="Student_details">
<class>com.javatpoint.jpa.student.StudentEntity</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/
studentdata"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="SEVERE"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
</properties>
</persistence-unit>
</persistence>
UpdateStudent.java
package com.javatpoint.jpa.update;
import javax.persistence.*;
import com.javatpoint.jpa.student.*;
public class UpdateStudent {
public static void main(String args[])
{
EntityManagerFactory emf=Persistence.createEntityManagerFactory("Studen
t_details");
EntityManager em=emf.createEntityManager();
StudentEntity s=em.find(StudentEntity.class,102);
System.out.println("Before Updation");
System.out.println("Student Name = "+s.getS_name());
s.setName("Ayush");
System.out.println("After Updation");
System.out.println("Student Name = "+s.getS_name());
}
}
package com.javatpoint.jpa.student;
import javax.persistence.*;
@Entity
@Table(name="student")
public class StudentEntity {
@Id
private int s_id;
private String s_name;
public StudentEntity(int s_id, String s_name) {
super();
this.s_id = s_id;
this.s_name = s_name;
}
public StudentEntity() {
super();
}
public int getS_id() {
return s_id;
}
public void setS_id(int s_id) {
this.s_id = s_id;
}
public String getS_name() {
return s_name;
}
public void setS_name(String s_name) {
this.s_name = s_name;
}
}
Now, map the entity class and other databases configuration in Persistence.xml
file.
Persistence.xml
<persistence>
<persistence-unit name="Student_details">
<class>com.javatpoint.jpa.student.StudentEntity</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Dri
ver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://
localhost:3306/studentdata"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="SEVERE"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-
tables"/>
</properties>
</persistence-unit>
</persistence>
Deletion.java
package com.javatpoint.jpa.delete;
import javax.persistence.*;
import com.javatpoint.jpa.student.*;
public class DeleteStudent {
public static void main(String args[])
{
EntityManagerFactory emf=Persistence.createEntityManagerFactory("Stu
dent_details");
EntityManager em=emf.createEntityManager();
em.getTransaction().begin();
StudentEntity s=em.find(StudentEntity.class,102);
em.remove(s);
em.getTransaction().commit();
emf.close();
em.close();
}
}
o Automatic Id generation - In this case, the application doesn't care about the
kind of id generation and hand over this task to the provider. If any value is
not specified explicitly, the generation type defaults to auto.
o Id generation using a table - The identifiers can also be generated using a
database table.
o Id generation using a database sequence - Databases support an internal
mechanism for id generation called sequences. To customize the database
sequence name, we can use the JPA @SequenceGenerator annotation.
o Id generation using a database identity - In this approach, whenever a row is
inserted into the table, a unique identifier is assigned to the identity column
that can be used to generate the identifiers for the objects.