JPA 菜鸟教程 12 复合主键-2个@Id+@IdClass

时间:2022-09-11 12:58:31

GitHub

复合主键

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

需求产生

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

ddl语句

同复合主键-2个@Id一样

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.IdClass;
import javax.persistence.Table;

/**
* @author JE哥
* @email 1272434821@qq.com
* @description:复合主键-2个@Id+@IdClass
*/

@Entity
@Table(name = "t_airline")
@IdClass(Airline.AirlinePK.class)
public class Airline {
@Id
private String startCity;
@Id
private String endCity;
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = 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;
}

@Override
public String toString() {
return "Airline [startCity=" + startCity + ", endCity=" + endCity + ", name=" + name + "]";
}

// 必须static的class
public static class AirlinePK implements Serializable {
private static final long serialVersionUID = -7189167162738318201L;
@Column(length = 3, nullable = false)
private String startCity;
@Column(length = 3, nullable = false)
private String endCity;

public AirlinePK() {
}

public AirlinePK(String startCity, String endCity) {
this.startCity = startCity;
this.endCity = endCity;
}

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;
}

@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;
}

@Override
public String toString() {
return "AirlinePK [startCity=" + startCity + ", endCity=" + endCity + "]";
}

}

}

MainTest

package com.jege.jpa.composite;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.jege.jpa.composite.Airline.AirlinePK;

/**
* @author JE哥
* @email 1272434821@qq.com
* @description:复合主键-2个@Id+@IdClass测试
*/

public class MainTest {
private static EntityManagerFactory entityManagerFactory = null;
private EntityManager entityManager = null;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
entityManagerFactory = Persistence.createEntityManagerFactory("com.jege.jpa");
}

@Before
public void setUp() throws Exception {
entityManager = entityManagerFactory.createEntityManager();
}

@Test
public void persist() {
Airline airline = new Airline();
airline.setEndCity("SHA");
airline.setStartCity("PEK");
airline.setName("北京飞上海");

entityManager.getTransaction().begin();
entityManager.persist(airline);
entityManager.getTransaction().commit();
}

@Test
public void find() {
persist();

AirlinePK pk = new AirlinePK("PEK", "SHA");
Airline airline = entityManager.find(Airline.class, pk);
System.out.println(airline);
}

@After
public void tearDown() throws Exception {
if (entityManager != null && entityManager.isOpen())
entityManager.close();
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
if (entityManagerFactory != null && entityManagerFactory.isOpen())
entityManagerFactory.close();
}
}

其他关联项目

源码地址

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

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
您的支持将鼓励我继续创作!谢谢!
JPA 菜鸟教程 12 复合主键-2个@Id+@IdClass
JPA 菜鸟教程 12 复合主键-2个@Id+@IdClass