hibernate @Any 映射,无法获取“任何”映射物品
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也遇到了类似的问题,经过一番苦思冥想,我发现 Hibernate 期望的“类”是一个类标识符,在本例中是鉴别器值。
因此,使用上面的示例,解决方案是查询类属性为 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: