JPA学习---第十二节:JPA中的联合主键
1、定义实体类,代码如下:
(1)、将联合主键放到一个类中,代码如下:
package learn.jpa.entity; import java.io.Serializable; import javax.persistence.Column;
import javax.persistence.Embeddable; /**
*
* 1、必须要有无擦得构造函数
* 2、必须要实现序列接口
* 3、必须重写 equals() 和 hashCode() 方法
* @Embeddable 告诉 jpa 只是使用实体类里面的属性
*/
@Embeddable
public class AirLinePK implements Serializable{ /**
*
*/
private static final long serialVersionUID = 1L;
private String startCity;
private String endCity; public AirLinePK() { } public AirLinePK(String startCity, String endCity) {
this.startCity = startCity;
this.endCity = endCity;
} @Column(length=3)
public String getStartCity() {
return startCity;
}
public void setStartCity(String startCity) {
this.startCity = startCity;
}
@Column(length=3)
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;
} }(2)、定义 AirLine 实体类,代码如下:
package learn.jpa.entity; 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 用于标注为实体的标识符
@EmbeddedId
public AirLinePK getId() {
return id;
}
public void setId(AirLinePK id) {
this.id = id;
}
@Column(length=30)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2、测试类,代码:
package learn.jpa.test; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence; import learn.jpa.entity.AirLine; import org.junit.Test; public class AirLineTest { /**
* 测试数据库是否可以生成表
*/
@Test
public void test() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa");
factory.close();
} @Test
public void save(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin(); // 开启事务
em.persist(new AirLine("PEK","SHA","北京飞上海"));
em.getTransaction().commit();
em.close();
factory.close();
}
}
注意:
1、必须实现Serializable序列化
2、必须提示无参的构造方法
3、必须重写hashCode和equals方法
@Embeddable 表示该类中所有属性在应用该联合主键的类中作为它的属性(字段)
JPA学习---第十二节:JPA中的联合主键的更多相关文章
- SQL Server中的联合主键、聚集索引、非聚集索引、mysql 联合索引
我们都知道在一个表中当需要2列以上才能确定记录的唯一性的时候,就需要用到联合主键,当建立联合主键以后,在查询数据的时候性能就会有很大的提升,不过并不是对联合主键的任何列单独查询的时候性能都会提升,但我 ...
- SQL Server中的联合主键、聚集索引、非聚集索引
我们都知道在一个表中当需要2列以上才能确定记录的唯一性的时候,就需要用到联合主键,当建立联合主键以后,在查询数据的时候性能就会有很大的提升,不过并不是对联合主键的任何列单独查询的时候性能都会提升,但我 ...
- SQL Server(MySql)中的联合主键(联合索引) 索引分析
最近有人问到这个问题,之前也一直没有深究联合索引具体使用逻辑,查阅多篇文章,并经过测试,得出一些结论 测试环境:SQL Server 2008 R2 测试结果与MySql联合索引查询机制类似,可以认为 ...
- Mysql 创建联合主键
Mysql 创建联合主键2008年01月11日 星期五 下午 5:21使用primary key (fieldlist) 比如: create table mytable ( ...
- Java进阶知识05 Hibernate联合主键之Annotation(注解)和XML实现方式
1.Hibernate联合主键(Annotation实现) 1.1.单列主键 1.1.1.为什么要有主键? //唯一确定一条记录 1.1.2.一个表能否有多个主键? //不能 1.1.3. ...
- JPA联合主键
联合主键也就是说需要多个字段才能确定数据库记录中的唯一一行.这样就需要多个字段一起,组成主键,也叫联合主键.例如飞机航线,我们需要知道飞机起飞的地点以及飞机降落的地点.所以需要飞机起飞的地点和降落的地 ...
- JPA注解实现联合主键
当表中一个主键不能唯一标识一条记录的时候,就需要使用联合主键了,下面是使用JPA注解实现联合主键的代码 1 首先需要建立一个复合主键类,用来存放需要生产联合主键的属性,该类需要实现序列化. packa ...
- JPA联合主键@EmbeddedId使用详解附查询例子
花了2个小时的时间解决这个问题,网上资料太少,记录下 详情看源文件TBicPrmCompute,TBicPrmComputePK package com.isoftstone.core.dom ...
- EntityFramework中Mapper怎么定义联合主键?
HasKey(m => new { m.StoreId, m.CarTypeId, m.CarLevel}) 用“new {}”联合主键以“,”分隔形式定义
随机推荐
- Xshell常用命令备忘
(1)命令ls——列出文件ls 显示当前目录文件ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的隐藏”文件ls a* 列出当前目录下以字母a开头的所有文件ls -l *.doc 给出 ...
- dnw-linux的安装及使用
<Tiny6410刷机指南>介绍了如何用USB线下载uboot,kernel,filesystem到开发板的nand flash,USB下载文件用到工具dnw.遗憾的是该教程提供的是win ...
- JS匿名函数自执行函数
JS匿名函数自执行函数:(function(){})();(function(){}) 这是一个函数,函数后面接(),则是调用函数 比如(function(arg){console.log(arg); ...
- C#虚方法和覆写方法
- 导航控制器(UINavigationController)
一.导航控制器基础 1.导航控制器的组成 i.中间视图 ii.导航栏 iii.工具栏 2.配置一个导航界面 a.配置一个导航界面最重要的部分就是配置被包含的视图控制器. b.当它所属的视图控制器在导航 ...
- C#中的快捷键,可以更方便的编写代码 (转载)
C#中的快捷键,可以更方便的编写代码 CTRL + SHIFT + B 生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O 打开项目 CTRL + ...
- [译]Memory Reordering Caught in the Act
原文:http://preshing.com/20120515/memory-reordering-caught-in-the-act/ 编写lock-free的C/C++程序时,在保证memory ...
- yii2解析非x-www-form-urlencoded类型的请求数据(json,xml)
组件配置添加: 'request' => [ 'parsers' => [ 'application/json' => 'yii\web\JsonParser', 'applicat ...
- (转)Android系统自带Activity样式(@android:style/)
在AndroidManifest.xml文件的activity中配置 1.android:theme="@android:style/Theme" 默认状态,即如果theme这里不 ...
- MongoDB 学习笔记(三)—— 修改器的使用
通常文档只会有一部分数据要更新,所以使用修改器来操作文档极为高效. 小技巧:了解函数功能,不带括号即可.如:db.blog.update即可查看update函数的具体参数和方法体. $set修改器 & ...