复合主键

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

需求产生

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

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.csdn.net/je_ge/article/details/53678164

其他关联项目

源码地址

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

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



JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. SpringData JPA复合主键

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

  7. springboot jpa 复合主键

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

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

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

  9. JPA 系列教程21-JPA2.0-@MapKeyColumn

    @MapKeyColumn 用@JoinColumn注解和@MapKeyColumn处理一对多关系 ddl语句 CREATE TABLE `t_employee` ( `id` bigint(20) ...

随机推荐

  1. 从excel读数据到informix的Found a quote for which there is no matching quote错误

    我从excel读取数据,然后存储到Informix数据库里.偶尔会发现出现Found a quote for which there is no matching quote这个错误.调试后发现,是因 ...

  2. BFS - leetcode [宽度优先遍历]

    127. Word Ladder int size = q.size(); for(int k = 0; k < size; k++){//for 次数 找到一个erase一个 q里面加入的是所 ...

  3. Scala 类和对象

    Scala class: Scala 源文件中可以有很多类(class),这些类默认都是Public的,public是Scala的默认访问级别.在Scala中,声明一个未用priavate修饰的字段 ...

  4. 多线程随笔二(Task)

    Task类是.net 4.0新加进来的特性,对原有的Thread,ThreadPool做了进一步的封装,使得.net平台上的多线程编程变得更加方便.废话不多说,进入正题. 一. Task启动 Task ...

  5. 使用Image.GetThumbnailImage 方法返回缩略图

    如果 Image 包含一个嵌入式缩略图像,则此方法会检索嵌入式缩略图,并将其缩放为所需大小. 如果 Image 不包含嵌入式缩略图像,此方法会通过缩放主图像创建一个缩略图像. 请求的缩略图像大小为 1 ...

  6. 关于Windows Boot Manager、Bootmgfw.efi、Bootx64.efi、bcdboot.exe 的详解

    1. http://bbs.wuyou.com/forum.php?mod=viewthread&tid=303679&fromuid=396698

  7. Javascript Date 判断输入日期是否正确

    JavaScript的Date对象有容错性,可将随意给定的日期的年月日自动生成正确的日期时间 //JavaScript中Date对象容错性 function dateCheck(){ var date ...

  8. lucene 总结收集(url)

    1.倒排索引结构 2.lucene自定义评分域 3.Lucene系列-FieldCache 4.Lucene系列-facet | IT瘾 5.lucene4.7 之排序 6.lucene排序---相关 ...

  9. HTML5入门总结 HTML5API

    w3cshools  MDN英文  MDN中文 HTML5 HTML5 is the latest evolution of the standard that defines HTML. The t ...

  10. mysql for windows zip版安装

    1.将mysql_5.6.24_winx64.zip 解压到文件夹 2.增加环境变量 3.修改mysql配置文件 将mysql根目录下的my-default.ini 复制一份更名为 my.ini.修改 ...