我已经用hibernate中的where条件执行了select查询。它返回列表中的值,其中包含where字段。我不需要列表中的where condition字段。我怎么能把它删除。
我使用的查询:
select cityname,cityid from citymaster where stateid ='2';
它返回列表中的cityname,cityid,stateid。但我需要cityname,cityid
休眠:
Query query = session.createQuery("select cityname,cityid from Citymaster where stateid ='2'");
query.setResultTransformer(Transformers.aliasToBean(Citymaster.class));
List cityMasterList = query.list();
Citymaster[] cma = cityMasterList.toArray();
在数组中的cma-cityname,cityid,stateid中。但我需要cityname,cityid。
答案 0 :(得分:2)
我真的怀疑这个查询检索了stateid。我使用Hibernate执行了很多类似的查询,他们只返回select子句中的列。
当然,如果你的Citymaster
类有一个stateid
属性,那么它不会因为你没有查询它而神奇地从类中消失。所有返回的实例将只具有此属性中的默认值(您在Citymaster
的无参数构造函数中设置的值,或0或如果您未在构造函数中设置任何特定值,则为null,具体取决于stateid
字段的类型。
如果您想确定,只需删除query.setResultTransformer调用,并检查列表的内容。它应该包含Object[]
包含两个元素的实例。
答案 1 :(得分:0)
正在重新获取该值,因为您已在citymaster中设置了stateid的方法。 因此,您不必在Citymaster类中为它编写set方法。 删除该方法&检查一个。你会得到你的输出。