JPA 系列教程21-JPA2.0-@MapKeyColumn
生活随笔
收集整理的這篇文章主要介紹了
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@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
如果覺得我的文章或者代碼對您有幫助,可以請我喝杯咖啡。您的支持將鼓勵我繼續創作!謝謝!
轉載于:https://www.cnblogs.com/je-ge/p/6254339.html
總結
以上是生活随笔為你收集整理的JPA 系列教程21-JPA2.0-@MapKeyColumn的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVAWEB项目如何实现验证码
- 下一篇: LaunchScreen原理