Reputation: 419
I have a entity called Test.java with 70 fields, some of the fields are related to other entities also(through join one to many, many to one, etc assosiations).
I need to have an object of this type but I don't need all the fields just 3 fields out of them one field has foreign key relation relation with other entity.
So, I wrote a native query while executing the query jpa executing other queries also causes taking so much time to complete.
Here is my entity class:
public Class Test implements Serializable{
@Id
private int id;
@ManyToOne
@JoinColumn(name = "STATUSID")
private xxxxx xxxxx;
/// other fields, getters and setters
}
Here is my native query
public List<Order> getAllOpenOrders() {
final String query = "SELECT t.* from T_TEST";
Query createNativeQuery = em.createNativeQuery(query, Test.class);
List<Order> resultList = createNativeQuery.getResultList();
return resultList;
}
How can I get the Order entity object with selected fields, I don't need other fields means I am using only these three fields for my functionlity.
How can I stop other queries execution?
Thank you.
Upvotes: 1
Views: 1218
Reputation: 2843
You can select only those columns you need using Projections
. You do not have to resort to Native Queries for this.
As far as I remember (I had debugged a long time ago), hibernate does a flush before executing a native query. This results in executing all pending queries as well, right before executing the native query. This might be the case in your situation where you are seeing some other queries.
You can use Projections
, which are a bit cleaner way to select only required columns.
As answered here : https://stackoverflow.com/a/11626877/564503
Upvotes: 1