Jpa-2.0 – JPA2 criteria builder – query on abstract classes and multiple subclasses
First of all, I'm sorry if I've asked this question, but I can't find any similar questions or answers to my questions
My problem is that I have several subclasses that inherit from a subclass at several hierarchy levels
@Entity public class A{ } @Entity public class B extends A { ... } @Entity public class C extends A{ ... } @Entity public class D extends C { private String someAttribute; } @Entity public class E extends C { private String anotherAttribute; }
I need to process queries on C, get all entities from C, D and e according to my conditions, and access attributes from D and E
I noticed that it is impossible to access, for example, someattribute from D to execute the query on C, as shown below:
Root root = query.from(C.class); Path p = root.get("someAttribute"); Path p2 = root.get("anotherAttribute");
Please note that I cannot use the metamodel at this time
In jpaql, I will write something similar:
`select e1 from C eq where someAttribute = .... or anotherAttribute = ....`
It will solve my hierarchy correctly
To solve this problem, I created my own annotation, which is equivalent to @ xmlseealo and named @ persistenceseealo. It tells me which subclasses I must find to find my properties Therefore, when I deal with my hierarchy conforming to @ persistenceseealso and get my path, I need to create a new root element for each subclass, and I find my attributes
The main problem here is query Form (clazz) creates a join on the query, which completely confuses my query, but I need to use the root element on my type to resolve the path
So my question is; Is there any way to use JPA2 criteriabuilder to handle multiple subclass selection without creating a new root instance? You may use EntityType?
Or did I do something wrong?
Thank you very much in advance!
Best wishes, Q
Solution
Sorry for the long wait!
This is a good question I have always seen The problem is that you are assuming information about subtypes of C, which is not entirely correct
If you use the per class table or join table inheritance policy, the data of D and E are stored in a separate table and select E1 from C EQ, where someattribute = Or otherattribute = Does not work because these columns do not exist in C
Before filtering, you need to left click the columns in D and e to connect to C (I answered the similar question before, which should be helpful