JPA 系列教程21-JPA2.0-@MapKeyColumn
@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();
}
}
其他关联项目
- JPA 系列教程20-JPA2.0-@CollectionTable
http://blog.csdn.net/je_ge/article/details/53998548
源码地址
如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。您的支持将鼓励我继续创作!谢谢!
JPA 系列教程21-JPA2.0-@MapKeyColumn的更多相关文章
- JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable
复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id和复合主键-2个@Id+ ...
- JPA 系列教程12-复合主键-2个@Id+@IdClass
复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id一样 Airline p ...
- JPA 系列教程5-双向一对多
双向一对多的ddl语句 同单向多对一,单向一对多表的ddl语句一致 Product package com.jege.jpa.one2many; import javax.persistence.En ...
- JPA 系列教程4-单向一对多
JPA中的@OneToMany @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface OneToMany { Class tar ...
- JPA 系列教程17-继承-独立表-TABLE_PER_CLASS
PerTable策略 每个具体的类一个表的策略 举例 这种映射策略每个类都会映射成一个单独的表,类的所有属性,包括继承的属性都会映射成表的列. 这种映射策略的缺点是:对多态关系的支持有限,当查询涉及到 ...
- JPA 系列教程16-继承-联合子类-JOINED
联合子类策略 这种情况下子类的字段被映射到各自的表中,这些字段包括父类中的字段,并执行一个join操作来实例化子类. 举例 如果实体类Teacher继承实体类Person,实体类Student也继承自 ...
- JPA 系列教程10-双向一对一关联表
双向一对一关联表的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255 ...
- JPA 系列教程9-双向一对一唯一外键
双向一对一唯一外键的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(25 ...
- JPA 系列教程7-双向多对多
双向多对多的ddl语句 同单向多对多表的ddl语句一致 Student package com.jege.jpa.many2many; import java.util.HashSet; import ...
随机推荐
- Operation not permitted引发的惊魂72小时
0.问题及描述 在测试产品的时候,莫名其妙发现了我们的主进程VPNd会出现以下的报错: 2013-07-18 13:05:13 www.1.com/192.168.200.220:65527 wri ...
- Java远程调用邮件服务器,实现邮件发送
写这篇文章的背景是公司Android客户端需要实现一个功能,实现类似于密码找回或者用户注册完发送一个邮件给用户的功能,当然这些逻辑客户端只负责请求自己的服务端,自己的服务端再去请求邮件服务器. 邮件服 ...
- Java7新特性
① 新增了switch对字符串的支持,也就是说可以在switch之后直接使用字符串来进行判断,语法基本与Java7之前支持的语法一样. ② 对数值字面量的增强支持,首先是可以在源代码中直接使用二进制数 ...
- CF 327E(Axis Walking-状态压缩Dp-lowbit的使用)
E. Axis Walking time limit per test 3 seconds memory limit per test 512 megabytes input standard inp ...
- VS2012下基于Glut OpenGL glScissor示例程序:
剪裁测试用于限制绘制区域.我们可以指定一个矩形的剪裁窗口,当启用剪裁测试后,只有在这个窗口之内的像素才能被绘制,其它像素则会被丢弃.换句话说,无论怎么绘制,剪裁窗口以外的像素将不会被修改.有的朋友可能 ...
- swift设置textfield边框颜色
//swift3.0,如果是2.0的话也可以照着这个样子去写,语法有所变动.根据联想出来的就可以了. let tx = UITextField(frame: CGRect(x: 100, y: 100 ...
- 关于preg_match()函数的一点小说明
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $ ...
- Qt 富文本处理
富文本处理 所有的类围绕 QTextDocument 展开, 它保证了用户可以创建和修改 富文本块, 而无须定义中间语言.一个 QTextDocument 可以通过两个接口操作, 一个是用于编辑的 C ...
- 2014蓝桥杯问题 C: 神奇算式
没做完,先搞答题了 #include <stdio.h> #include<string.h> #include<stdlib.h> int comp(const ...
- 打包apk java 虚拟机内存不足
解决方案:在android->sdk->build-tools-android-version 中有个 dx.bat dx.bat --dex 命令的dx.bat脚本有这样一句代码 REM ...