Hibernate composite key
有两种方法来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的更多相关文章
- [转]Support Composite Key in ASP.NET Web API OData
本文转自:https://code.msdn.microsoft.com/Support-Composite-Key-in-d1d53161 he default EntitySetControlle ...
- hibernate映射文件set key one-to-many
转自:https://www.linuxidc.com/Linux/2013-11/92228.htm 1 hibernate映射文件set key one-to-many的配置. POJOs如下: ...
- HIBERNATE - 符合Java习惯的关系数据库持久化(精华篇)
HIBERNATE - 符合Java习惯的关系数据库持久化 下一页 HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.0.4 目录 前言 1. ...
- eclipse中hibernate和mybatis中xml配置文件的没有标签提醒解决方法
当我们使用eclipse编写Mybatis或hibernate的xml文件时,面对众多标签的配置文件,却没有自动提醒,对于工作和学习都十分不方便. 之所以没有自动提醒,是因为dtd文件没有加载成功. ...
- hibernate学习(一)配置,导包
框架的作用 学过javaWeb基础的已经对web层 jsp servlet ,service 层 ,dao层的jdbc .DBUtils 有了很深的了解 并编写代码实现某种功能 为了提高开发 ...
- 5 -- Hibernate的基本用法 --5 3 改变持久对象状态的方法
1. 持久化实体 Serializable save(Object obj) : 将obj对象变为持久化状态,该对象的属性将被保存到数据库. void persist(Object obj) : 将o ...
- Hibernate的dtd文件和properties文件
hibernate-configuration-3.0.dtd <!-- Hibernate file-based configuration document. <!DOCTYPE hi ...
- 关于Hibernate的Dialect
org.hibernate HibernateException Dialect must be explicitly set :*** 使用Hibernate,有时候会遇到类似上面的异常. 使用 ...
- Hibernate的集合映射(Set、List、Array、Map、Bag)
POJOs如下: Customer类------>customer表 Order类对应---------->orders表 customer(1)<-------------- ...
随机推荐
- Android线程池使用终结版
有一段时间没写博文了,今天抽空总结一下,也希望能通过自己写的这些文章,加深理解的同时能帮 助在技术方面有疑点的朋友搞清楚个所以然来,由于经常会在网上或群里看到有朋友会问线程方面的 东西,就像我一个朋友 ...
- Vue.js 学习笔记 一
本文的Demo和源代码已放到GitHub,如果您觉得本篇内容不错,请点个赞,或在GitHub上加个星星! https://github.com/zwl-jasmine95/Vue_test 以下所有知 ...
- JavaScript中数组类型的属性和方法
除了Object,Array类型应该是ECMAScript中最常用的类型了. ECMAScript的数组虽然也是数据的有序列表,但还是与其他语言中的数组有很大的区别.比如ECMAScript数组每一项 ...
- mysql 免安装版 + sqlyog 安装 步骤 --- 发的有点晚
总有些朋友不会安装mysql,其实软件安装不是学习mysql的重点,基本上也就安装一次,工作后一般公司里也不会让你安装,如果非要安装,百度一下就行了.安装版本百度上有许多,下面就提供一个免安装版的步骤 ...
- mac下安装Java开发环境
1.安装JDK 打开网页,进入jdk官网下:http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载后,进入finde ...
- Elasticsearch搜索之most_fields分析
顾名思义,most_field就是匹配词干的字段数越多,分数越高,也可设置权重boost. 下面是简易公式(详细评分算法请参考:http://m.blog.csdn.net/article/detai ...
- C#数据结构之串
串(string)是n(n>=0)个字符组成的有限序列. 由于串中的字符都是连续存储的,在C#中有恒定不变的特性.一经创建就保持不变. 为了区别C#中的string,因此以stringDS类模拟 ...
- 【算法系列学习】HDU 5527 Too Rich贪心
http://www.cnblogs.com/AOQNRMGYXLMV/p/4934747.html #include<iostream> #include<cstdio> # ...
- 蓝桥杯-分小组-java
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...
- 安装配置sentry服务
环境 系统环境:Centos6.7 Hadoop版本:CDH5.10 jdk版本:jdk7 注:本文并未集成kerberos组件 安装Sentry Server 选择安装hive的节点进行安装测试: ...