JPA 菜鸟教程 21 JPA2.0 @MapKeyColumn
2017-01-05 21:53
405 查看
GitHub
@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-@CollectionTablehttp://blog.csdn.net/je_ge/article/details/53998548
源码地址
https://github.com/je-ge/jpa如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
您的支持将鼓励我继续创作!谢谢!
相关文章推荐
- JPA 菜鸟教程 20 JPA2.0 @CollectionTable
- ExtJS2.0实用简明教程——Column列布局
- google map key 申请官网地址教程
- JPA 菜鸟教程 9 双向一对一唯一外键
- JPA 菜鸟教程 14 自定义类型-@Embedded+@Embeddable
- JPA 菜鸟教程 13 复合主键-@EmbeddedId+@Embeddable
- 菜鸟教程终极篇之Microsoft Windows Pre-installation Environment (Windows PE) 2.0 推荐
- JPA 菜鸟教程 18 自动把firstName+lastName合并为name字段
- JPA 菜鸟教程 7 双向多对多
- JPA 菜鸟教程 6 单向多对多
- JPA 菜鸟教程 16 继承-联合子类-JOINED
- .Net 一对多的关联Map写法(KeyColumn)
- JavaPersistenceWithHibernate第二版笔记-第七章-004Mapping a map(@MapKeyEnumerated 、 @MapKeyTemporal、@MapKeyColumn)
- swift菜鸟入门视频教程-12-21讲
- JPA 菜鸟教程 8 双向一对一共享主键
- 菜鸟教程终极篇之Microsoft Windows Pre-installation Environment (Windows PE) 2.0
- Hibernate,JPA注解@PrimaryKeyJoinColumn
- JPA 菜鸟教程 1 环境搭建
- JPA 菜鸟教程 11 复合主键-2个@Id
- JPA 菜鸟教程 17 继承-独立表-TABLE_PER_CLASS