I also have two entities, “Child1” and “Child2”, both extending the “Parent” entity. “Child1” has an additional field called “thirdField,” and “Child2” has an additional field called “fourthField.”
These entities are stored in the same database table, where all four fields are present.
I would like to create a database query using a repository (or DataManager, if applicable). Initially, I want to query the “Parent” entity using a request like “SELECT p FROM Parent p WHERE firstField = :value.” In the future, I would like to cast the retrieved “Parent” object to either “Child1” or “Child2” and access the specific fields of each child entity.
However, when I attempt to do this, I encounter an exception stating: Caused by: java.lang.IllegalStateException: Cannot get unfetched attribute [thirdField] from detached object com.example.entity.Child1-ad5ba90f-1a28-6faf-4bb0-2b0aa22819fc [detached].
Can you please help me understand how to achieve this functionality?
However, the main goal is to optimize the code and minimize the number of database queries. Currently, I have implemented a solution that requires separate repositories for each entity and performs individual queries to the database for each of them. Unfortunately, this approach still involves three separate requests, which does not fully address my requirement for a single-database request solution.
Therefore, I am still seeking a solution that can achieve the desired functionality with a single query to the database.