@MapKeyColumn

用@JoinColumn注解和@MapKeyColumn处理一对多关系

ddl语句

CREATE TABLE `t_employee` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; CREATE TABLE `t_employee_map` (
`Employee_id` bigint(20) NOT NULL,
`emp_value` varchar(255) DEFAULT NULL,
`emp_key` varchar(255) NOT NULL,
PRIMARY KEY (`Employee_id`,`emp_key`),
CONSTRAINT `FK_k06nikcsc4pc9oasboix6uagw` FOREIGN KEY (`Employee_id`) REFERENCES `t_employee` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Employee

package com.jege.jpa;

import java.util.HashMap;
import java.util.Map; import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:pojo模型
*/
@Entity
@Table(name = "t_employee")
public class Employee {
@Id
@GeneratedValue
private Long id;
private String name;
@ElementCollection
// 生成的表的主键Map.key+EmployeeMap_id
@CollectionTable(name = "t_employee_map")
@MapKeyColumn(name = "emp_key")
@Column(name = "emp_value")
private Map<String, String> others = new HashMap<String, String>(); public Employee() { } public Employee(String name) {
this.name = name;
} public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Map<String, String> getOthers() {
return others;
} public void setOthers(Map<String, String> others) {
this.others = others;
} }

JPA2Test

package com.jege.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; public class JPA2Test { private static EntityManagerFactory entityManagerFactory = null;
private EntityManager entityManager = null; @BeforeClass
public static void setUpBeforeClass() throws Exception {
entityManagerFactory = Persistence.createEntityManagerFactory("com.jege.jpa");
} @Before
public void setUp() throws Exception {
entityManager = entityManagerFactory.createEntityManager();// Session
} @Test
public void persist() throws Exception {
Employee employee = new Employee();
employee.setName("je-ge"); employee.getOthers().put("home", "beijing");
employee.getOthers().put("work", "shanghai");
entityManager.getTransaction().begin();
entityManager.persist(employee);
entityManager.getTransaction().commit();
} @Test
public void find() throws Exception {
persist();
entityManager.clear(); Employee employee = entityManager.find(Employee.class, 1L);
System.out.println(employee.getName());
System.out.println(employee.getOthers());
} @After
public void tearDown() throws Exception {
if (entityManager != null && entityManager.isOpen())
entityManager.close();
} @AfterClass
public static void tearDownAfterClass() throws Exception {
if (entityManagerFactory != null && entityManagerFactory.isOpen())
entityManagerFactory.close();
} }

其他关联项目

源码地址

https://github.com/je-ge/jpa

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。您的支持将鼓励我继续创作!谢谢!



JPA 系列教程21-JPA2.0-@MapKeyColumn的更多相关文章

  1. JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable

    复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id和复合主键-2个@Id+ ...

  2. JPA 系列教程12-复合主键-2个@Id+@IdClass

    复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id一样 Airline p ...

  3. JPA 系列教程5-双向一对多

    双向一对多的ddl语句 同单向多对一,单向一对多表的ddl语句一致 Product package com.jege.jpa.one2many; import javax.persistence.En ...

  4. JPA 系列教程4-单向一对多

    JPA中的@OneToMany @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface OneToMany { Class tar ...

  5. JPA 系列教程17-继承-独立表-TABLE_PER_CLASS

    PerTable策略 每个具体的类一个表的策略 举例 这种映射策略每个类都会映射成一个单独的表,类的所有属性,包括继承的属性都会映射成表的列. 这种映射策略的缺点是:对多态关系的支持有限,当查询涉及到 ...

  6. JPA 系列教程16-继承-联合子类-JOINED

    联合子类策略 这种情况下子类的字段被映射到各自的表中,这些字段包括父类中的字段,并执行一个join操作来实例化子类. 举例 如果实体类Teacher继承实体类Person,实体类Student也继承自 ...

  7. JPA 系列教程10-双向一对一关联表

    双向一对一关联表的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255 ...

  8. JPA 系列教程9-双向一对一唯一外键

    双向一对一唯一外键的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(25 ...

  9. JPA 系列教程7-双向多对多

    双向多对多的ddl语句 同单向多对多表的ddl语句一致 Student package com.jege.jpa.many2many; import java.util.HashSet; import ...

随机推荐

  1. 正则表达式引擎:nfa的转换规则。

    正则表达式引擎:nfa的转换规则. 正则到nfa 前言 在写代码的过程中,本来还想根据龙书上的说明来实现re到nfa的转换.可是写代码的时候发现,根据课本来会生成很多的无用过渡节点和空转换边,需要许多 ...

  2. 为通过ClickOnce部署的应用程序进行数字签名

    为通过ClickOnce部署的应用程序进行数字签名 ClickOnce是.NET用于Windows应用程序的一种便捷部署方式.不过由于便捷,导致缺少自定义操作的空间.比如需要对通过ClickOnce部 ...

  3. Ubuntu 12.04(所有ubuntu发行版都适用)sudo免输入密码

    首先执行以下命令(该命令用来修改 /etc/sudoers 文件): $ sudo gedit /etc/sudoers 然后把  %sudo    ALL=(ALL:ALL) ALL  这行注释掉, ...

  4. listView 分页加载数据

    Android应用 开发中,采用ListView组件来展示数据是很常用的功能,当一个应用要展现很多的数据时,一般情况下都不会把所有的数据一次就展示出来,而是通过分页 的形式来展示数据,个人觉得这样会有 ...

  5. SharePoint 2016 自定义城市和区域字段

    前言 最近有这么一个需求,就是用到中国的各种行政区,然后还是三级联动,就琢磨写这么一个字段.然后,觉得挺有意义的,写字段的过程也有点心得,就想到拿到博客里分享给大家,一起看看. 1. 创建字段的解决方 ...

  6. 办理西蒙菲莎大学(本科)学历认证『微信171922772』SFU学位证成绩单使馆认证Simon Fraser University

    办理西蒙菲莎大学(本科)学历认证『微信171922772』SFU学位证成绩单使馆认证Simon Fraser University Q.微信:171922772办理教育部国外学历学位认证海外大学毕业证 ...

  7. Angularjs实现简单分页

    一个后台中总需要一款分页,那我为了自己方便使用,实现如下效果 我把这个组件命名为tm.pagination,原因是因为起名真的太难起了.而且我网名也叫天名, TM就这样了吧.github地址https ...

  8. 利用fiddler将本地网页放到某个域下

    注: 1)在学习慕课网课程<搜索框制作>中遇到如题困难,查找资料后解决,做此记录.课程网址http://www.imooc.com/video/263. 2)建议同时去学习慕课网课程< ...

  9. android 物理按键 监听

    android连接了一个4x4的矩阵键盘,linux内核中注册了按键,在app中监听键盘事件. package com.example.tony.keydemo; import android.sup ...

  10. jq 测试是否到页面最底端

    $(window).scroll(function () { if ($(document).scrollTop() + $(window).height() >= $(document).he ...