复合主键

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

需求产生

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

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. 一个简单sql注入的poc

    最近在提高自己编程能力,拿一些实用的小工具练下.该脚本为python语言,主要涉及模块urllib,re. 功能:验证CmsEasy5.5爆账号密码 实验用源码:http://pan.baidu.co ...

  2. LeetCode 385. Mini Parse

    Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...

  3. Grunt使用教程(限winows)

    前提:安装nodejs 一. 打开dos命令窗口,输入命令 "node -v",确认nodejs安装成功 二. 其次,安装grunt-cli (该插件是grunt命令行插件),执行 ...

  4. centos 安装cacti监控

    CentOS 6下Cacti搭建文档 安装依赖关系 yum -y install mysql-devel httpd php php-pdo php-snmp php-mysql lm_sensors ...

  5. java poi 导出Excel文件

    1,导包  poi-3.9-XXX.JAR 2, 创建一个实体对象 public class Student implements Serializable { /** * */ private st ...

  6. iOS TableView的分割线

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [self.tableView setSeparato ...

  7. Linux MySQL5.5源码安装

    环境:CentOS7,MySQL5.5 1.MySQL5.5源码下载 Oracle的网站打开较慢,http://mirrors.sohu.com/mysql/这里提供了MySQL的镜像.一般的,Lin ...

  8. python 基础学习4-with语句

    why use With? 有些事情需要事先进行设置,事后进行处理,with语句提供了一个很好的处理方式,例如文件读写处理,有时候可能忘记关闭文件,with可以很好地处理这种现象. with语句用来简 ...

  9. electron的艰难安装之旅

    最近对前端开发很感兴趣,抽空研究了下前段的开发工具,发现比较流行的是sublime,atom,vscode等, 由于一直以来从事.net开发所以对vscode很感兴趣,在vscode的安装配置过程偶然 ...

  10. Users is not mapped(Hibernate实体类采用注解)

    今天做简单的登陆验证web应用时,用HQL语句查询数据表时 总是出现Users is not mapped [from Users u where u.username=? and u.passwor ...