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 {}”联合主键以“,”分隔形式定义
随机推荐
- leetcode 19
最开始用一般的方法,首先遍历链表求出长度,进而求出需要删除节点的位置,最后进行节点的删除. 代码如下: /** * Definition for singly-linked list. * struc ...
- CICS中设置程序常驻内存
CICS中设置程序常驻内存 Permanent=no 修改为Permanent=yse --------------------- 对CICS的参数进行调节,RD中
- JavaScript的匿名函数和模块化的使用方法
对于开发人员来说,很多时候我们都会涉及到JavaScript的使用,而在使用过程中,最令人沮丧的就是变量没有相应的使用范围. 在开发中,对于任何变量.数组.函数.对象等,只要不在函数的内部,都会被默认 ...
- C puzzles详解【13-15题】
第十三题 int CountBits(unsigned int x) { ; while(x) { count++; x = x&(x-); } return count; } 知识点讲解 位 ...
- POJ C程序设计进阶 编程题#3 : 排队游戏
编程题#3:排队游戏 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 在幼儿 ...
- 数据包判断是否丢包 ping+tracert+mtr
1.用咱们最常用的Ping命令来查看是不是真的丢包了 这里可以看到数据包发送了4个,返回了4个,丢失=0 证明没有丢失 也有可能中间路由做了策略不给ICMP的回应 这样就ping没法判断了 正常情 ...
- silverlight 退出当前页面、跳转到主页面
1.退出当前页面 private void imgExit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (Message ...
- canvas绘制文字
绘制字体时可以使用fillText方法或者strokeText方法. fillText方法用填充的方式来绘制字符串 context.fillText (text, x,y,[maxwidth]); s ...
- luigi学习3-使用luigid
--local-scheduler的方式只适用于开发调试阶段,当你真正要把程序部署到一个产品时,我们推荐使用luigid服务. 使用luigid服务不但能提供锁服务(防止一个任务被多个进程重复执行), ...
- 配置NTP服务ntpd/ntp.conf(搭建Hadoop集群可参考)
本文拟定是在一个局域网内(比如一个Hadoop集群)设定一台NTP服务器作为整个网络的标准时间参考,使用网络(集群)内的所有机器保持时间一致!以下是详细的操作步骤: 1. 修改选定的服务器的本地时间 ...