I am trying to run native query in a loop, the query displays the correct sql syntax but the output is always the same.
for (int i=0; i<translations.size(); i++) {
Query query = entityManager.createNativeQuery("Select * from " + translations.get(i).getName(), MyModel.class);
rows = (List<MyModel>)query.getResultList();
// rest of the function...
}
now in the console I can see the Hibernate statements like:
Hibernate: Select * from translation1
Hibernate: Select * from translation2
Hibernate: Select * from translation3
but the variable “rows” always contains the result of the first select statement i.e. rows of translation1 table.
Any ideas why in the console it shows that it is selecting from other tables too but in reality it always gets data from translation1 table?
If all tables have the same set of ids, it’s an expected behaviour.
Hibernate session cache guarantees that there can be only one instance of an entity of a particular type with a particular id inside a session. Since entities are resolved via the session cache even in the case of a native query, you get the same instances.
So, you have several options:
clear()ordetach()