步骤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();
}
}
看能将表创建出来,并插入数据即可。
分享到:
相关推荐
赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...
JPA学习笔记-EJB-04JPA关联映射总结 JPA JPA关联映射JPA学习笔记-EJB-04JPA关联映射总结 JPA JPA关联映射
hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码 hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码
hibernate-jpa-2.1-api-1.0.0.final.jar.zip,内含hibernate-jpa-2.1-api-1.0.0.final.jar
总结一下关于JPA的主键生成策略,JPA是用@GeneratedValue标记来注释的。一般的我把主键生成分成两大类。第一个就是简单的单字段主键类型,一个就是复杂的复合主键类型。我们分2种情况分别讨论。 第一种单字段主键...
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar官方下载,请放心使用
hibernate-jpa-2.0-api-1.0.1.Final-sources.jar hibernate jpa 源代码
java.lang.NoClassDefFoundError: javax/persistence/spi/ProviderUtil 或javax/persistence/entitylistener问题
NULL 博文链接:https://ylq365.iteye.com/blog/1664599
java运行依赖jar包
hibernate-jpa-2.0-api-1.0.1.Final-sources
hibernate-jpa-2.1-api-1.0.0.final-sources.jar.zip的源代码
JPA学习笔记-EJB-06JPA+Spring使用经验。
antlr-2.7.6.jar, cglib-2.2.jar, commons-collections-3.1.jar, ...hibernate-jpa-2.0-api-1.0.0.Final.jar, javassist-3.9.0.GA.jar, jta-1.1.jar, slf4j-api-1.6.1.jar, slf4j-log4j12-1.6.1.jar
JPA开发文档总结 包括:1)发展中的持久化技术 2)JPA体系架构 3)Entity Bean 4)EntityManager 5)JPA Query 总共5个部分
java运行依赖jar包