联合主键的一些知识:

使用@EmbeddedId标示联合主键;

在联合主键类中只是定义确定联合主键的字段即可;
* 联合主键类的规则
* 1.必须包含一个无参的构造函数
* 2.必须实现序列化接口
* 3.必须重写hashCode和equals方法,而且equals方法的参数必须包括确定联合主键的所有字段

联合主键类的定义:

 package com.yl.demo1.bean.JointPK;

 import java.io.Serializable;

 import javax.persistence.Column;
import javax.persistence.Embeddable; /**
* 在联合主键类中只是定义确定联合主键的字段即可
* 联合主键类的规则
* 1.必须包含一个无参的构造函数
* 2.必须实现序列化接口
* 3.必须重写hashCode和equals方法,而且equals方法的参数必须包括确定联合主键的所有字段
* @author yul
*
*@Embeddable--告诉其它类,在使用AieLinePK时只是使用AieLinePK的属性作为实体的持久化属性
*/ @Embeddable //嵌入
public class AirLinePK implements Serializable {
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;
} }
 package com.yl.demo1.bean.JointPK;

 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("YL");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();//事务开始 em.persist(new AirLine("PEK", "SHA", "北京飞上海")); em.getTransaction().commit();
em.close();
factory.close();
}

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

  1. JPA联合主键

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

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

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

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

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

  4. JPA注解实现联合主键

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

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

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

  6. hibernate 注解 联合主键映射

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

  7. SQL联合主键 查重

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

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

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

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

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

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

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

随机推荐

  1. 【dapper】.net平台下的框架

    http://www.cnblogs.com/yipu/archive/2012/11/21/2780199.html Method Duration Remarks Hand coded (usin ...

  2. aaaa

    http://www.host.com http://www.host.com http://sz.weixun.com/scenery/details-3.htm http://sz.weixun. ...

  3. Critical Rendering Path

    1.生成 dom & cssom https://developers.google.com/web/fundamentals/performance/critical-rendering-p ...

  4. struts2拦截器-简单实现非法登录验证

    概念:什么是拦截器 拦截器实现了面向切面的组件,它会影响多个业务对象的公共行为封装到一个个可重用的模块,减少了系统的重复代码,实现高度内聚,确保业务对象的整洁!   为什么使用拦截器 拦截器消除了动作 ...

  5. SQL优化之索引

    最近碰到一个问题,因数据量越来越大,然后存储过程查询过慢!后来发现没有加索引列导致的!从这里让我开始慢慢去了解索引的原理及作用!以下是我的总结,个人理解只供参考: SQL SERVER提供了两种索引: ...

  6. Javascript中Array.prototype.map()详解

    map 方法会给原数组中的每个元素都按顺序调用一次 callback 函数.callback 每次执行后的返回值组合起来形成一个新数组. callback 函数只会在有值的索引上被调用:那些从来没被赋 ...

  7. 如何保护 .NET 应用的安全?

    自从 Web 应用能给访问者提供丰富的内容之后,黑客们就把目光转向任何他们能够破坏,损毁,欺骗的漏洞.通过网络浏览器提供的应用越来越多,网络罪犯们可以利用的漏洞数量也呈指数增长起来. 大多数企业都依赖 ...

  8. java核心技术记录

    Java是一种强类型的语言,这意味着必须为每一个变量声明一种类型.在java中,一共有8种基本类型,其中4种整型.2种浮点型.1种用于表示Unicode编码的字符单元的字符类型char和一种用于表示真 ...

  9. eclipse运行hadoop程序报错:Connection refused: no further information

    eclipse运行hadoop程序报错:Connection refused: no further information log4j:WARN No appenders could be foun ...

  10. 转--利用函数模板技术,写一个简单高效的 JSON 查询器

    http://www.cnblogs.com/index-html/archive/2012/07/18/js_select.html http://www.ibm.com/developerwork ...