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 ...
随机推荐
- javabean解决jsp中中文乱码问题
问题描述:useBean行为定义了Java Bean对象(Person类包括姓名[string],年龄[int]),使用html页面向JSP页面提交数据,JSP页面中使用Java Bean行为来处理提 ...
- invalid receiver type
Because in a case like this: type I int type P *I func (i I) Get() int { return int(i) } func (p P) ...
- SQL LIKE语句多条件贪婪匹配算法
在CMS开发中,经常会有类似这样的需求: 提问——回答模式,最经典的例子就是百度提问. 提问者提出问题,由其他人回答,其他人可以是用户,也可以是服务商. 在这个模式中,如何充分利用历史数据是最关键的技 ...
- use ContourPlot-使用ContourPlot
use ContourPlot to draw implicit function graphics 使用ContourPlot 画隐函数图像 for example $x^{3}+y^{3}-3xy ...
- datatables.js 里面遇到的问题
1. 假如需要A行的data 和 B行的data 合并 在B行 data:name 在A行的 render:function(){ return data+full.name 此时返回的就是A+B ...
- ExtJS从入门到后面肯定要抛弃
一.ExtJs定义 ①基于JavaScript语言 ②基于JavaSwing的MVC架构 ③支持组件化.模块化设计 ④提供“本地数据源”的支持 ⑤完完善与服务端的交互机制 ⑥是最有可能拥有大规模可视化 ...
- gitlab自动备份
1.周期性计划任务: $crontab -e 0 0 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create $service crond res ...
- nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
环境:Centos6.5 行为:安装nginx 问题: nginx: [emerg] socket() [::]: failed (: Address family not supported by ...
- 永久修改python默认的字符编码为utf-8
这个修改说来简单,其实不同的系统,修改起来还真不一样.下面来罗列下3中情况 首先所有修改的动作都是要创建一个叫 sitecustomize.py的文件,为什么要创建这个文件呢,是因为python在启动 ...
- [妙味JS基础]第三课:自定义属性、索引值
知识点总结 自定义属性 元素.自定义属性 = 值: 比如: oDiv.abc = 100; =>abc为自定义属性 索引值 index =>也是自定义属性 oDiv.index = '' ...