2

使用休眠@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 });
}

这是不可能的,还是我错过了什么?

谢谢

4

1 回答 1

0

我有一个类似的问题,经过多次头疼后,我发现 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" });
}
于 2011-11-25T10:06:33.487 回答