JPA插入實體


在JPA中,我們可以通過實體輕鬆地將資料插入到資料庫中。 EntityManager提供persist()方法來插入資料記錄。

JPA實體插入範例

在這裡,我們將演示如何使用實現插入學生的記錄。

建立一個JPA專案,其專案目錄結構如下所示 -

這個例子包含以下步驟 -

第1步:com.yiibai.jpa.student包下建立一個名為StudentEntity.java的實體類,這個類包含以下幾個屬性:s_ids_names_age

檔案:StudentEntity.java 的程式碼如下 -

package com.yiibai.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步: 現在,將實體類和其他資料庫組態對映到persistence.xml檔案中。

檔案:persistence.xml 的內容如下 -

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="Student_details">
        <class>com.yiibai.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/testdb?serverTimezone=UTC" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="123456" />
            <property name="eclipselink.logging.level" value="SEVERE" />
            <property name="eclipselink.ddl-generation"
                value="create-or-extend-tables" />
        </properties>
    </persistence-unit>

</persistence>

第3步:com.yiibai.jpa.persist包下建立一個名為PersistStudent.java的持久化類,以便將實體物件與資料保持一致。

檔案:PersistStudent.java 的程式碼如下 -

package com.yiibai.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_id(1001);
        s1.setS_name("Maxsu");
        s1.setS_age(26);

        StudentEntity s2 = new StudentEntity();
        s2.setS_id(1102);
        s2.setS_name("Yiibai");
        s2.setS_age(23);

        StudentEntity s3 = new StudentEntity();
        s3.setS_id(1003);
        s3.setS_name("James");
        s3.setS_age(28);

        em.persist(s1);
        em.persist(s2);
        em.persist(s3);

        em.getTransaction().commit();

        em.close();
        emf.close();

        System.out.println("All job done.");

    }
}

執行上面程式後,student表在MySQL工作台下生成。此表包含學生的詳細資訊。要獲取資料,請在MySQL控制台執行select * from student查詢。得到以下結果 -