Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphic JPA Query

Can someone please give me an example of a JPA query which select few selected sub-classed entities? For example, the parent entity is Institution. I have sub-classed it to Manufacturers, Suppliers, Service Providers. I want to get all the Suppliers and Service Providers, but not Manufacturers in a single query which return Institution objects.

I googled, but could not locate any resource addressing this specific issue. Thanks in advance.

like image 620
Buddhika Ariyaratne Avatar asked Jun 19 '26 22:06

Buddhika Ariyaratne


1 Answers

from the JPA 2.0 Spec:

4.6.17.4 Entity Type Expressions [...] The Java class of the entity is used as an input parameter to specify the entity type. Examples:

SELECT e FROM Employee e WHERE TYPE(e) IN (Exempt, Contractor)

SELECT e FROM Employee e WHERE TYPE(e) IN (:empType1, :empType2)

[...]

so I'd try something like:

List<Institution> institutions = em.createQuery("SELECT i FROM Institution i WHERE TYPE(i) IN ( Supplier, ServiceProvider ) ).getResultList();
like image 159
Korgen Avatar answered Jun 21 '26 12:06

Korgen



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!