zoukankan      html  css  js  c++  java
  • JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable

    复合主键

    指多个主键联合形成一个主键组合

    需求产生

    比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示

    ddl语句

    同复合主键-2个@Id和复合主键-2个@Id+@IdClass一样

    Airline

    package com.jege.jpa.embedded;
    
    import javax.persistence.EmbeddedId;
    import javax.persistence.Entity;
    import javax.persistence.Table;
    
    /**
     * @author JE哥
     * @email 1272434821@qq.com
     * @description:复合主键-@EmbeddedId
     */
    @Entity
    @Table(name = "t_airline")
    public class Airline {
      @EmbeddedId
      private AirlinePK pk;
      private String name;
    
      public Airline() {
    
      }
    
      public Airline(AirlinePK pk, String name) {
        this.pk = pk;
        this.name = name;
      }
    
      public Airline(String startCity, String endCity, String name) {
        pk = new AirlinePK(startCity, endCity);
        this.name = name;
      }
    
      public AirlinePK getPk() {
        return pk;
      }
    
      public void setPk(AirlinePK pk) {
        this.pk = pk;
      }
    
      public String getName() {
        return name;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    
      @Override
      public String toString() {
        return "Airline [pk=" + pk + ", name=" + name + "]";
      }
    
    }
    

    AirlinePK

    package com.jege.jpa.embedded;
    
    import java.io.Serializable;
    
    import javax.persistence.Column;
    import javax.persistence.Embeddable;
    
    /**
     * @author JE哥
     * @email 1272434821@qq.com
     * @description:复合主键-@Embeddable
     */
    @Embeddable
    public class AirlinePK implements Serializable {
      private static final long serialVersionUID = 2836348182939717563L;
      @Column(length = 3, nullable = false)
      private String startCity;
      @Column(length = 3, nullable = false)
      private String endCity;
    
      public AirlinePK() {
      }
    
      public AirlinePK(String startCity, String endCity) {
        this.startCity = startCity;
        this.endCity = endCity;
      }
    
      public String getStartCity() {
        return startCity;
      }
    
      public void setStartCity(String startCity) {
        this.startCity = startCity;
      }
    
      public String getEndCity() {
        return endCity;
      }
    
      public void setEndCity(String endCity) {
        this.endCity = endCity;
      }
    
      @Override
      public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((endCity == null) ? 0 : endCity.hashCode());
        result = prime * result + ((startCity == null) ? 0 : startCity.hashCode());
        return result;
      }
    
      @Override
      public boolean equals(Object obj) {
        if (this == obj)
          return true;
        if (obj == null)
          return false;
        if (getClass() != obj.getClass())
          return false;
        AirlinePK other = (AirlinePK) obj;
        if (endCity == null) {
          if (other.endCity != null)
        return false;
        } else if (!endCity.equals(other.endCity))
          return false;
        if (startCity == null) {
          if (other.startCity != null)
        return false;
        } else if (!startCity.equals(other.startCity))
          return false;
        return true;
      }
    
      @Override
      public String toString() {
        return "AirlinePK [startCity=" + startCity + ", endCity=" + endCity + "]";
      }
    
    }
    

    MainTest

    package com.jege.jpa.embedded;
    
    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;
    
    /**
     * @author JE哥
     * @email 1272434821@qq.com
     * @description:复合主键@EmbeddedId+@Embeddable测试
     */
    public class MainTest {
      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();
      }
    
      @Test
      public void persist() {
        Airline airline = new Airline("PEK", "SHA", "北京飞上海");
        airline.setName("北京飞上海");
    
        entityManager.getTransaction().begin();
        entityManager.persist(airline);
        entityManager.getTransaction().commit();
      }
    
      @Test
      public void find() {
        persist();
    
        AirlinePK pk = new AirlinePK("PEK", "SHA");
        Airline airline = entityManager.find(Airline.class, pk);
        System.out.println(airline);
      }
    
      @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();
      }
    }
    http://blog.csdn.net/je_ge/article/details/53678164

    其他关联项目

    源码地址

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

    如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!
    微信打赏
    支付宝打赏

  • 相关阅读:
    以太坊客户端Ethereum Wallet与Geth区别简介
    苹果企业版签名分发相关问题,蒲公英签名,fir.im分发,安装ipa设置信任
    usdt钱包开发,比特币协议 Omni 层协议 USDT
    产品经理-需求分析-用户故事-敏捷开发 详解 一张图帮你了解Scrum敏捷流程
    产品经理杂谈,产品管理=技术+设计+业务
    使用NodeJsScan扫描nodejs代码检查安全性
    人人都是操盘手(李笑来内部录音,揭秘币圈黑幕完整文字版)运营驱动时代,欧神比李笑来究竟差在哪里?
    Node.js中环境变量process.env详解
    selenium-java web自动化测试工具抓取百度搜索结果实例
    前一天或后一天
  • 原文地址:https://www.cnblogs.com/je-ge/p/6201169.html
Copyright © 2011-2022 走看看