Javatpoint Logo
Javatpoint Logo

Inserting an Entity

In JPA, we can easily insert data into database through entities. The EntityManager provides persist() method to insert records.

JPA Entity Insertion Example

Here, we will insert the record of students.

This example contains the following steps: -

  • Create an entity class named as StudentEntity.java under com.javatpoint.jpa.student package that contains attributes s_id, s_name, s_age.

StudentEntity.java

  • Now, map the entity class and other databases confiuguration in Persistence.xml file.

Persistence.xml

  • Create a persistence class named as PersistStudent.java under com.javatpoint.jpa.persist package to persist the entity object with data.

PersistStudent.java

Output:

After the execution of the program, the student table is generated under MySQL workbench.This table contains the student details.To fetch data, run select * from student query in MySQL.


JPA Inserting an Entity





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA