标签:style blog io color ar os 使用 java sp
联合主键的一些知识:
使用@EmbeddedId标示联合主键;
在联合主键类中只是定义确定联合主键的字段即可;
* 联合主键类的规则
* 1.必须包含一个无参的构造函数
* 2.必须实现序列化接口
* 3.必须重写hashCode和equals方法,而且equals方法的参数必须包括确定联合主键的所有字段
联合主键类的定义:
1 package com.yl.demo1.bean.JointPK; 2 3 import java.io.Serializable; 4 5 import javax.persistence.Column; 6 import javax.persistence.Embeddable; 7 8 /** 9 * 在联合主键类中只是定义确定联合主键的字段即可 10 * 联合主键类的规则 11 * 1.必须包含一个无参的构造函数 12 * 2.必须实现序列化接口 13 * 3.必须重写hashCode和equals方法,而且equals方法的参数必须包括确定联合主键的所有字段 14 * @author yul 15 * 16 *@Embeddable--告诉其它类,在使用AieLinePK时只是使用AieLinePK的属性作为实体的持久化属性 17 */ 18 19 @Embeddable //嵌入 20 public class AirLinePK implements Serializable { 21 private String startCity;//航空中每个城市都有一个三位的字母标识符 22 private String endCity; 23 24 public AirLinePK(){} 25 26 27 public AirLinePK(String startCity, String endCity) { 28 this.startCity = startCity; 29 this.endCity = endCity; 30 } 31 @Column(length=3) 32 public String getStartCity() { 33 return startCity; 34 } 35 public void setStartCity(String startCity) { 36 this.startCity = startCity; 37 } 38 @Column(length=3) 39 public String getEndCity() { 40 return endCity; 41 } 42 public void setEndCity(String endCity) { 43 this.endCity = endCity; 44 } 45 46 @Override 47 public int hashCode() { 48 final int prime = 31; 49 int result = 1; 50 result = prime * result + ((endCity == null) ? 0 : endCity.hashCode()); 51 result = prime * result 52 + ((startCity == null) ? 0 : startCity.hashCode()); 53 return result; 54 } 55 @Override 56 public boolean equals(Object obj) { 57 if (this == obj) 58 return true; 59 if (obj == null) 60 return false; 61 if (getClass() != obj.getClass()) 62 return false; 63 AirLinePK other = (AirLinePK) obj; 64 if (endCity == null) { 65 if (other.endCity != null) 66 return false; 67 } else if (!endCity.equals(other.endCity)) 68 return false; 69 if (startCity == null) { 70 if (other.startCity != null) 71 return false; 72 } else if (!startCity.equals(other.startCity)) 73 return false; 74 return true; 75 } 76 77 78 }
1 package com.yl.demo1.bean.JointPK; 2 3 import javax.persistence.Column; 4 import javax.persistence.EmbeddedId; 5 import javax.persistence.Entity; 6 7 @Entity 8 public class AirLine { 9 private AirLinePK id; 10 private String name; 11 12 13 14 public AirLine(){} 15 16 public AirLine(AirLinePK id) { 17 this.id = id; 18 } 19 20 public AirLine(String startCity, String endCity, String name) { 21 this.id = new AirLinePK(startCity, endCity); 22 this.name = name; 23 } 24 25 @EmbeddedId 26 public AirLinePK getId() { 27 return id; 28 } 29 public void setId(AirLinePK id) { 30 this.id = id; 31 } 32 @Column(length=20) 33 public String getName() { 34 return name; 35 } 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 }
常用操作:
1 @Test 2 public void save() { 3 EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL"); 4 EntityManager em = factory.createEntityManager(); 5 em.getTransaction().begin();//事务开始 6 7 em.persist(new AirLine("PEK", "SHA", "北京飞上海")); 8 9 em.getTransaction().commit(); 10 em.close(); 11 factory.close(); 12 }
标签:style blog io color ar os 使用 java sp
原文地址:http://www.cnblogs.com/dreamfree/p/4077118.html