JPAにおける複合キーのマッピング

6163 ワード

航空システムでは、航路には出発都市と到着都市の複合メインキーがあり、1つの航路が複数のフライトを持つことができるため、フライト対航路は多対一の関係である.メンテナンスはフライトです.航路の主キーは複合主キーなので、フライトクラスで航路の主キーを引用して外キーにするにはどうすればいいですか?使用法は次のとおりです.
1、複合主キークラスの作成
シーケンス化インタフェースの実装とhashcodeとequalsの書き換え方法が必要
Jpa注記の使い方はコードを参照してください.
package cn.itcast.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class AirLinePK implements Serializable{//JPA      
	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;
		final 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、航路類の作成
package cn.itcast.bean;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.OneToMany;

@Entity
public class AirLine {
	private AirLinePK id;
	private Boolean onpp = true; //    
	private Set<Filght> filghts = new HashSet<Filght>();
	
	@OneToMany(mappedBy="airLine", cascade=CascadeType.REFRESH)
	public Set<Filght> getFilghts() {
		return filghts;
	}

	public void setFilghts(Set<Filght> filghts) {
		this.filghts = filghts;
	}

	public AirLine(){}
	
	public AirLine(AirLinePK id) {
		this.id = id;
	}
	
	public AirLine(String startcity, String endcity) {
		this.id = new AirLinePK(startcity, endcity);
	}
	
	@EmbeddedId    //         
	public AirLinePK getId() {//         
		return id;
	}
	public void setId(AirLinePK id) {
		this.id = id;
	}
	@Column(nullable=false)
	public Boolean getOnoff() {
		return onpp;
	}
	public void setOnoff(Boolean onoff) {
		this.onpp = onoff;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.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;
		final AirLine other = (AirLine) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}

3、フライトのコード
package cn.itcast.bean;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;

@Entity
public class Filght {
	private String filghtno;
	private String aircompany;
	private AirLine airLine;
	
	@ManyToOne(cascade=CascadeType.REFRESH, optional=false)
	@JoinColumns({//                    。
		@JoinColumn(name="startcity", referencedColumnName="startcity"),
		@JoinColumn(name="endcity", referencedColumnName="endcity")
	})
	public AirLine getAirLine() {
		return airLine;
	}
	public void setAirLine(AirLine airLine) {
		this.airLine = airLine;
	}
	@Id @Column(length=6)
	public String getFilghtno() {
		return filghtno;
	}
	public void setFilghtno(String filghtno) {
		this.filghtno = filghtno;
	}
	@Column(length=2)
	public String getAircompany() {
		return aircompany;
	}
	public void setAircompany(String aircompany) {
		this.aircompany = aircompany;
	}
	
}

テストコード
@Test
	public void save(){
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		AirLine airLine = new AirLine("PEK", "CAN");
		em.persist(airLine);
		
		Filght filght = new Filght();
		filght.setFilghtno("CA1002");
		filght.setAircompany("CA");
		filght.setAirLine(airLine);
		em.persist(filght);
		
		em.getTransaction().commit();
		em.close();
		factory.close();
	}