zoukankan      html  css  js  c++  java
  • 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();
      }
    
    }
    

    其他关联项目

    源码地址

    https://github.com/je-ge/jpa

    如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。您的支持将鼓励我继续创作!谢谢!
    微信打赏
    支付宝打赏

  • 相关阅读:
    第七届蓝桥杯省赛javaB组 第七题剪邮票
    蓝桥杯第八届省赛 包子凑数
    蓝桥第八届省赛 javaB组承压计算
    Android studio 中添加依赖model时依赖所需的准备
    java 实现小数取最后一位、四舍五入
    如何创建 SVN 服务器,并搭建自己的 SVN 仓库 如何将代码工程添加到VisualSVN Server里面管理
    jquery对append进的元素的监听操作
    定位
    float/文档流
    line-height
  • 原文地址:https://www.cnblogs.com/je-ge/p/6254339.html
Copyright © 2011-2022 走看看