联合主键也就是说需要多个字段才能确定数据库记录中的唯一一行。这样就需要多个字段一起,组成主键,也叫联合主键。例如飞机航线,我们需要知道飞机起飞的地点以及飞机降落的地点。所以需要飞机起飞的地点和降落的地方才能确定一条航线。AirLine表示航线,AirLinePK表示主键类。AirLinePK代码如下:

 package com.yichun.bean;

 import java.io.Serializable;

 import javax.persistence.Column;
import javax.persistence.Embeddable; /**
* 联合主键。一般使用PK 只需要定义用作主键的字段
* <p>
* 联合主键类必须遵守的JPA规范:<br>
* 1、必须要提供一个public的无参数的构造方法<br>
* 2、必须要实现序列化接口<br>
* 3、必须重写hashCode()与equals()方法
*/
// 用在实体里面,只是使用该类中的属性。(该类中的属性用在持久化的类中的字段)
@Embeddable
public class AirLinePK implements Serializable {
private String startCity;// PEK,北京 CAN广州,SHA上海
private String endCity; public AirLinePK() {
} public AirLinePK(String startCity, String endCity) {
this.startCity = startCity;
this.endCity = endCity;
} @Column(length = 3, nullable = false)
public String getStartCity() {
return startCity;
} public void setStartCity(String startCity) {
this.startCity = startCity;
} @Column(length = 3, nullable = false)
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;
} }

  AirLine 代码如下:

 package com.yichun.bean;

 import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity; @Entity
public class AirLine {
private AirLinePK id;
private String name; public AirLine() {
} public AirLine(AirLinePK id) {
this.id = id;
} public AirLine(String startCity, String endCity, String name) {
this.id = new AirLinePK(startCity, endCity);
this.name = name;
} // 用于标识该属性为实体的标识符,专门用于复合主键类
@EmbeddedId
public AirLinePK getId() {
return id;
} public void setId(AirLinePK id) {
this.id = id;
} @Column(length = 20)
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

保存数据如下:

     @Test
public void save() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("yichun");
EntityManager manager = factory.createEntityManager();
manager.getTransaction().begin(); manager.persist(new AirLine("PEK", "SHA", "北京飞上海")); manager.getTransaction().commit();
manager.close();
factory.close();
}

查找数据如下:

 @Test
public void find() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("yichun");
EntityManager manager = factory.createEntityManager(); AirLine airLine = manager.find(AirLine.class, new AirLinePK("PEK",
"SHA"));
System.out.println(airLine.getName() + " : "
+ airLine.getId().getStartCity() + " --> "
+ airLine.getId().getEndCity()); manager.close();
factory.close();
}

JPA联合主键的更多相关文章

  1. JPA联合主键@EmbeddedId使用详解附查询例子

    花了2个小时的时间解决这个问题,网上资料太少,记录下     详情看源文件TBicPrmCompute,TBicPrmComputePK package com.isoftstone.core.dom ...

  2. JPA学习---第十二节:JPA中的联合主键

    1.定义实体类,代码如下: (1).将联合主键放到一个类中,代码如下: package learn.jpa.entity; import java.io.Serializable; import ja ...

  3. JPA注解实现联合主键

    当表中一个主键不能唯一标识一条记录的时候,就需要使用联合主键了,下面是使用JPA注解实现联合主键的代码 1 首先需要建立一个复合主键类,用来存放需要生产联合主键的属性,该类需要实现序列化. packa ...

  4. Hibernate(5)—— 联合主键 、一对一关联关系映射(xml和注解) 和 领域驱动设计

    俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习!涉及的知识点总结如下: One to One 映射关系 一对一单向外键(XML/Annotation) 一对一双向外键关联(XML/A ...

  5. hibernate 注解 联合主键映射

    联合主键用Hibernate注解映射方式主要有三种: 第一.将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将 该类注解 ...

  6. SQL联合主键 查重

    2014年最后一天,今天在给数据库导入数据的时候,遇到一个问题,就是联合主键去重. 事情是这样的,现有一个表M,我想找个表中导入了许多数据,并需要将字段A(int)和B(int)联合设置为主键. 但是 ...

  7. Hibernate注解映射联合主键的三种主要方式

    今天在做项目的时候,一个中间表没有主键,所有在创建实体的时候也未加组件,结果报以下错误: org.springframework.beans.factory.BeanCreationException ...

  8. 联合主键用Hibernate注解映射的三种方式

    第一.将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将该类注解为@Embeddable,最后在主类中(该类不包含联合主 ...

  9. EntityFramework中Mapper怎么定义联合主键?

    HasKey(m => new { m.StoreId, m.CarTypeId, m.CarLevel}) 用“new {}”联合主键以“,”分隔形式定义

随机推荐

  1. iOS - BSDSocket 的使用

    1.BSDSocket 一套 unix 系统下的 socket API(纯 C). iOS 系统基于 unix,所以支持底层的 BSD Socket,在 Xcode 中可以直接使用. 2.基本使用 2 ...

  2. JMeter ----内置函数列表

    Jmeter有两种类型的函数: 用户定义的静态值(或变量) 内置函数 用户定义的静态值允许用户定义变量时被替换为静态的值测试树编译并提交运行.需要注意的是,变量目前无法嵌套,即${Var${N}}不起 ...

  3. OAF_OAF控件系列7 - Tree的实现(案列)

    2014-06-02 Created By BaoXinjian

  4. Unix环境高级编程(八)进程关系

    本章看后给人似懂非懂的感觉,主要是不知道实际当中如何去使用.通过前面几章的学习,每个进程都有一个父进程,当子进程终止时,父进程得到通知并取得子进程的退出状态.先将本章基本的知识点总结如下,日后再看时候 ...

  5. C++ 设计模式 —— 訪问者(Visitor)

    訪问者设计模式的实现借助于两个继承体系, (1)elements:一个是被操作的类(基类及其子类) (2)visitors:一个定义了一系列操作的訪问者(基类及其子类) 訪问者模式是一种行为型设计模式 ...

  6. Calendar 中getActualMaximumd 功能

    String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")) .format(new Date()); Calend ...

  7. shell教程一:字符串操作

    一:Linux shell字符串截取与拼接 假设有变量 var=http://www.linuxidc.com/123.htm 1  # 号截取,删除左边字符,保留右边字符. echo ${var#* ...

  8. 国外程序员整理的Java资源大全

    构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化 ...

  9. angular学习笔记(二十四)-$http(2)-设置http请求头

    1. angular默认的请求头: 其中,Accept 和 X-Requested-With是$http自带的默认配置 Accept:application/json,text/plain       ...

  10. 使用自定义的按钮替换默认的<input type='file'>

    可以通过让默认的input type = 'file'按钮透明度变为0,并且让它刚好覆盖在自定义的按钮上,来实现此效果: 将它写成一个jQuery插件: (function($){ $.fn.brow ...