@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@GeneratedValueprivate 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;@BeforeClasspublic static void setUpBeforeClass() throws Exception {entityManagerFactory = Persistence.createEntityManagerFactory("com.jege.jpa");}@Beforepublic void setUp() throws Exception {entityManager = entityManagerFactory.createEntityManager();// Session}@Testpublic 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();}@Testpublic void find() throws Exception {persist();entityManager.clear();Employee employee = entityManager.find(Employee.class, 1L);System.out.println(employee.getName());System.out.println(employee.getOthers());}@Afterpublic void tearDown() throws Exception {if (entityManager != null && entityManager.isOpen())entityManager.close();}@AfterClasspublic 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
源码地址
https://github.com/je-ge/jpa
如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。您的支持将鼓励我继续创作!谢谢!