JPA allows us to change the records in database by updating an entity.
JPA Entity Update Example
Here, we will update the age of a student on the basis of primary key.
This example contains the following steps: -
1. Create an entity class named as StudentEntity.java under com.javahubpoint.jpa.student package that contains attributes s_id, s_name and s_age.
StudentEntity.java
package com.javahubpoint.jpa.student;
import javax.persistence.*;
@Entity
@Table(name="student")
public class StudentEntity {
@Id
private int s_id;
private String s_name;
private int s_age;
public StudentEntity(int s_id, String s_name, int s_age) {
super();
this.s_id = s_id;
this.s_name = s_name;
this.s_age = s_age;
}
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;
}
public int getS_age() {
return s_age;
}
public void setS_age(int s_age) {
this.s_age = s_age;
}
}
2. Now, map the entity class and other databases configuration in Persistence.xml file.
Persistence.xml
<persistence>
<persistence-unit name="Student_details">
<class>com.javahubpoint.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>
<persistence-unit name="Student_details">
<class>com.javahubpoint.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>
3. Create a persistence class named as UpdateStudent.java under com.javahubpoint.jpa.update package to persist the entity object with data.
UpdateStudent.java
package com.javahubpoint.jpa.update;
import javax.persistence.*;
import com.javahubpoint.jpa.student.*;
public class UpdateStudent {
public static void main(String args[])
{
EntityManagerFactory emf=Persistence.createEntityManagerFactory("Student_details");
EntityManager em=emf.createEntityManager();
StudentEntity s=em.find(StudentEntity.class,102);
System.out.println("Before Updation");
System.out.println("Student id = "+s.getS_id());
System.out.println("Student Name = "+s.getS_name());
System.out.println("Student Age = "+s.getS_age());
s.setS_age(30);
import javax.persistence.*;
import com.javahubpoint.jpa.student.*;
public class UpdateStudent {
public static void main(String args[])
{
EntityManagerFactory emf=Persistence.createEntityManagerFactory("Student_details");
EntityManager em=emf.createEntityManager();
StudentEntity s=em.find(StudentEntity.class,102);
System.out.println("Before Updation");
System.out.println("Student id = "+s.getS_id());
System.out.println("Student Name = "+s.getS_name());
System.out.println("Student Age = "+s.getS_age());
s.setS_age(30);
em.persist(s);
em.getTransaction().commit();
emf.close();
em.close();
System.out.println("After Updation");
System.out.println("Student id = "+s.getS_id());
System.out.println("Student Name = "+s.getS_name());
System.out.println("Student Age = "+s.getS_age());
}
}
em.getTransaction().commit();
emf.close();
em.close();
System.out.println("After Updation");
System.out.println("Student id = "+s.getS_id());
System.out.println("Student Name = "+s.getS_name());
System.out.println("Student Age = "+s.getS_age());
}
}
Output: