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

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

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值