欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JPA 联合主键(多主键)映射

程序员文章站 2022-03-02 15:33:07
...
package com.jvwl.model;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
/**
 * 航班类
 * @author Jerval
 *
 */
@Entity
public class AirLine {
	/**
	 * 用类作ID,相当于多个主键
	 */
	private AirLinePK id;
	/**
	 * 航班名称
	 */
	private String name;

	public AirLine() {
	}

	public AirLine(AirLinePK id) {
		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 = 10, nullable = true)
	public String getName() {
		return name;
	}

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

}

 

package com.jvwl.model;

import java.io.Serializable;

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

/**
 * 联合主键类
 * 
 * 定义联合主键类要求:
 * 1.要有无参构造方法
 * 2.要实现序列化接口
 * 3.重写hashCode方法和equals方法
 * @author Jerval
 */
@Embeddable
// 标识它不是一个实体,而是嵌入别的实体中的一个属性。
public class AirLinePK implements Serializable {

	private static final long serialVersionUID = 5940144469818589822L;
	private String startCity;
	private String endCity;

	public AirLinePK() {
	}

	public AirLinePK(String startCity, String endCity) {
		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;
	}

}

 

package junit.test;

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

import org.junit.Test;

import com.jvwl.model.AirLine;

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

 

相关标签: JPA junit