I have a single table which is generated from two jpa entities (inheritance). So we have a DTYPE column generated in the database table which has entity name has values.
How do I write a jpa query to fetch data for any given entity. i.e DTYPE = 'A'?
I just tried using DTYPE in jpa query but as expected I got an error "Could not resolve property". Please let me know how to write this query.
Note - I dont want to use native query.
Update:
I have class A and subclass AA which extends A. DTYPE has two values A and AA.
When I query for A, I get all the A plus AA entities. How to exclude AA when I query for A?
Simply add a simple field dtype in your parent class.
@Column(insertable = false, updatable = false) private String dtype;
And so, you can use it in your JPQL query
Assuming you have three entities:
where the last two are the two entities you store with a discriminator column, your JPA-QL query should be as simple as:
select p from Person p
to get all people. JPA is smart enough to figure it out. Likewise you can do:
select p from Party p
and get all of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With