Employee.java
public class Employee
{
String id;
String name;
List<String> designations;
List<String> qualifications;
}
Employee.hbm.xml
<hibernate-mapping>
<class name="com.novelty.Employee" table="Employee">
<id column="ID" name="id" type="long">
<generator class="increment"/>
</id>
<property column="name" name="name"/>
<list lazy="false" name="designations" table="designations">
<key column="ID"/>
<list-index column="idx"/>
<element column="designation" type="string"/>
</list>
<list lazy="false" name="qualifications" table="qualifications">
<key column="ID"/>
<list-index column="idx"/>
<element column="qualification" type="string"/>
</list>
</hibernate-mapping>
我需要获取特定员工的名称列表(我有名字或ID)。我不想将Employee对象作为一个整体获取并获取列表。我尝试了预测,但徒劳无功。
Criteria criteria = session.createCriteria(c);
ProjectionList proList = Projections.projectionList();
proList.add(Projections.property("designations"));
criteria.setProjection(proList);
List list = criteria.list();
我得到了以下异常堆栈跟踪。
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at org.hibernate.loader.criteria.CriteriaLoader.getResultColumnOrRow(CriteriaLoader.java:148)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:639)
at org.hibernate.loader.Loader.doQuery(Loader.java:829)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2533)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
答案 0 :(得分:1)
由于designations
是一个列表,您应该join
try {
Criteria criteria = session.createCriteria(Employee.class);
criteria.setFetchMode("designations", FetchMode.JOIN);
List list = criteria.list();
} catch (Exception e) {
//print e
}
答案 1 :(得分:0)
无法获得符合条件的元素集合(阅读here)。您必须使用本机SQL