Hibernate composite key
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(); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。