gpt4 book ai didi

java - 如何在数据库表中查询仅包含两个数据对象的记录列表

转载 作者:行者123 更新时间:2023-12-02 05:31:25 26 4
gpt4 key购买 nike

我的数据库表如下所示:

ID      ACCESSID    ENTITYGUID      NAME    NAMEID      OWNERGUID   TIMECREATED     VALUEID     VALUETYPE  

我只想检索包含两个字段的列表:namevalueId,而不是整个记录。我尝试如下:

通过预测:

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=?)

这些方法都不起作用。请帮助我使其工作,以便我可以获得包含 namenameId

两个数据对象的列表

更新:

数据库表实体类:

public class RevMetadata implements Serializable {
private String name;
private int valueId;

}

最佳答案

如果只检索某些指定的列,结果将为List<Object[]> .

就您而言,Object[]将填充 name值 => 索引 0 nameId => 索引 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

关于java - 如何在数据库表中查询仅包含两个数据对象的记录列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25499835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com