复合主键

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

需求产生

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

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的更多相关文章

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

    复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 CREATE TABLE `t_airline ...

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

    复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id和复合主键-2个@Id+ ...

  3. Hibernate 系列教程8-复合主键

    复合主键 复合主键的意思就是2个字段同时为主键 不使用无业务含义的自增id作为主键 Airline package com.jege.hibernate.compositeid; import jav ...

  4. 【hibernate/JPA】注解方式实现 复合主键【spring boot】

    1>hibernate/JPA实现复合主键的思路:是将所有的主键属性封装在一个主键类中,提供给需要复合主键的实体类使用. 2>主键类的几点要求: . 使用复合主键的实体类必须实现Seria ...

  5. Hibernate复合主键映射

    目录: 1. 实现方式一:将复合主键对应的属性与实体其他普通属性放在一起 2. 实现方式二:将主键属性提取到一个主键类中,实体类只需包含主键类的一个引用 在日常开发中会遇到这样一种情况,数据库中的某张 ...

  6. JPA 系列教程9-双向一对一唯一外键

    双向一对一唯一外键的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(25 ...

  7. SpringData JPA复合主键

    上一篇博客简单介绍了SpringData JPA实现简单的CRUD,分页与多条件的排序,那里的主键类型是Long,有时我们会遇到主键不是一个的,复合主键,经过调研如下.确定一个人,不能只根据他的姓名来 ...

  8. springboot jpa 复合主键

    https://blog.csdn.net/wyc_cs/article/details/9031991 创建一个复合主键类 public class LevelPostMultiKeysClass ...

  9. 【hibernate/JPA】对实体类的的多个字段建立唯一索引,达到复合主键的效果【spring boot】注解创建唯一索引和普通索引

    对实体类的的多个字段建立唯一索引,达到复合主键的效果 package com.sxd.swapping.domain; import lombok.Getter; import lombok.Sett ...

随机推荐

  1. 观察者模式 C++11

    #include <functional> #include <vector> #include <algorithm> #include <iostream ...

  2. VB调用WebService(SOA2.0接口)(直接Post方式)并解析返回的XML

    SOA 2.0接口 Function GetDepartmentCode(reqDeptCode) Dim soaRequestXML : soaRequestXML = "" D ...

  3. CSS绑定

    css绑定会对元素的CSS类进行操作.在某些情况下这将非常有用,例如:当数值是负的时将其高亮显示. (注:如果如果不想直接更改CSS类,而是只要改其中一个样式,则需要使用style绑定) 示例:使用静 ...

  4. leaflet地图在选项卡中不正常显示

    可以在选项卡中加个click事件,调用下 <a href="#tab1" >tab1</a><a href="#tab2" onc ...

  5. Codeforces Round #346 (Div. 2) D Bicycle Race

    D. Bicycle Race 题目链接http://codeforces.com/contest/659/problem/D Description Maria participates in a ...

  6. C#学习心得,记录学习

  7. Java Swing 记事本代码

    记事本代码分为4个部分: 1.顶部点击可展开的菜单如何生成?2.当点击了顶部菜单的某一个子菜单,在程序中如何判断点击了哪个子菜单?[正在写]3.那个供你输入文字并且可以滚动的文本框如何生成?4.点击了 ...

  8. css中position中的几个属性

    static  是默认值.任意 position: static; 的元素不会被特殊的定位.一个 static 元素表示它不会被"positioned",一个 position 属 ...

  9. python操作---RabbitMQ

    RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列(MQ)是一种应用程序 ...

  10. 安装webstrom,免激活长久使用

    1.在jetbrain官网下载最新版webstrom, 2.安装webstrom,不建议安装在c盘 3.安装时选择试用三十天 接下来就很重要: 首先将系统时间改到未来的某天,或者你未来写不动代码的一天 ...