首页 > 代码库 > JPA学习---第十二节:JPA中的联合主键

JPA学习---第十二节:JPA中的联合主键

1、定义实体类,代码如下:

(1)、将联合主键放到一个类中,代码如下:

package learn.jpa.entity;import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Embeddable;/** *  * 1、必须要有无擦得构造函数 * 2、必须要实现序列接口 * 3、必须重写 equals() 和 hashCode() 方法 * @Embeddable 告诉 jpa 只是使用实体类里面的属性 */@Embeddablepublic class AirLinePK implements Serializable{    /**     *      */    private static final long serialVersionUID = 1L;    private String startCity;    private String endCity;        public AirLinePK() {            }    public AirLinePK(String startCity, String endCity) {        this.startCity = startCity;        this.endCity = endCity;    }        @Column(length=3)    public String getStartCity() {        return startCity;    }    public void setStartCity(String startCity) {        this.startCity = startCity;    }    @Column(length=3)    public String getEndCity() {        return endCity;    }    public void setEndCity(String endCity) {        this.endCity = endCity;    }    @Override    public int hashCode() {        final int prime = 31;        int result = 1;        result = prime * result + ((endCity == null) ? 0 : endCity.hashCode());        result = prime * result                + ((startCity == null) ? 0 : startCity.hashCode());        return result;    }    @Override    public boolean equals(Object obj) {        if (this == obj)            return true;        if (obj == null)            return false;        if (getClass() != obj.getClass())            return false;        AirLinePK other = (AirLinePK) obj;        if (endCity == null) {            if (other.endCity != null)                return false;        } else if (!endCity.equals(other.endCity))            return false;        if (startCity == null) {            if (other.startCity != null)                return false;        } else if (!startCity.equals(other.startCity))            return false;        return true;    }    }

(2)、定义 AirLine 实体类,代码如下:

package learn.jpa.entity;import javax.persistence.Column;import javax.persistence.EmbeddedId;import javax.persistence.Entity;@Entitypublic class AirLine {    private AirLinePK id;    private String name;        public AirLine(){}        public AirLine(AirLinePK id) {        this.id = id;    }        public AirLine(String startCity, String endCity, String name){        this.id = new AirLinePK(startCity, endCity);        this.name = name;    }        // @EmbeddedId 用于标注为实体的标识符    @EmbeddedId    public AirLinePK getId() {        return id;    }    public void setId(AirLinePK id) {        this.id = id;    }    @Column(length=30)    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

2、测试类,代码:

package learn.jpa.test;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.Persistence;import learn.jpa.entity.AirLine;import org.junit.Test;public class AirLineTest {    /**     * 测试数据库是否可以生成表     */    @Test    public void test() {        EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa");        factory.close();    }        @Test    public void save(){        EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa");        EntityManager em = factory.createEntityManager();        em.getTransaction().begin();   // 开启事务        em.persist(new AirLine("PEK","SHA","北京飞上海"));        em.getTransaction().commit();        em.close();        factory.close();    }}

注意:

1、必须实现Serializable序列化

2、必须提示无参的构造方法

3、必须重写hashCode和equals方法

@Embeddable 表示该类中所有属性在应用该联合主键的类中作为它的属性(字段)

JPA学习---第十二节:JPA中的联合主键