有两种方法来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. JavaWeb之cookie

    什么叫做会话 ? 用户从打开一个浏览器开始,浏览器网站,到关闭浏览器的整个过程叫做一次会话! 每个用户与服务器进行交互的过程中,各自会有一些数据,程序要想办法保存每个用户的数据. 例如:用户点击超链接 ...

  2. JS弹出框

    一.JS三种最常见的对话框 1.alert()警告框      alert是警告框,只有一个按钮"确定"无返回值,警告框经常用于确保用户可以得到某些信息.当警告框出现后,用户需要点 ...

  3. alert 和 console.log的区别

    出走半月,一直以为 console.log 和 alert 的用法是一样的,只是表现的形式不同,alert 是以弹框的形式出现,console.log 是在后台打印输出. 但是今天在写东西的时候,发现 ...

  4. CSS布局之-水平垂直居中

    对一个元素水平垂直居中,在我们的工作中是会经常遇到的,也是CSS布局中很重要的一部分,本文就来讲讲CSS水平垂直居中的一些方法.另外,文中的css都是用less书写的,如果看不懂less,可以把我给的 ...

  5. hexo工具介绍及使用方法

    Hexo is a fast, simple & powerful blog framework 安装方法:npm install hexo-cli -g; require:node.js g ...

  6. 监听器的小示例:利用HttpSessionListener和HttpServletContextListener实现定时销毁HttpSession

    1.创建MyServletContextListener实现HttpServletContextListener接口 @Override public void contextDestroyed(Se ...

  7. C#图解教程-方法参数笔记(上)

    一晃大学四年要过去了,期间乱点了很多技能点, 导致每一项技能都只是处于入门阶段.为了将C#作为我的主要技能,准备恶补相关姿势(知识),通过各种技术论坛的推荐,找到了<C#图解教程>这本书. ...

  8. ReentrantLock 以及 AQS 实现原理

    什么是可重入锁?       ReentrantLock是可重入锁,什么是可重入锁呢?可重入锁就是当前持有该锁的线程能够多次获取该锁,无需等待.可重入锁是如何实现的呢?这要从ReentrantLock ...

  9. Unity粒子系统

    最近比较清闲,就重新看了一遍例子系统,感觉能把效果做的炫酷对于初学者来说并不是一件容易的事,但是回头想想,最重要的原因可能还是没有把Particle Systems组件研究透吧,温故而知新,一起复习一 ...

  10. JS绑定种类汇总

    这里是<你不知道的JS>中常见的this绑定种类分享: 1)默认绑定: function foo(){ console.log(this.a); } var a = 2; foo(); 解 ...