`
hududumo
  • 浏览: 239107 次
文章分类
社区版块
存档分类
最新评论

JPA复合主键案例

 
阅读更多

AirLine实体类

package com.jason.bean;

import java.io.Serializable;

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

@Entity
public class AirLine implements Serializable{
	private static final long serialVersionUID = -5617618540181055061L;
	private AirLinePk id;
    private String name;

    public AirLine() {
        super();
    }

    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;
    }
    
}

AirLinePk复合主键类

package com.jason.bean;

import java.io.Serializable;

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

/**
 * 使用复合主键要满足的条件
 * 
 * 1、要实现序列化 2、提供默认的构造方法 3、实现hashCode
 * 
 * @author jason
 * 
 */

@Embeddable  //embeddable: 可嵌入的
public class AirLinePk implements Serializable {
	private static final long serialVersionUID = 7102936569989834678L;
	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;
        final 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;
    }

}

PKTest测试类

package junit.test;

import java.util.List;

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

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.jason.bean.AirLine;
import com.jason.bean.AirLinePk;

public class PKTest {
	private static EntityManagerFactory factory;
	private static EntityManager em;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		 factory = Persistence.createEntityManagerFactory("jason");
		 em = factory.createEntityManager();
	     em.getTransaction().begin();
	}
	
    @Test
    public void save() {
        em.persist(new AirLine("PEK","SHA","北京飞上海"));
    }
    
    @Test
    public void find() {
        AirLine airLine=em.find(AirLine.class, new AirLinePk("PEK","SHA"));
        System.out.println(airLine.getName());
    }
    
    @Test
    public void update() {
        AirLine airLine=em.getReference(AirLine.class, new AirLinePk("PEK","SHA"));
        airLine.setName("北京飞上海啊哈哈");
        em.merge(airLine);
    }
    
    @SuppressWarnings("unchecked")
	@Test
    public void list() {
        List<AirLine> airLines=em.createQuery("select o from AirLine o").getResultList();
        for(AirLine air:airLines){
            System.out.println(air.getName());
        }
    }
    
    @Test
    public void detele() {
        em.remove(em.getReference(AirLine.class, new AirLinePk("PEK","SHA")));
    }

    /**
     * 用来判断映射是否成功
     * 
     */
    @Test
    public void test() {
        Persistence.createEntityManagerFactory("jason");
    }
    
    @AfterClass
	public static void tearDownAfterClass() throws Exception {
    	em.getTransaction().commit();
        em.close();
        factory.close();
	}
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics