欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JPA及联合主键的使用

程序员文章站 2022-03-02 15:37:43
...
JPA全称为java persistence API,是原SUN公司提出的java持久化规范,它为开发人员提供一种对象/关系映射工具来管理java应用中的关系数据,它的出现是为了简化现有的持久化开发工作和整合ORM技术,结束了现在Hibernate,TopLink,JDO各自为营的局面,JPA不是一种ORM框架,它只是规范了现有的ORM技术,这点类似于JDBC,此时开发出来的应用不再依赖于某个开发商,在不修改代码的情况下,只要在JPA环境下应该都可以运行,做到了低耦合,可扩展性
JPA规范要求在类路径META-INF目录下放置persistence.xml,文件的名称是固定的,下面我就用联合主键的小例子来演示JPA的使用

1.新建一个java项目,引入所需的jar包,我选的JPA的实现框架是Hibernate,在src目录下新建一个文件夹META-INF,在其文件夹先新建配置文件persistence.xml

<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="testJPA" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/数据库名?userUnicode=true&characterEncoding=UTF-8" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password"
value="数据库密码" />

</properties>
</persistence-unit>
</persistence>


我用的是MYSQL数据库,所谓联合主键就是一个实体类中主键由两个或者两个以上字段构成,以下是以飞机票为例
联合主键类


package com.lamp.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class AirLinePK implements Serializable {

private static final long serialVersionUID = -1487096630571343331L;

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 getEndCity() {
return endCity;
}

public void setEndCity(String endCity) {
this.endCity = endCity;
}

@Column(length=3)
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;
}

}


实体类


package com.lamp.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;
}
}


写个测试类

@Test
public void save(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("testJPA");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();

em.persist(new AirLine("PEK","SHA","从北京飞上海"));

em.getTransaction().commit();
em.close();
factory.close();
}


既然说到了实体就不得不说说四种实体bean的存在方式
分别是新建,托管,游离,删除,新建,删除好理解,新建就是new一个新的对象,删除就是删除记录,托管是指在事务开启的情况下从数据库取出一条记录并将其某一属性值修改然后提交这样数据库中相应的记录就会得到更新,而游离也是在事务开启的情况下得到记录,不同托管的是实体管理器需将其下的所有实体变成游离状态,举个例子如下

EntityManagerFactory factory = Persistence.createEntityManagerFactory("testJPA");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Person person = em.find(Person.class, 1);
em.clear(); //把实体管理器中的实体全部变成游离状态
person.setName("游离状态后设置的名称");
em.merge(person);
em.getTransaction().commit();
em.close();
factory.close();

着眼未来几年的技术走向,JPA作为ORM领域标准化的整合者的目标不难实现,让我们共同期待