110
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.tutoraspire.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.tutoraspire.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.
Next TopicJPA Finding an Entity