跨连接子类问题进行限制条件查询
我有下图:
OrderLine
OrderLineExtension
OrderLineExtensionA
OrderLineExtensionB
OrderLineExtensionC
OrderLine 包含一组 OrderLineExtension。
OrderLineExtension 定义为: @Inheritance(strategy = InheritanceType.JOINED) 并被标记为实体并且是抽象的
在数据库中为层次结构中的每个类创建一个表。
我正在尝试执行查询,其中 OrderLine 上的商品名称为“item”,但 OrderLineExtensionA 中的 orderReferenceNumber 为“orderRef”。
我不确定如何从 OrderLine 遍历到 OrderLineExtensionA,因为没有从 OrderLineExtension 到 OrderLineExtensionA 的 java 引用。
到目前为止我已经得到了这个,当然这是行不通的。
filter=DetachedCriteria.forClass(OrderLine.class);
.add(Restrictions.eq("item", "item"));
.createCriteria("orderLineExtension")
.add(Restrictions.eq("orderReferenceNumber", "orderRef"));
这会失败,因为 hibernate 抱怨它在 orderLineExtension 类中找不到 orderReferenceNumber ,这在 orderLineExtensionA 中是正确的。但是,我如何遍历到扩展呢?我无法嵌套另一个 createCriteria,因为超类中没有对子类的引用。
任何帮助将不胜感激。
I have the following graph:
OrderLine
OrderLineExtension
OrderLineExtensionA
OrderLineExtensionB
OrderLineExtensionC
OrderLine contains a Set of OrderLineExtension.
OrderLineExtension is defined as : @Inheritance(strategy = InheritanceType.JOINED) and is marked as an entity and is abstract
A table is created in the db for each of these classes in the hierarchy.
I'm trying to perform a query where the item name on OrderLine is "item", but where the is orderReferenceNumber "orderRef" in OrderLineExtensionA.
I'm not sure how to traverse from OrderLine to OrderLineExtensionA as there is no java reference from OrderLineExtension to OrderLineExtensionA.
I've so far got this which of course does not work.
filter=DetachedCriteria.forClass(OrderLine.class);
.add(Restrictions.eq("item", "item"));
.createCriteria("orderLineExtension")
.add(Restrictions.eq("orderReferenceNumber", "orderRef"));
This fails as hibernate complains that it cannot find orderReferenceNumber in class orderLineExtension which is true as it's in orderLineExtensionA. But, how do I traverse to the extension? I cannot nest another createCriteria as there is no reference in the superclass to the subclass.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀,别担心。之所以提出这个问题,是因为我没有意识到同事在每个加入的子类中创建了相同的字段。 Hibernate 只会去第一个子类来加载该属性。这就是为什么我得到了错误的实例。
现在我们已经重命名了它们,一切正常。虽然我的头挠了一段时间。
Oops, don't worry. This problem is thrown up because I did not realise that a collegue had created the same field in each joined subclass. Hibernate will just go the the first subclass to load the property. This why i was getting the wrong instance back.
Now we have renamed them, it all works fine. Had my head scratching for a while though.