JPA中将联合主键看做一个对象,所以将联合主键放在一个类中
AirLine.java
import javax.persistence.Column;
import javax.persistence.EmbeddedId;}
AirLinePK.java
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
/**
*
* 建立联合主键类有三个要求:
* 1.实现一个无参的构造函数
* 2.实现序列化接口,因为要保存在数据库中就必须实现序列化接口
* 3.重写equal和hashcode两个方法,要判断主键是否相等嘛
*
*/
@Embeddable//告诉JPA,我们只是使用联合主键类里面的属性作为实体类的持久化字段
public class AirLinePK implements Serializable {
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;
}
}
操作
@Test
public void save(){
EntityManagerFactory factory=Persistence.createEntityManagerFactory("itcast");
EntityManager em=factory.createEntityManager();
em.getTransaction().begin();
em.persist(new AirLine("PEK", "SHA", "北京飞上海"));
em.getTransaction().commit();
em.close();
factory.close();
}
一般不会用,因为这属于业务主键,与业务相关,而实际上我们推荐使用自然主键,非业务相关的。