JPA 系列教程21-JPA2.0-@MapKeyColumn

时间:2023-12-24 19:19:25

@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

JPA 系列教程21-JPA2.0-@MapKeyColumn