我的数据库表格如下:
ID ACCESSID ENTITYGUID NAME NAMEID OWNERGUID TIMECREATED VALUEID VALUETYPE
我想只检索包含两个字段name
和valueId
的列表,而不是整个记录。我尝试如下:
通过投影:
public void getAllEntityMetadata(int GUID) {
Criteria c = session.createCriteria(RevMetadata.class)
.add(Restrictions.eq("entityGUID", GUID))
.setProjection(Projections.property("name"))
.setProjection(Projections.property("nameId"));
List<RevMetadata> m = (List<RevMetadata>) (RevMetadata) c.list();
for (RevMetadata item : m) {
System.out.println("-> All entity metadata for GUID: " + item.getName() + " : " + item.getValueId());
}
}
我收到错误:
Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to wakiliproject.Persistence.RevMetadata
at wakiliproject.Persistence.PersistRevMetadata.getAllEntityMetadata(PersistRevMetadata.java:112)
at wakiliproject.HomeController.showMetastring(HomeController.java:492)
... 40 more
通过示例查询,我只得到下面的输出,但没有数据。不会调用错误:
public void getAllEntityMetadataEg(int GUID) {
RevMetadata m = new RevMetadata();
m.setEntityGUID(GUID);
Example e = Example.create(m);
Criteria c = session.createCriteria(RevMetadata.class).add(e);
for (Iterator it = c.list().iterator(); it.hasNext();) {
RevMetadata item = (RevMetadata) it.next();
System.out.println("-> All entity metadata for GUID: " + item.getName() + " : " + item.getValueId());
}
}
输出:
Hibernate: select this_.id as id1_4_0_, this_.accessId as accessId2_4_0_, this_.entityGUID as entityGU3_4_0_, this_.name as name4_4_0_, this_.nameId as nameId5_4_0_, this_.ownerGUID as ownerGUI6_4_0_, this_.timeCreated as timeCrea7_4_0_, this_.valueId as valueId8_4_0_, this_.valueType as valueTyp9_4_0_ from METADATA this_ where (this_.accessId=? and this_.entityGUID=? and this_.nameId=? and this_.ownerGUID=? and this_.timeCreated=? and this_.valueId=?)
这些方法都不起作用。请帮助我使其工作,以便我可以获得包含name
和nameId
更新
数据库表实体类:
public class RevMetadata implements Serializable {
private String name;
private int valueId;
}
答案 0 :(得分:1)
如果只检索某些指定的列,则结果为List<Object[]>
。
在您的情况下,Object[]
将填充 name
value =&gt; index 0 和 nameId
=&gt;指数1 。需要将其转换为目标值类型。
Criteria criteria = session.createCriteria(RevMetadata.class);
criteria.add(Restrictions.eq("entityGUID", GUID));
criteria.setProjection(Projections.projectionList()
.add(Projections.property("name"))
.add(Projections.property("nameId")));
List<Object[]> criteriaResult = criteria.list();
List<RevMetadata> results = new ArrayList<RevMetadata>();
//then get result into the class properties
for (Object[] object : criteriaResult) {
//cast to target value type
String name = (String)object[0];
int nameId = ((Integer)object[1]).intValue();
RevMetadata result = new RevMetadata();
result.setName(name);
result.setValueId(nameId);
results.add(result);
}
//now results of List<RevMetadata> is ready
答案 1 :(得分:1)
如果您想获得多个列,则需要将投影添加到projectList中:
Projections.projectionList()
.add(Projections.property("name"))
.add(Projections.property("valueId"))
c.list()
还会返回List
,此列表包含添加到projection list
的字段,因此它包含object array
,第一个字段为name
属性和第二个字段作为valueId
属性。所以你的c.list()
应该是这样的:
List<Object[]> rows = (List<Object[]>) c.list();
以下是有效的更改:
Criteria c = session.createCriteria(RevMetadata.class)
.add(Restrictions.eq("entityGUID", GUID));
c.setProjection(Projections.projectionList()
.add(Projections.property("name"))
.add(Projections.property("valueId")));
List<Object[]> rows = (List<Object[]>) c.list();
System.out.println(rows);
for (Object[] row : rows) {
System.out.println(row[0] + " and " + row[1]);
}
答案 2 :(得分:0)
List m = c.list();
for(Iterator it = c.iterator(); it.hasNext()) {
Object[] row = (Object[]) it.next();
System.out.println("-> All entity metadata for GUID: " + row[0] + " : " +row[1]);
}
答案 3 :(得分:0)
似乎问题是双重投射
(List<RevMetadata>) (RevMetadata) c.list();
尝试删除演员
(RevMetadata)