当前位置: 首页 > 教程 > JPA >

JPA列表映射

精华
小牛编辑
149浏览
2023-03-14

列表(List)是一个用于基于索引插入和删除元素的接口。当需要以用户定义的顺序检索元素时,可以使用它。

列表映射实例

在这个例子中,我们将一个对象嵌入到一个实体类中,并将其定义为一个集合类型 - List

private List<Address> address=new ArrayList<Address>();

这个例子包含以下步骤 -

第1步:com.yiibai.jpa包下创建一个实体类Employee.java,这个类包含员工idname 和嵌入对象(员工地址)。注解 @ElementCollection 表示嵌入对象。

文件:Employee.java 的代码如下所示 -

package com.yiibai.jpa;

import java.util.*;

import javax.persistence.*;

@Entity

public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int e_id;
    private String e_name;

    @ElementCollection
    private List<Address> address = new ArrayList<Address>();

    public int getE_id() {
        return e_id;
    }

    public void setE_id(int e_id) {
        this.e_id = e_id;
    }

    public String getE_name() {
        return e_name;
    }

    public void setE_name(String e_name) {
        this.e_name = e_name;
    }

    public List<Address> getAddress() {
        return address;
    }

    public void setAddress(List<Address> address) {
        this.address = address;
    }

}

第2步:com.yiibai.jpa包下创建一个嵌入对象Address.java类。 注解@Embeddable表示可嵌入对象。

文件:Address.java 的代码如下所示 -

package com.yiibai.jpa;

import javax.persistence.*;

@Embeddable
public class Address {

    private int e_pincode;
    private String e_city;
    private String e_state;

    public int getE_pincode() {
        return e_pincode;
    }

    public void setE_pincode(int e_pincode) {
        this.e_pincode = e_pincode;
    }

    public String getE_city() {
        return e_city;
    }

    public void setE_city(String e_city) {
        this.e_city = e_city;
    }

    public String getE_state() {
        return e_state;
    }

    public void setE_state(String e_state) {
        this.e_state = e_state;
    }

}

第3步: 将实体类和其他数据库配置映射到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="Collection_Type">
        <class>com.yiibai.jpa.Employee</class>
        <class>com.yiibai.jpa.Address</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>

com.yiibai.collection包下创建一个持久化类ListMapping.java,以便将实体对象与数据保持一致。

文件:ListMapping.java 的代码如下所示 -

package com.yiibai.collection;

import javax.persistence.*;

import com.yiibai.jpa.*;

public class ListMapping {

    public static void main(String[] args) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Collection_Type");
        EntityManager em = emf.createEntityManager();

        System.out.println("Start work...");
        em.getTransaction().begin();

        Address a1 = new Address();
        a1.setE_pincode(201301);
        a1.setE_city("Guangzhou");
        a1.setE_state("GuangDong");

        Address a2 = new Address();
        a2.setE_pincode(302001);
        a2.setE_city("Haikou");
        a2.setE_state("Hainan");

        Employee e1 = new Employee();
        e1.setE_id(1);
        e1.setE_name("Maxsu");
        e1.getAddress().add(a1);

        Employee e2 = new Employee();
        e2.setE_id(2);
        e2.setE_name("John");
        e2.getAddress().add(a2);

        em.persist(e1);
        em.persist(e2);

        em.getTransaction().commit();

        em.close();
        emf.close();
        System.out.println("End work...");

    }

}

程序输出结果

程序执行完成后,下面的表格在MySQL工作台下生成。

  • Employee表 - 此表包含员工详细信息。要获取数据,请在MySQL中运行select * from employee查询。结果如下所示 -
    mysql> select * from employee;
    +------+--------+
    | E_ID | E_NAME |
    +------+--------+
    |    1 | Maxsu  |
    |    2 | John   |
    +------+--------+
    2 rows in set
    
  • Employee_address表 - 此表表示员工和地址表之间的映射。 要获取数据,请在MySQL中运行select * from employee_address查询。结果如下所示 -
    mysql> select * from employee_address;
    +-----------+-----------+-----------+---------------+
    | E_CITY    | E_PINCODE | E_STATE   | Employee_E_ID |
    +-----------+-----------+-----------+---------------+
    | Guangzhou |    201301 | GuangDong |             1 |
    | Haikou    |    302001 | Hainan    |             2 |
    +-----------+-----------+-----------+---------------+
    2 rows in set
    

以下是纠正/补充内容:

Employee e1 = new Employee e1.setE_id1 e1.setE_name"Maxsu" e1.getAddress.adda1 Employee e2 = new Employee e2.setE_id2 e2.setE_name"John" e2.getAddress.adda2应该为:Employee e1 = new Employee e1.setE_id1 e1.setE_name"Maxsu" e1.setAddress.adda1 Employee e2 = new Employee e2.setE_id2 e2.setE_name"John" e2.setAddress.adda2   提交时间:2019-10-10