Hbername compsite key

3642 ワード

map compossite keyには二つの方法があります。一つ目は@IdClassで二つ目は@Emboddableで、リンクを参照してください。 http://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate 
一. @IdClassの方法
1.ビーン
package locationService.beans;

import java.io.Serializable;

import javax.persistence.*;

@Entity
@Table(name = "entity_to_entity")
@IdClass(EntityToEntityPk.class)
public class EntityToEntity implements Serializable {

	  private static final long serialVersionUID = 11L;
	  private int parentId;
	  private int childId;
	  
	  @Id
	  @Column(name="parent_id")
	  public int getParentId() {
		return parentId;
	  } 
	  public void setParentId(int parentId) {
		this.parentId = parentId;
	  }
	  
	  @Id
	  @Column(name="child_id")
	  public int getChildId() {
		return childId;
	  }
	  public void setChildId(int childId) {
		this.childId = childId;
	  }


	  @Override
	  public String toString() {
	    return "parentId is " + parentId + ", childId is " + childId;
	  }

}


class EntityToEntityPk implements Serializable {
	  private static final long serialVersionUID = 12L;
	  protected Integer parentId;
	  protected Integer childId;
	  
	  public EntityToEntityPk() {}
	  
	  public EntityToEntityPk(Integer parentId, Integer childId) {
		  this.parentId = parentId;
		  this.childId = childId;
	  }
	
	  public Integer getParentId() {
		return parentId;
	  } 
	  public void setParentId(int parentId) {
		this.parentId = parentId;
	  }
	  
	  public Integer getChildId() {
		return childId;
	  }
	  public void setChildId(int childId) {
		this.childId = childId;
	  }
	  
	  @Override
	  public boolean equals(Object o) {
	      if(o instanceof EntityToEntityPk){
	    	  EntityToEntityPk other = (EntityToEntityPk) o;
	          return parentId.equals(other.getParentId()) && childId.equals(other.getChildId());
	      }
	
	      return false;
	  }
	
	  @Override
	  public int hashCode() {
	      return parentId.hashCode() + childId.hashCode();
	  }
  

}
  
equalsとhas Codeの方法はcompossite keyに必要です。
A compsite id must implement both methods or the id will not work properly.Hbernate must be able to compre ids.A compsite id will most likely use all its id fields in the equals and hashCode methods.
An entity does not need to implement equals and hashCode under some conditions.The persistence context garanties that an ntity with a given id exists only once.You cannot let t t to to object with the same ide persistent.Asuming that a class Country uses the isocode as ID、the follwing code will cause a non unique object exception.
参照リンク: http://www.laliluna.de/jpa-hibernate-guide/ch06s06.html
 
2.ユニットTest
  @Test
  public void testEnityToEntity() {
	 Session session = Config.getSessionFactory().openSession();
	 session.beginTransaction();
	
	 Query query = session.createQuery("select t.parentId from EntityToEntity t");
	
	 @SuppressWarnings("unchecked")
	 List<Object> entities = query.list();
	 assertEquals(entities.get(0), 1);
	 
	 session.getTransaction().commit();
	 session.close();
  }