JPA 菜鸟教程 13 复合主键-@EmbeddedId+@Embeddable
2016-12-19 13:32
441 查看
GitHub
复合主键
指多个主键联合形成一个主键组合需求产生
比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示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[/code]其他关联项目
JPA 菜鸟教程 11 复合主键-2个@Id
http://blog.csdn.net/je_ge/article/details/53678085
JPA 菜鸟教程 12 复合主键-2个@Id+@IdClass
http://blog.csdn.net/je_ge/article/details/53678164源码地址
https://github.com/je-ge/jpa
如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
您的支持将鼓励我继续创作!谢谢!
相关文章推荐
- JPA 菜鸟教程 11 复合主键-2个@Id
- JPA 菜鸟教程 12 复合主键-2个@Id+@IdClass
- JPA联合主键的使用(@EmbeddedId+@Embeddable)
- JPA 菜鸟教程 14 自定义类型-@Embedded+@Embeddable
- JPA 菜鸟教程 8 双向一对一共享主键
- 在jpa的JpaRepository需要对符合主键@EmbeddedId 中的一个字段进行查询的用法
- JPA 菜鸟教程 19 jpa uuid主键生成策略
- JPA联合主键@EmbeddedId使用详解附查询例子
- JPA 主键@Id、@IdClass、@Embeddable、@EmbeddedId
- JPA 复合主键源码案例
- Java程序员从笨鸟到菜鸟之(六十)细谈Hibernate(十一)hibernate复合主键映射
- Java程序员从笨鸟到菜鸟之(六十)细谈Hibernate(十一)hibernate复合主键映射
- 嵌入式主键@EmbeddedId
- 13、复合主键映射(联合主键)
- JPA关联映射 - 复合主键映射
- 复合主键@IdClass
- Hibernate,JPA注解@EmbeddedId
- jpa的复合主键中关系中如何配置才能做到级联删除,我现在的问题是现在有一个学生类一个课程类,下面补充
- 复合主键JPA
- 嵌入式主键@EmbeddedId