JPA中的联合主键
来源 itcast
第一步:
照样先搭建环境导入JPA相应的实现产品如:hibernate toplink 的jar包 及数据库驱动
在classpath路径下配置META-INF 文件夹 下面文件 persistence.xml 一定要放在classpath路径下
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<!-- 以transaction-type下是 事务提交类型有两种事务:第一、本地事务(在同一个数据库中完成事务) 第二、全局事务(在不同数据库中需要在同一事务中完成不同数据库的操作)-->
<persistence-unit name="person" transaction-type="RESOURCE_LOCAL">
<properties>
<!-- 使用的方言 -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver" />
<!-- 数据库用户名 -->
<property name="hibernate.connection.username" value="root" />
<!-- 数据库密码 -->
<property name="hibernate.connection.password" value="liyong" />
<!-- 数据库连接url -->
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8" />
<!-- 表结构发生变化的时候更新表结构 ,表不存在是创建表-->
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
第二步:编写复合主键类
@Embeddable// @Embeddable只使用这个实体内的属性
/*
* 作为复合主键条件
* 1、必须使用这个注解@Embeddable
* 2、必须序列化
* 3、必须重写hashCode()和equels()方法
*/
public class AirLinePK implements Serializable{
private static final long serialVersionUID = 1L;
private String startCity;
private String endCity;
public AirLinePK(){}
public AirLinePK(String startCity, String endCity) {
super();
this.endCity = endCity;
this.startCity = startCity;
}
@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;
}
}
第三步 :编写关系对象映射类
@Entity
public class AirLine {
private String name;
private AirLinePK id;
public AirLine(){}
public AirLine(String name,String start,String end){
this.name=name;
this.id=new AirLinePK(start,end);
}
@Column(length=20)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@EmbeddedId//这是复合主键
public AirLinePK getId() {
return id;
}
public void setId(AirLinePK id) {
this.id = id;
}
}
第四步:编写单元测试
@Entity
public class AirLine {
private String name;
private AirLinePK id;
public AirLine(){}
public AirLine(String name,String start,String end){
this.name=name;
this.id=new AirLinePK(start,end);
}
@Column(length=20)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@EmbeddedId//这是复合主键
public AirLinePK getId() {
return id;
}
public void setId(AirLinePK id) {
this.id = id;
}
}
第五步:测试。。。。
代码见附件。。。。。。。。。