JPA--联合主键

联合主键必须符合以下三点:

   1、必须实现Serializable序列化 2、必须提示无参的构造方法 3、必须重写hashCode和equals方法


AirLinePK.java


package com.olay.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class AirLinePK implements Serializable{
	/** 
	 * 联合主键  
	 *  
	 * @Embeddable 表示该类中所有属性在应用该联合主键的类中作为它的属性(字段) 
	 * @author olay
	 *  
	 */  
	private static final long serialVersionUID = 1L;
	private String staCity;
	private String endCity;

	public AirLinePK() {

	}

	public AirLinePK(String staCity, String endCity) {
		this.staCity = staCity;
		this.endCity = endCity;
	}
   
	@Column(length=30,nullable=false)
	public String getStaCity() {
		return staCity;
	}

	public void setStaCity(String staCity) {
		this.staCity = staCity;
	}

	@Column(length=30,nullable=false)
	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 + ((staCity == null) ? 0 : staCity.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 (staCity == null) {
			if (other.staCity != null)
				return false;
		} else if (!staCity.equals(other.staCity))
			return false;
		return true;
	}
	
	

}


AirLineEntity.java

package com.olay.entity;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity  
@Table(name = "AIRLINE") 
public class AirLineEntity {

	private AirLinePK a_id;
	private String a_name;

	// 联合主键
	@EmbeddedId
	public AirLinePK getA_id() {
		return a_id;
	}

	public void setA_id(AirLinePK aId) {
		a_id = aId;
	}

	@Column(length=30,nullable=false)
	public String getA_name() {
		return a_name;
	}

	public void setA_name(String aName) {
		a_name = aName;
	}

}

测试类

package com.olay.entity;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import junit.framework.TestCase;

public class CompositePKTest extends TestCase { 
    public void save(){  
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("olayjpa");  
        EntityManager em = factory.createEntityManager();  
        em.getTransaction().begin();  
          
        AirLineEntity airLine = new AirLineEntity();  
        airLine.setA_id(new AirLinePK("BeJing","ShangHai"));  
        airLine.setA_name("北京至上海");  
          
        em.persist(airLine);  
          
        em.getTransaction().commit();  
        em.close();  
        factory.close();  
    }  

}

生成的表如下



表的主键为


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值