What are the steps to insert an entity?

Answer

We can easily insert the data into the database through the entity. The EntityManager provides persist() method to add records. The following steps are used to insert the record into the database.

    • Create an entity class, for example, Student.java with the attribute student_name.
 
  1. package com.javatpoint.jpa.student;  
  2. import javax.persistence.*;  
  3.   
  4. @Entity  
  5. @Table(name="student")  
  6. public class Student {  
  7.   
  8.     @Id  
  9.     private String s_name;  
  10.       
  11.     public StudentEntity(String s_name) {  
  12.         super();  
  13.         this.s_name = s_name;  
  14.     }  
  15.   
  16.     public StudentEntity() {  
  17.         super();  
  18.     }  
  19.   
  20.     public String getS_name() {  
  21.         return s_name;  
  22.     }  
  23.   
  24.     public void setS_name(String s_name) {  
  25.         this.s_name = s_name;  
  26.     }  
  27. }  
    • Now, map the entity class and other databases configuration in Persistence.xml file.
 
  1. <persistence>  
  2. <persistence-unit name="Student_details">  
  3.       
  4.     <class>com.javatpoint.jpa.student.StudentEntity</class>  
  5.   
  6. <properties>  
  7. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>  
  8. <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/studentdata"/>  
  9. <property name="javax.persistence.jdbc.user" value="root"/>  
  10. <property name="javax.persistence.jdbc.password" value=""/>  
  11. <property name="eclipselink.logging.level" value="SEVERE"/>  
  12. <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>  
  13. </properties>  
  14.   
  15.     </persistence-unit>  
  16. </persistence>  
    • Create a persistence class named as PersistStudent.java under com.javatpoint.jpa.persist package to persist the entity object with data
 
  1. package com.javatpoint.jpa.persist;  
  2.   
  3. import com.javatpoint.jpa.student.*;  
  4. import javax.persistence.*;  
  5. public class PersistStudent {  
  6.       
  7.     public static void main(String args[])  
  8.     {  
  9.           
  10.         EntityManagerFactory emf=Persistence.createEntityManagerFactory("Student_details");  
  11.         EntityManager em=emf.createEntityManager();  
  12.           
  13. em.getTransaction().begin();  
  14.           
  15.         StudentEntity s1=new StudentEntity();  
  16.         s1.setS_name("Gaurav");  
  17.         em.persist(s1);  
  18.         em.getTransaction().commit();  
  19.         emf.close();  
  20.         em.close();  
  21.     }  
  22. }  

All jpa Questions

Ask your interview questions on jpa

Write Your comment or Questions if you want the answers on jpa from jpa Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---