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.csdn.net/je_ge/article/details/53678164
其他关联项目
- JPA 系列教程11-复合主键-2个@Id
http://blog.csdn.net/je_ge/article/details/53678085 - JPA 系列教程12-复合主键-2个@Id+@IdClass
http://blog.csdn.net/je_ge/article/details/53678164
源码地址
如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!
JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable的更多相关文章
- JPA 系列教程12-复合主键-2个@Id+@IdClass
复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id一样 Airline p ...
- JPA 系列教程11-复合主键-2个@Id
复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 CREATE TABLE `t_airline ...
- Hibernate 系列教程8-复合主键
复合主键 复合主键的意思就是2个字段同时为主键 不使用无业务含义的自增id作为主键 Airline package com.jege.hibernate.compositeid; import jav ...
- 【hibernate/JPA】注解方式实现 复合主键【spring boot】
1>hibernate/JPA实现复合主键的思路:是将所有的主键属性封装在一个主键类中,提供给需要复合主键的实体类使用. 2>主键类的几点要求: . 使用复合主键的实体类必须实现Seria ...
- JPA 系列教程9-双向一对一唯一外键
双向一对一唯一外键的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(25 ...
- SpringData JPA复合主键
上一篇博客简单介绍了SpringData JPA实现简单的CRUD,分页与多条件的排序,那里的主键类型是Long,有时我们会遇到主键不是一个的,复合主键,经过调研如下.确定一个人,不能只根据他的姓名来 ...
- springboot jpa 复合主键
https://blog.csdn.net/wyc_cs/article/details/9031991 创建一个复合主键类 public class LevelPostMultiKeysClass ...
- 【hibernate/JPA】对实体类的的多个字段建立唯一索引,达到复合主键的效果【spring boot】注解创建唯一索引和普通索引
对实体类的的多个字段建立唯一索引,达到复合主键的效果 package com.sxd.swapping.domain; import lombok.Getter; import lombok.Sett ...
- JPA 系列教程21-JPA2.0-@MapKeyColumn
@MapKeyColumn 用@JoinColumn注解和@MapKeyColumn处理一对多关系 ddl语句 CREATE TABLE `t_employee` ( `id` bigint(20) ...
随机推荐
- C#中string的小结
C#中的string类型明确定义为引用类型,但是使用时常常表现出数值型的特性.最典型的例子就是交换字符串. public static void stringexchange(string a, st ...
- 2015 Syrian Private Universities Collegiate Programming Contest 题解
题目在这里>_< 发现这场比赛在网上没有完整的题解,甚至连题目代码都没人贴出来(大概是因为题目太水了吧...).所以宝宝就来写个题解,也就当作成长记录了233333 A. Window 题 ...
- mysql安装和基本配置-redhat
1.redhat yum替换参考 url:http://blog.csdn.net/zcyhappy1314/article/details/17580943 2.yum卸载mysql rpm -qa ...
- git clone出现SSL错误
在学习git的时候,发现不能使用git clone从github.com下载,报了个ssl错误. Cloning into cancan... error: SSL certificate probl ...
- mongodb 性能提高之利用索引, 待续
> 10 , 用户无法忍受 >1s , 需要加装中提示 数据库对软件整体影响是不言而喻的, 那么使用 MOngoDB时 该如何提高数据库性能 第一: 索引, 相当于记忆法的 地点桩 1. ...
- redis安装以及远程连接
第一步下载: Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases. 第二步: 运行安装 记录安装路径 C:\Program Fil ...
- mysql 用户权限设置
windows使用的zip包的mysql.配置成功后,root用户没有权限,需要进行权限分配.管理员运行cmd,执行以下命令,进入mysql. mysql -u root -p Passwd Pass ...
- img的问题
一个div的宽高比和 里面的img的宽高比是一样的 ,div img { width:100%:height:100%;} img {border:0} img{ 设置为border:none无 ...
- 淘淘商城_day06_课堂笔记
今日大纲 实现单点登录系统 基于单点登录系统实现,用户的注册和登录 商品数据同步问题 问题 后台系统中将商品修改,前台系统没有进行数据的同步,导致前端系统不能够实时显示最新的数据. 解决 后台系统中商 ...
- AVFoundation--AVCaptureSession
// // ViewController.m // AVFountionCamera // // Created by ZhuYi on 16/5/3. // Copyright © 2016年 DD ...