首页 > Web开发 > 详细

Hibernate composite key

时间:2015-03-06 06:09:09      阅读:378      评论:0      收藏:0      [点我收藏+]

1. Bean

package locationService.beans;

import java.io.Serializable;

import javax.persistence.*;

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

  private static final long serialVersionUID = 1L;
  
  @EmbeddedId
  private EntityToEntityPK entityToEntityPk;
  
  public EntityToEntityPK getEntityToEntityPK() {
	return entityToEntityPk;
  }
  public void setEntityToEntityPK(EntityToEntityPK entityToEntityPk) {
	this.entityToEntityPk = entityToEntityPk;
  }


  @Embeddable
  class EntityToEntityPK implements Serializable {
    private static final long serialVersionUID = 1L;
    private int parentId;
    private int childId;
    
    public EntityToEntityPK() {}
    
    public EntityToEntityPK(int parentId, int childId) {
  	  this.parentId = parentId;
  	  this.childId = childId;
    }


  }
  


}

2. Unit Test

  @Test
  public void testEnityToEntity() {
	 Session session = Config.getSessionFactory().openSession();
	 session.beginTransaction();
	
	 Query query = session.createSQLQuery("select parent_id from entity_to_entity");
	
	 @SuppressWarnings("unchecked")
	 List<Object> entities = query.list();
	 assertEquals(entities.get(0), 1);
	 
	 session.getTransaction().commit();
	 session.close();
  }

 

Hibernate composite key

原文:http://www.cnblogs.com/codingforum/p/4317062.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!