如何使用 Hibernate 注释有条件地映射集合?

发布于 2024-11-29 02:41:13 字数 486 浏览 3 评论 0原文

如果我有一个如下所示的类结构(删除注释):

public class Person {
  String store;

  Swatch swatch;

  Collection<Paint> paints;
}

public class Swatch {
  String color;
}

public class Paint {
  String color;

  String store;
}

Store 永远不会为 null,并且是 Person 和 Paint 之间使用的键。目标是将所有与 Swatch 具有相同颜色的 Paint 记录与与 Person 位于同一商店的所有 Paint 记录进行匹配。但是,Swatch 可能为 null 或者它的颜色属性可能为 null(这两种情况都返回与 Person 具有相同存储的所有 Paint)。

是否有基于 JPA 或 Hibernate 的注释可以在 Paints 集合上构造此查询?

If I have a class structure which looks like the following (annotations removed):

public class Person {
  String store;

  Swatch swatch;

  Collection<Paint> paints;
}

public class Swatch {
  String color;
}

public class Paint {
  String color;

  String store;
}

Store is never null and is the key used between Person and Paint. The goal is to match all Paint records with the same color as the Swatch with the same store as the Person. However, the Swatch may be null or its color attribute may be null (in both cases return all Paints with the same store as the Person).

Is there a JPA or Hibernate based annotation that will construct this query on the paints collection?

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

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

发布评论

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

评论(1

笑着哭最痛 2024-12-06 02:41:13

我的结论是,这对于 Hibernate 注释来说要么是不可能的,要么只是不太明显。

我的解决方案相当黑客:在 Person 上,我有一个 Transient 注释方法,可以以编程方式过滤绘画。不太理想,因为它可能会强制执行额外的查询来获取 Swatch 实体。

我不会将此标记为正确,因为我认为 Vineet Reynolds 创建查询的答案在我提出的情况下可能是最合理的。然而,由于未提及的因素,我认为这些因素与我的示例无关,并且提及它们会显着增加复杂性,因此在我的实际场景中很难实现查询。

I concluded that this is either impossible with Hibernate annotations or perhaps just not very obvious.

My solution was rather hackish: on Person I have a Transient annotated method which filters paints programmatically. Less than ideal because it may force an additional query to grab the Swatch entity.

I will not mark this as correct as I think Vineet Reynolds' answer to create a query is probably most reasonable in the case I presented. However, a query is harder to implement in my real scenario due to unmentioned factors which I thought were irrelevant to my example and mentioning them would have added significant complexity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文