1.配置主键类
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
/**
* 1.提供无参数构造器
* 2.实现序列号接口
* 3.重写hashCode和equals方法
* @author Administrator
*
*/
@Embeddable //用在实体里面,告诉JPA只是使用该类里面的属性作为持久化字段
public class AirLinePK implements Serializable{
private String startCityString;//北京PEK
private String endCityString;
public AirLinePK(){}
public AirLinePK(String endCityString, String startCityString) {
this.endCityString = endCityString;
this.startCityString = startCityString;
}
@Column(length=3)
public String getStartCityString() {
return startCityString;
}
public void setStartCityString(String startCityString) {
this.startCityString = startCityString;
}
@Column(length=3)
public String getEndCityString() {
return endCityString;
}
public void setEndCityString(String endCityString) {
this.endCityString = endCityString;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((endCityString == null) ? 0 : endCityString.hashCode());
result = prime * result
+ ((startCityString == null) ? 0 : startCityString.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 (endCityString == null) {
if (other.endCityString != null)
return false;
} else if (!endCityString.equals(other.endCityString))
return false;
if (startCityString == null) {
if (other.startCityString != null)
return false;
} else if (!startCityString.equals(other.startCityString))
return false;
return true;
}
}
2.配置实体bean
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
@Entity
public class AirLine {
private AirLinePK id;
private String name;
public AirLine() {
// TODO Auto-generated constructor stub
}
public AirLine(String endCityString, String startCityString, String name) {
this.id=new AirLinePK(startCityString,endCityString);
this.name = name;
}
@EmbeddedId //为实体的id
public AirLinePK getId() {
return id;
}
public void setId(AirLinePK id) {
this.id = id;
}
@Column(length=20)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.Junit测试
package junit.test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.BeforeClass;
import org.junit.Test;
import com.persia.bean.AirLine;
import com.persia.bean.AirLinePK;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class CompositePKTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
//@Test
public void save(){
EntityManagerFactory factory=Persistence.createEntityManagerFactory("JPACompositePKPU");
EntityManager entityManager=factory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(new AirLine("PEK","SHA","北京飞往上海"));
entityManager.getTransaction().commit();
entityManager.close();
factory.close();
}
//@Test
public void getAirLine(){
EntityManagerFactory factory=Persistence.createEntityManagerFactory("JPACompositePKPU");
EntityManager entityManager=factory.createEntityManager();
//entityManager.getTransaction().begin();
AirLine aLine=entityManager.find(AirLine.class, new AirLinePK("SHA","PEK"));
System.out.println("查询出来的航线:"+aLine.getName());
//entityManager.getTransaction().commit();
entityManager.close();
factory.close();
}
//@Test
public void updateAirLine(){
EntityManagerFactory factory=Persistence.createEntityManagerFactory("JPACompositePKPU");
EntityManager entityManager=factory.createEntityManager();
entityManager.getTransaction().begin();
AirLine aLine=entityManager.find(AirLine.class, new AirLinePK("SHA","PEK"));
aLine.setName("bj-sh");
aLine.setId(new AirLinePK("sh","bj"));//主键无法修改
entityManager.merge(aLine);
entityManager.getTransaction().commit();
entityManager.close();
factory.close();
}
@Test
public void deleteAirLine(){
EntityManagerFactory factory=Persistence.createEntityManagerFactory("JPACompositePKPU");
EntityManager entityManager=factory.createEntityManager();
entityManager.getTransaction().begin();
AirLine aLine=entityManager.find(AirLine.class, new AirLinePK("SHA","PEK"));
entityManager.remove(aLine);
entityManager.getTransaction().commit();
entityManager.close();
factory.close();
}
}