JPA学习总结(五)--JPACompositePK联合主键
程序员文章站
2022-03-02 15:33:25
...
步骤1.新建java项目:JPACompositePK,将hibernate包以及mysql驱动包导入
步骤2.在类路径下新建xml文件persistence.xml,将其放入META-INF文件夹下,其代码如下:
<?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"> <persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL"> <properties> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8" /> <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="root" /> <property name="hibernate.max_fetch_depth" value="3" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit> </persistence>
步骤3.编写实体Bean类
AirLinePK代码如下:
package cn.itcast.bean;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
/**
* 联合主键的要求:
* 1.要求无参数的构造函数
* 2.实现Serializable接口
* 3.重写hashCode()和equals()方法
*/
@Embeddable//在实体里面只使用该类的属性,作为实体的持久化属性
public class AirLinePK implements Serializable{
private static final long serialVersionUID = 362881507756738676L;
private String startCity;//PEK(北京),SHA(上海)
private String endCity;
public AirLinePK(){}
public AirLinePK(String startCity, String endCity) {
this.startCity = startCity;
this.endCity = endCity;
}
@Column(length=3,nullable=false)
public String getEndCity() {
return endCity;
}
public void setEndCity(String endCity) {
this.endCity = endCity;
}
@Column(length=3,nullable=false)
public String getStartCity() {
return startCity;
}
public void setStartCity(String startCity) {
this.startCity = startCity;
}
@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;
}
}
AirLine代码如下:
package cn.itcast.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(){}
public AirLine(String startCity,String endCity,String name) {
this.id = new AirLinePK(startCity,endCity);
this.name = name;
}
@EmbeddedId//复合主键的主键标注
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;
}
}
步骤4.创建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 cn.itcast.bean.AirLine;
public class AirLineTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void save(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
AirLine line = new AirLine("SHA","PEK","上海飞北京");
em.persist(line);
em.getTransaction().commit();
em.close();
factory.close();
}
}
看能将表创建出来,并插入数据即可。