Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate filternig list of objects by its subclass specific fields

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.

like image 799
0lukasz0 Avatar asked Feb 03 '26 06:02

0lukasz0


1 Answers

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.

like image 131
Pieter Avatar answered Feb 04 '26 19:02

Pieter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!