天天看點

jpa複合主鍵的使用

package com.ljq.entity;

import javax.persistence.Column;

import javax.persistence.Embeddable;

/**

* 使用複合主鍵要滿足的條件

*

* 1、要實作序列化 2、提供預設的構造方法 3、實作hashCope

* @author Administrator

*/

@SuppressWarnings("serial")

@Embeddable //embeddable: 可嵌入的

public class AirLinePk implements java.io.Serializable {

@Column(length = 3)

private String startCity;// 出發城市

private String endCity;// 抵達城市

public AirLinePk() {

super();

}

public AirLinePk(String startCity, String endCity) {

this.startCity = startCity;

this.endCity = endCity;

public String getEndCity() {

return endCity;

public void setEndCity(String endCity) {

public String getStartCity() {

return startCity;

public void setStartCity(String startCity) {

@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;

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

final AirLinePk other = (AirLinePk) obj;

if (endCity == null) {

if (other.endCity != null)

} else if (!endCity.equals(other.endCity))

if (startCity == null) {

if (other.startCity != null)

} else if (!startCity.equals(other.startCity))

}

import javax.persistence.EmbeddedId;

import javax.persistence.Entity;

@Entity

public class AirLine implements java.io.Serializable{

@EmbeddedId

private AirLinePk id;

@Column(length=20)

private String name;

public AirLine() {

public AirLine(String startCity,String endCity, String name) {

this.id =new AirLinePk(startCity,endCity);

this.name = name;

public AirLinePk getId() {

return id;

public void setId(AirLinePk id) {

this.id = id;

public String getName() {

return name;

public void setName(String name) {

package com.ljq.test;

import java.util.List;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;

import org.junit.Test;

import com.ljq.entity.AirLine;

import com.ljq.entity.AirLinePk;

public class PKTest {

@Test

public void save() {

EntityManagerFactory factory = Persistence.createEntityManagerFactory("ljq");

EntityManager em=factory.createEntityManager();

em.getTransaction().begin();

em.persist(new AirLine("PEK","SHA","北京飛上海"));

em.getTransaction().commit();

em.close();

factory.close();

public void find() {

AirLine airLine=em.find(AirLine.class, new AirLinePk("PEK","SHA"));

System.out.println(airLine.getName());

public void update() {

AirLine airLine=em.getReference(AirLine.class, new AirLinePk("PEK","SHA"));

airLine.setName("北京飛上海北京飛上海");

em.merge(airLine);

@SuppressWarnings({ "unused", "unchecked" })

public void list() {

List<AirLine> airLines=em.createQuery("select o from AirLine o").getResultList();

for(AirLine air:airLines){

System.out.println(air.getName());

}

public void detele() {

em.remove(em.getReference(AirLine.class, new AirLinePk("PEK","SHA")));

* 用來判斷映射是否成功

*

public void test() {

Persistence.createEntityManagerFactory("ljq");