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();
}
原文:http://www.cnblogs.com/codingforum/p/4317062.html