findAll()不返回正确的对象类型

时间:2021-12-28 22:30:46

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

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

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:

这个简单的查询按预期工作。我找回了ItemTags列表,可以完成ItemTags应该做的所有精彩事情:

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.

但是,只要我在标准中添加另一个表,而不是获取ItemTags列表,我就会得到一个通用对象列表。

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!

我需要做什么才能获得可用的对象?谢谢!

2 个解决方案

#1


In Hibernate, the

在Hibernate中,

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

“来自ItemTag b,标记a其中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.

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

You have to use e.g.

你必须使用例如

(ItemTag) theTags2[0][0]

to access the first ItemTag instance.

访问第一个ItemTag实例。

#2


Assuming you are just trying to get the ItemTag object you can also change the HQL to something like:

假设您只是想获取ItemTag对象,您还可以将HQL更改为:

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

That tells it you only want one object. You should also be able to use a join condition I think something like:

这告诉你只需要一个对象。您还应该能够使用连接条件我认为:

def theTags2 = ItemTag.findAll("from ItemTag b where b.tag is not null")

#1


In Hibernate, the

在Hibernate中,

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

“来自ItemTag b,标记a其中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.

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

You have to use e.g.

你必须使用例如

(ItemTag) theTags2[0][0]

to access the first ItemTag instance.

访问第一个ItemTag实例。

#2


Assuming you are just trying to get the ItemTag object you can also change the HQL to something like:

假设您只是想获取ItemTag对象,您还可以将HQL更改为:

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

That tells it you only want one object. You should also be able to use a join condition I think something like:

这告诉你只需要一个对象。您还应该能够使用连接条件我认为:

def theTags2 = ItemTag.findAll("from ItemTag b where b.tag is not null")