hibernate @Any 映射,无法获取“任何”映射物品

发布于 2024-11-17 16:17:46 字数 1246 浏览 2 评论 0原文

使用 hibernate @Any 映射来映射同一个实体中的多个实体,我无法构建请求来获取特定类的所有对象。

Hibernate 似乎无法处理这种请求。

我的应用程序无法启动并抛出以下错误:

Invocation of init method failed; nested exception is java.lang.ClassCastException: org.hibernate.type.AnyType cannot be cast to org.hibernate.type.ComponentType

删除请求中的“left join fetch ic.item i”部分可以解决此错误。

例如,我有以下模型:

@Entity
@NamedQueries({
@NamedQuery(name = "GET_ITEMS", query = "from ItemContainer ic left join fetch ic.item i where ic.id=:containerId and ic.class=:clazz")})
public class ItemContainer {
.... 
    @Any(metaColumn = @Column(name = "TYPE"), fetch = FetchType.LAZY)
    @AnyMetaDef(idType = "long", metaType = "string", metaValues = { @MetaValue(targetEntity = Item1.class, value = "I1") })
    @JoinColumn(name = "TYPE_ID")
    private IItem item;
...
}

(Item1 实现 IItem)

这是我的 DAO:

@Override
@SuppressWarnings("unchecked")
public List<ItemContainer> getItem1s(final Long containerId) {
return getHibernateTemplate().findByNamedQueryAndNamedParam("GET_ITEMS", new String[] { "containerId", "clazz" },
        new Object[] { containerId, Item1.class });
}

这是不可能的,还是我遗漏了一些东西?

谢谢

Using hibernate @Any mapping to map multiple entities in the same, I haven't be able to build a request to fetch all the objects of a specific class.

Hibernate doesn't seem to be capable to handle this kind of request.

My application doesn't start and throw the following error :

Invocation of init method failed; nested exception is java.lang.ClassCastException: org.hibernate.type.AnyType cannot be cast to org.hibernate.type.ComponentType

removing the part "left join fetch ic.item i" in the request solve this error.

For example, I've got the following model:

@Entity
@NamedQueries({
@NamedQuery(name = "GET_ITEMS", query = "from ItemContainer ic left join fetch ic.item i where ic.id=:containerId and ic.class=:clazz")})
public class ItemContainer {
.... 
    @Any(metaColumn = @Column(name = "TYPE"), fetch = FetchType.LAZY)
    @AnyMetaDef(idType = "long", metaType = "string", metaValues = { @MetaValue(targetEntity = Item1.class, value = "I1") })
    @JoinColumn(name = "TYPE_ID")
    private IItem item;
...
}

(with Item1 implements IItem)

And here is my DAO :

@Override
@SuppressWarnings("unchecked")
public List<ItemContainer> getItem1s(final Long containerId) {
return getHibernateTemplate().findByNamedQueryAndNamedParam("GET_ITEMS", new String[] { "containerId", "clazz" },
        new Object[] { containerId, Item1.class });
}

Is it just impossible, or am I missing something ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸢与 2024-11-24 16:17:46

我也遇到了类似的问题,经过一番苦思冥想,我发现 Hibernate 期望的“类”是一个类标识符,在本例中是鉴别器值

因此,使用上面的示例,解决方案是查询类属性为 I1:

@Override
@SuppressWarnings("unchecked")
public List<ItemContainer> getItem1s(final Long containerId) {
return getHibernateTemplate().findByNamedQueryAndNamedParam("GET_ITEMS", new String[] { "containerId", "clazz" },
        new Object[] { containerId, "I1" });
}

I had a similar issue, and after much headbanging I discovered that the "class" Hibernate was expecting was a class identifier, in this case the descriminator value.

So using the above example, the solution was to query the class property as being I1:

@Override
@SuppressWarnings("unchecked")
public List<ItemContainer> getItem1s(final Long containerId) {
return getHibernateTemplate().findByNamedQueryAndNamedParam("GET_ITEMS", new String[] { "containerId", "clazz" },
        new Object[] { containerId, "I1" });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文