Hi!
I'm developing a JPA based app where I have the following model:
It represents a dynamic data model where I define a Type which is composed of some TypeField. Each TypeField represents a data type definition (contains attributes like name, description, etc.) which can be of any kind (String, Date, Boolean). Then I have some nodes which represent instances of Types. So each Node is composed of NodeFields which can be Strings, Dates, etc. NodeField instances contains the data represented by each TypeField.
I need to query Nodes which match some criterias, where a criteria consist of some constraint applied to each field it is composed of. For example, one query could be "Give me every node which field with ID X of String type contains Y value and which field with ID Z of Date type contains W value.
I've came up with following JPA query:
Code:
SELECT
FROM Node n JOIN n.fields f, StringNodeField sf WHERE sf = f AND f.typeField.id = X AND sf.data LIKE Y AND n IN
( SELECT FROM Node n JOIN n.fields f, DateNodeField sf WHERE sf = f AND f.typeField.id = Z AND sf.data = W )
As you can see, I build multiple partial queries which apply each field constraint and then I merge the results with the IN clause. This works but I wonder If there is a simpler wath to express the query withouth involving a subquery for each field constraint. I need to include the concrete NodeField type (StringNodeField, DateNodeField and so on) because the data is stored in the concrete NodeField and is a different Java type in each case (java.lang.String, java.util.Date, etc.)
Thank you for your help!
Daniel.