JPA之JPA中的联合主键

如果要搭建JPA开发环境,请从JPA第一篇博客看起。


下面以坐飞机为例,讲解联合主键的使用。基本思想是一条航线由两个端点组成。


1、组合主键类

package compositepk;

import java.io.Serializable;

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

/**
 * 组合主键类: 这里以坐飞机为例,一段航线由起始点和结束点组成 规则一:必须提供无参的构造函数 规则二:必须实现Serializable接口
 * 规则三:必须重写hashCode()和equals()方法
 */
@Embeddable//该注解表示该类用在实体里面,直接使用该类定义的属性
public class AirLinePK implements Serializable {
	private static final long serialVersionUID = 5913768452213218677L;
	private String startCity;
	private String endCity;

	public AirLinePK() {
		super();
	}

	public AirLinePK(String startCity, String endCity) {
		super();
		this.startCity = startCity;
		this.endCity = endCity;
	}

	@Column(length=3)
	public String getStartCity() {
		return startCity;
	}

	public void setStartCity(String startCity) {
		this.startCity = startCity;
	}

	@Column(length=3)
	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;
	}

}


2、航线类

package compositepk;

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

/**
 * 航线类
 */
@Entity
public class AirLine {
	private AirLinePK id;//可嵌入主键
	private String name;

	public AirLine() {
		super();
	}

	public AirLine(AirLinePK id) {
		super();
		this.id = id;
	}
	
	public AirLine(String startCity, String endCity, String name){
		this.id = new AirLinePK(startCity, endCity);
		this.name = name;
	}

	//用于标识组合主键
	@EmbeddedId
	public AirLinePK getId() {
		return id;
	}

	public void setId(AirLinePK id) {
		this.id = id;
	}

	@Column(length=20)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

3、测试类中的代码

package junit.test;

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

import org.junit.Test;

import compositepk.AirLine;

/**
 * 测试组合主键
 */
public class CompositTest {

	@Test
	public void save() {
		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("sunft_first");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		em.persist(new AirLine("PEK", "SHA", "北京飞上海"));
		em.getTransaction().commit();
		em.close();
		factory.close();
	}
}


4、数据库中生成的记录如下



你可能感兴趣的:(JPA,联合主键)