Hibernate 通过其子类特定字段过滤对象列表
我有一个 Java 超类 - Message - 和 3 个子类 - Problem、Report、Update。对于这 4 个类中的每一个,我都有一个单独的控制器(列表器),它使用 Hibernate 从数据库获取对象。此外,每个控制器都添加了一些特定于其所使用的类的过滤标准(限制)。 ListMessages 可以为 Message 类中的字段添加条件,ListProblems 可以为 Message 字段和 Problem 特定字段添加条件。但是有没有办法使用子类中的字段来过滤消息?
例如,消息有字段内容,问题有状态,报告有拒绝原因(所有字符串)。 我可以按内容过滤所有消息,按内容和状态过滤所有问题,按内容和拒绝原因过滤报告。但是有没有一种方法可以按内容、状态和拒绝原因过滤消息呢? 我需要这个,因为我想要一个所有消息的列表,但能够过滤所有实际上是与过滤模式匹配的状态问题(或带有拒绝原因的报告)的消息。 不幸的是,我无法从数据库获取消息并在 Java 代码中过滤它们 - 它们需要在数据库端进行过滤。
I have one Java superclass - Message - and 3 children classes - Problem, Report, Update. For each of those 4 classes I have a separate controller (lister) which uses Hibernate to get objects from database. Additionally, each of those controllers is adding some filtering criteria (Restrictions) specific for the class it is using. ListMessages can add criteria for fields in Message class, ListProblems can add criteria for Message fields and Problem specific fields. But is there any way of filtering Messages using fields from children classes?
For example, Message has a field content, Problem has status, Report has refusalReason (all Strings).
I can filter all Messages by content, Problems by content and status, Reports by content and refusalReason. But is there a way of filtering Messages by content, status and refusalReason?
I need this because I want to have a list of all Messages, but to be able to filter all Messages that are in fact Problems with status (or Reports with refusalReason) that matches the filtering pattern.
Unfortunately I cannot get messages from database and filter them in Java code - they need to be filtered on db side.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道你使用的是哪种休眠查询语言,所以我只是笼统地回答这个问题。如果您发布您的查询(语言),我会看看是否可以用示例更新问题。
您所要做的就是使用左外连接来包含子类,并对每个连接施加额外的限制以包含您想要的过滤。这应该会给您留下所需的消息。
I don't know which hibernate query language you're using, so I will just answer the question in general. If you post the query (language) you have, I'll see if I can update the question with an example.
What you have to do is use left outer joins to include the subclasses and put an additional restriction on each join to include the filtering you want. This should leave you with the desired messages.