在航空系统中,航线有出发城市和到达城市复合主键确定 一个航线可以有多个航班 所以航班对航线是多对一的关系。维护端为航班。由于航线的主键为复合主键,在航班类中要引用航线的主键做为外键 该怎么使用呢?下面是用法:
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(); }