findAll()不返回正确的对象类型 [英] findAll() Not Returning Correct Object Type

查看:132
本文介绍了findAll()不返回正确的对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ItemTag对象包含一个Item对象和一个Tag对象。 (这些是Java域对象。)



这个简单的查询按预期工作。我得到一个ItemTags列表,并可以完成ItemTag应该做的所有美妙事情:

  def theTags1 = ItemTag.findAll (from ItemTag b)

例如:

  println(theTags1 [0] .tag.tag)



 朝圣者进步

但是,只要向条件中添加另一个表,而不是获取ItemTag列表,我只需获取一个通用对象列表。



例如以下

  def theTags2 = ItemTag.findAll(from ItemTag b,Tag a其中b.tag = a)

theTags2.each {
theClass = it.getClass();
nameOfClass = theClass.getName();
println(nameOfClass)
}

返回

  [Ljava.lang.Object; 
[Ljava.lang.Object;
[Ljava.lang.Object;

我根本无法使用结果对象。例如:

  println(theTags2 [0] .tag.tag)

给我这个错误:

 异常评估属性'标签'为java.util.ArrayList,原因:groovy.lang.MissingPropertyException:没有这样的属性:类的标签:java.lang.String 



  def exTag2 =(ItemTag)theTags2 [0] 
code>

给我这个错误:

 不能将类'[Ljava.lang.Object; @ 2d81f'与类'[Ljava.lang.Object;'转换为类'org.maflt.flashlit.pojo.ItemTag''

获取可用对象需要做些什么?感谢!

解决方案

在Hibernate中,

lockquote
















$ b $查询是一个交叉连接。此查询的结果是一个Object数组列表,其中第一个项目是ItemTag实例,第二个项目是一个Tag实例。



您必须使用例如


(ItemTag)theTags2 [0] [0]


访问第一个ItemTag实例。


ItemTag objects contain an Item object and a Tag object. (These are Java domain objects.)

This simple query works as expected. I get back a list ItemTags and can do all the wonderful things that ItemTags are supposed to do:

def theTags1 = ItemTag.findAll("from ItemTag  b")

For example:

println(theTags1[0].tag.tag)

gives me this as expected:

Pilgrim's Progress

However, as soon as I add another table to the criteria, instead of getting a list of ItemTags, I just get a list of generic objects.

e.g the following

def theTags2 = ItemTag.findAll("from ItemTag  b, Tag a where b.tag= a")

theTags2.each {
     theClass = it.getClass();
     nameOfClass = theClass.getName();
     println(nameOfClass)
}   

returns

[Ljava.lang.Object;
[Ljava.lang.Object;
[Ljava.lang.Object;

And I can't use the resulting objects at all. For example:

println(theTags2[0].tag.tag)

gives me this error:

Exception evaluating property 'tag' for java.util.ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: tag for class: java.lang.String

and

def exTag2 = (ItemTag) theTags2[0]

gives me this error:

Cannot cast object '[Ljava.lang.Object;@2d81f' with class '[Ljava.lang.Object;' to class 'org.maflt.flashlit.pojo.ItemTag'

What do I need to do to get usable objects? Thanks!

解决方案

In Hibernate, the

"from ItemTag b, Tag a where b.tag= a"

query is a cross-join. The result of this query is a list of Object arrays where the first item is an ItemTag instance and the second is a Tag instance.

You have to use e.g.

(ItemTag) theTags2[0][0]

to access the first ItemTag instance.

这篇关于findAll()不返回正确的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆