JPA 系列教程11-复合主键-2个@Id
复合主键
指多个主键联合形成一个主键组合
需求产生
比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示
ddl语句
CREATE TABLE `t_airline` (
`startCity` varchar(3) NOT NULL,
`endCity` varchar(3) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`startCity`,`endCity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
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.Table;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:复合主键-2个@Id
*/
@Entity
@Table(name = "t_airline")
public class Airline implements Serializable {
private static final long serialVersionUID = -906357110051689484L;
@Id
@Column(length = 3)
private String startCity;
@Id
@Column(length = 3)
private String endCity;
private String 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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Airline [startCity=" + startCity + ", endCity=" + endCity + ", name=" + name + "]";
}
}
MainTest
package com.jege.jpa.composite;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceException;
import org.junit.Test;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:复合主键-2个@Id测试
*/
public class MainTest {
@Test
public void crud() {
EntityManagerFactory entityManagerFactory = null;
EntityManager entityManager = null;
try {
entityManagerFactory = Persistence.createEntityManagerFactory("com.jege.jpa");
entityManager = entityManagerFactory.createEntityManager();
Airline airline = new Airline();
airline.setStartCity("PEK");
airline.setEndCity("SHA");
airline.setName("bj to sh");// 新建状态
entityManager.getTransaction().begin();
entityManager.persist(airline);// 托管状态
airline.setName("北京飞上海");
entityManager.getTransaction().commit();
Airline airline1 = new Airline();
airline1.setStartCity("PEK");
airline1.setEndCity("SHA");
// hibernate:瞬时状态,持久状态(托管),游离(脱管)状态
airline1 = entityManager.find(Airline.class, airline1);
System.out.println("name:" + airline1.getName());
entityManager.getTransaction().begin();
entityManager.remove(airline1);
entityManager.getTransaction().commit();
// airline1变为删除状态
entityManager.getTransaction().begin();
entityManager.persist(airline1);
entityManager.getTransaction().commit();
} catch (PersistenceException e) {
e.printStackTrace();
entityManager.getTransaction().rollback();
throw e;
} finally {
if (entityManager != null && entityManager.isOpen())
entityManager.close();// 游离状态
if (entityManagerFactory != null && entityManagerFactory.isOpen())
entityManagerFactory.close();
}
}
}
源码地址
如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!
JPA 系列教程11-复合主键-2个@Id的更多相关文章
- JPA 系列教程12-复合主键-2个@Id+@IdClass
复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id一样 Airline p ...
- JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable
复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id和复合主键-2个@Id+ ...
- Hibernate 系列教程8-复合主键
复合主键 复合主键的意思就是2个字段同时为主键 不使用无业务含义的自增id作为主键 Airline package com.jege.hibernate.compositeid; import jav ...
- 【hibernate/JPA】注解方式实现 复合主键【spring boot】
1>hibernate/JPA实现复合主键的思路:是将所有的主键属性封装在一个主键类中,提供给需要复合主键的实体类使用. 2>主键类的几点要求: . 使用复合主键的实体类必须实现Seria ...
- Hibernate复合主键映射
目录: 1. 实现方式一:将复合主键对应的属性与实体其他普通属性放在一起 2. 实现方式二:将主键属性提取到一个主键类中,实体类只需包含主键类的一个引用 在日常开发中会遇到这样一种情况,数据库中的某张 ...
- 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 ...
随机推荐
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- edgerouter bonding
configure set interfaces bonding bond0 mode 802.3ad set interfaces ethernet eth1 bond-group bond0 se ...
- sudo密码错误的解决办法
按一下Caps Lock键,如果大写灯亮了,再按一下. 然后重新输入sudo密码,尝试.
- python 学习 设计模式(goF设计模式)
一 单例模式 用来创建单个实例 #/usr/bin/env python3 # -*- coding:utf-8 -*- # Author: ZSHAOX class Foo: instance = ...
- Font-Awesome IIS下不显示问题
解决方法: 在IIS设置中添加一个IMME fileExtension: .woff mimeType:application/x-font-woff
- ie6的png24问题
解决IE6的PNG透明JS插件 DD_belatedPNG 引:http://www.cnblogs.com/cobby/archive/2012/05/11/2495801.html IE6的PNG ...
- eclipse 发布web工程,修改tomcat端口
如果想修改tomcat发布的端口,有两种方法: 1.用记事本打开tomcat安装目录下的conf文件夹下的server.xml,找到<Connector port="8080" ...
- 绳关节(b2RopeJoint)
package{ import Box2D.Collision.b2AABB; import Box2D.Collision.b2RayCastInput; import Box2D.Collisio ...
- BCTF Web Code–考脑洞,你能过么?
BCTF Web Code–考脑洞,你能过么? 1)打开链接,是一张图片 根据URL特点推断可能是有文件包含漏洞 2) 将jpg参数修改成index.php,查看源代码,发现base64编码后的代码 ...
- python之~ 序列化与反序列化
sy1.proto文件 syntax = "proto2"; message stuff { required int32 stuff_ID = ; required ; opti ...