JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable

时间:2023-03-08 23:24:25
JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable

复合主键

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

需求产生

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

ddl语句

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

Airline

package com.jege.jpa.embedded;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:复合主键-@EmbeddedId
*/
@Entity
@Table(name = "t_airline")
public class Airline {
@EmbeddedId
private AirlinePK pk;
private String name; public Airline() { } public Airline(AirlinePK pk, String name) {
this.pk = pk;
this.name = name;
} public Airline(String startCity, String endCity, String name) {
pk = new AirlinePK(startCity, endCity);
this.name = name;
} public AirlinePK getPk() {
return pk;
} public void setPk(AirlinePK pk) {
this.pk = pk;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "Airline [pk=" + pk + ", name=" + name + "]";
} }

AirlinePK

package com.jege.jpa.embedded;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:复合主键-@Embeddable
*/
@Embeddable
public class AirlinePK implements Serializable {
private static final long serialVersionUID = 2836348182939717563L;
@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.embedded;

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; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:复合主键@EmbeddedId+@Embeddable测试
*/
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("PEK", "SHA", "北京飞上海");
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();
}
}
http://blog.****.net/je_ge/article/details/53678164

其他关联项目

源码地址

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

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

JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable

JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable