• Hibernate composite key


    有两种方法来map composite key. 第一种用@IdClass第二种用@Embeddable,参考链接: http://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate 

    一. @IdClass方法

    1. Bean

    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和hasCode方法是composite key必须要有的

    A composite id must implement both methods or the id will not work properly. Hibernate must be able to compare ids. A composite 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 guaranties that an entity with a given id exists only once. You cannot let two objects with the same id become persistent. Assuming that a class Country uses the isocode as ID, the following code will cause a non unique object exception.

    参考链接: http://www.laliluna.de/jpa-hibernate-guide/ch06s06.html

    2. Unit 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();
      }
    
  • 相关阅读:
    解读AppIcon图标设置置信息和App内存警告临界值
    我在外包公司做增删改查有前途么?
    浅议Grpc传输机制和WCF中的回调机制的代码迁移
    2019.NET Conf China(中国.NET开发者峰会)活动全纪录:.NET技术之崛起,已势不可挡
    一位年轻而优秀的.NET开发者的成长点滴
    领域驱动设计-让程序员心中有码(九)
    2019.NET Conf,我们在共同期待
    码农的技术小世界
    .NET Core使用gRPC打造服务间通信基础设施
    坚持写了一年的博客,我有哪些收获
  • 原文地址:https://www.cnblogs.com/codingforum/p/4317062.html
Copyright © 2020-2023  润新知