JPA 系列教程11-复合主键-2个@Id

复合主键

指多个主键联合形成一个主键组合

需求产生

比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示

ddl语句

CREATE TABLE `t_airline` (
`startCity` varchar(3) NOT NULL,
`endCity` varchar(3) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`startCity`,`endCity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Airline

package com.jege.jpa.composite;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:复合主键-2个@Id
*/
@Entity
@Table(name = "t_airline")
public class Airline implements Serializable {
private static final long serialVersionUID = -906357110051689484L;
@Id
@Column(length = 3)
private String startCity;
@Id
@Column(length = 3)
private String endCity;
private String name; public String getStartCity() {
return startCity;
} public void setStartCity(String startCity) {
this.startCity = startCity;
} public String getEndCity() {
return endCity;
} public void setEndCity(String endCity) {
this.endCity = endCity;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "Airline [startCity=" + startCity + ", endCity=" + endCity + ", name=" + name + "]";
} }

MainTest

package com.jege.jpa.composite;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceException; import org.junit.Test; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:复合主键-2个@Id测试
*/
public class MainTest {
@Test
public void crud() {
EntityManagerFactory entityManagerFactory = null;
EntityManager entityManager = null; try {
entityManagerFactory = Persistence.createEntityManagerFactory("com.jege.jpa");
entityManager = entityManagerFactory.createEntityManager(); Airline airline = new Airline();
airline.setStartCity("PEK");
airline.setEndCity("SHA");
airline.setName("bj to sh");// 新建状态 entityManager.getTransaction().begin();
entityManager.persist(airline);// 托管状态 airline.setName("北京飞上海"); entityManager.getTransaction().commit(); Airline airline1 = new Airline();
airline1.setStartCity("PEK");
airline1.setEndCity("SHA"); // hibernate:瞬时状态,持久状态(托管),游离(脱管)状态 airline1 = entityManager.find(Airline.class, airline1);
System.out.println("name:" + airline1.getName()); entityManager.getTransaction().begin();
entityManager.remove(airline1);
entityManager.getTransaction().commit();
// airline1变为删除状态 entityManager.getTransaction().begin();
entityManager.persist(airline1);
entityManager.getTransaction().commit();
} catch (PersistenceException e) {
e.printStackTrace();
entityManager.getTransaction().rollback();
throw e;
} finally {
if (entityManager != null && entityManager.isOpen())
entityManager.close();// 游离状态
if (entityManagerFactory != null && entityManagerFactory.isOpen())
entityManagerFactory.close();
}
}
}

源码地址

https://github.com/je-ge/jpa

如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!

JPA 系列教程11-复合主键-2个@Id

JPA 系列教程11-复合主键-2个@Id

上一篇:可能是 Python 中最火的第三方开源测试框架 pytest


下一篇:Python测试框架pytest入门基础