有两种方法来map composite key. 第一种用@IdClass第二种用@Embeddable,参考链接: http://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate

一. @IdClass方法

1. Bean

package locationService.beans;

import java.io.Serializable;

import javax.persistence.*;

@Entity
@Table(name = "entity_to_entity")
@IdClass(EntityToEntityPk.class)
public class EntityToEntity implements Serializable { private static final long serialVersionUID = 11L;
private int parentId;
private int childId; @Id
@Column(name="parent_id")
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
} @Id
@Column(name="child_id")
public int getChildId() {
return childId;
}
public void setChildId(int childId) {
this.childId = childId;
} @Override
public String toString() {
return "parentId is " + parentId + ", childId is " + childId;
} } class EntityToEntityPk implements Serializable {
private static final long serialVersionUID = 12L;
protected Integer parentId;
protected Integer childId; public EntityToEntityPk() {} public EntityToEntityPk(Integer parentId, Integer childId) {
this.parentId = parentId;
this.childId = childId;
} public Integer getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
} public Integer getChildId() {
return childId;
}
public void setChildId(int childId) {
this.childId = childId;
} @Override
public boolean equals(Object o) {
if(o instanceof EntityToEntityPk){
EntityToEntityPk other = (EntityToEntityPk) o;
return parentId.equals(other.getParentId()) && childId.equals(other.getChildId());
} return false;
} @Override
public int hashCode() {
return parentId.hashCode() + childId.hashCode();
} }

  

注意equals和hasCode方法是composite key必须要有的

A composite id must implement both methods or the id will not work properly. Hibernate must be able to compare ids. A composite id will most likely use all its id fields in the equals and hashCode methods.

An entity does not need to implement equals and hashCode under some conditions. The persistence context guaranties that an entity with a given id exists only once. You cannot let two objects with the same id become persistent. Assuming that a class Country uses the isocode as ID, the following code will cause a non unique object exception.

参考链接: http://www.laliluna.de/jpa-hibernate-guide/ch06s06.html

2. Unit Test

  @Test
public void testEnityToEntity() {
Session session = Config.getSessionFactory().openSession();
session.beginTransaction(); Query query = session.createQuery("select t.parentId from EntityToEntity t"); @SuppressWarnings("unchecked")
List<Object> entities = query.list();
assertEquals(entities.get(0), 1); session.getTransaction().commit();
session.close();
}

Hibernate composite key的更多相关文章

  1. [转]Support Composite Key in ASP.NET Web API OData

    本文转自:https://code.msdn.microsoft.com/Support-Composite-Key-in-d1d53161 he default EntitySetControlle ...

  2. hibernate映射文件set key one-to-many

    转自:https://www.linuxidc.com/Linux/2013-11/92228.htm 1 hibernate映射文件set key one-to-many的配置. POJOs如下: ...

  3. HIBERNATE - 符合Java习惯的关系数据库持久化(精华篇)

    HIBERNATE - 符合Java习惯的关系数据库持久化      下一页 HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.0.4   目录 前言 1. ...

  4. eclipse中hibernate和mybatis中xml配置文件的没有标签提醒解决方法

    当我们使用eclipse编写Mybatis或hibernate的xml文件时,面对众多标签的配置文件,却没有自动提醒,对于工作和学习都十分不方便. 之所以没有自动提醒,是因为dtd文件没有加载成功. ...

  5. hibernate学习(一)配置,导包

    框架的作用 学过javaWeb基础的已经对web层 jsp  servlet   ,service  层  ,dao层的jdbc .DBUtils 有了很深的了解 并编写代码实现某种功能 为了提高开发 ...

  6. 5 -- Hibernate的基本用法 --5 3 改变持久对象状态的方法

    1. 持久化实体 Serializable save(Object obj) : 将obj对象变为持久化状态,该对象的属性将被保存到数据库. void persist(Object obj) : 将o ...

  7. Hibernate的dtd文件和properties文件

    hibernate-configuration-3.0.dtd <!-- Hibernate file-based configuration document. <!DOCTYPE hi ...

  8. 关于Hibernate的Dialect

    org.hibernate HibernateException Dialect must be explicitly set :***  使用Hibernate,有时候会遇到类似上面的异常.  使用 ...

  9. Hibernate的集合映射(Set、List、Array、Map、Bag)

    POJOs如下: Customer类------>customer表   Order类对应---------->orders表  customer(1)<-------------- ...

随机推荐

  1. 鼠标滚动:mousewheel事件在Firefox采用DOMMouseScroll事件的统一处理

    这是一个小事件,但当下的WEB应用交互非常丰富,判断鼠标的滚动来执行相应的操作是比较常见的.我用Chrome/IE/Firefox/Opera 4种浏览器做测试,发现只有firefox的处理方法有很大 ...

  2. 01 The Learning Problem

    什么时候适合用机器学习算法? 1.存在某种规则/模式,能够使性能提升,比如准确率: 2.这种规则难以程序化定义,人难以给出准确定义: 3.存在能够反映这种规则的资料. 所以,机器学习就是设计算法A,从 ...

  3. Win7+CentOS双系统(二)

    在之前的文章中我们实现了Win7+CentOS6.3双系统的安装和使用,不过比较不幸的是在CentOS6.4版本时其安装文件大小已经超过了FAT文件系统所能容纳的单个文件大小4G.我们使用FAT文件系 ...

  4. ionic打包项目,运行时报错A problem occurred configuring root project 'android'。。。

    运行报错的原因是sdk没有下载完整 解决办法: 1,打开sdk manage.分别下载android support repository.Google play services.google re ...

  5. eclipse中AndroidA工程依赖B工程设置

    假设library为B工程,而SlideMenuTest为A工程,且SlideMenuTest需要依赖library工程(减少jar包形式的修改麻烦). 需要简单的设置即可. 1.B工程设置为libr ...

  6. 浅谈PHP+Access数据库的连接 注意要点

    今天公司需要用php连接access 数据库,结果整了半天Access数据库 就是连接不上,查找 很多资料,以下是我的经验, -.- 希望能给需要连接access 数据的人带来帮助..-.- 需要注意 ...

  7. mysql 关联查询 索引不起作用原因记录

    业务逻辑如下:查询某篇文章的评论列表,且列出评论人及被评论人的昵称.头像. 先看一下表结构 评论表: 评论表的索引: 用户表: 用户表的索引: 查询语句如下: SELECT t1.comment_id ...

  8. web.xml 中配置了error-page但不起作用问题

    问题: 在web.xml 中配置了 error-page,但是好像不起作用,就是跳转不到指定的页面. 配置信息如下: <!-- 400错误 --> <error-page> & ...

  9. 解决 MySQL 分页数据错乱重复

    前言 一天,小明兴匆匆的在通讯工具上说:这边线上出现了个奇怪的问题,麻烦 DBA 大大鉴定下,执行语句 select xx from table_name wheere xxx order by 字段 ...

  10. 一个栗子上手CSS3动画

    最近杂七杂八的事情很多,很多知识都没来得及总结,是时候总结总结,开启新的篇章- 本篇文章不一一列举CSS3动画的属性,若需要了解API,可前往MDN 在开始栗子前,我们先补补基础知识. css3动画分 ...