0

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?

3
  • Do you have the same values of id fields in these tables? Commented Oct 18, 2011 at 19:38
  • not really possible to answer this without seeing everywhere else that rows is used. For example, why isn't rows declared within the for loop? Commented Oct 18, 2011 at 19:41
  • @axtavt yes all tables have same ids and same column names except for one column, rowText, this contains the text in different languages depending on the transalation table. Commented Oct 18, 2011 at 20:05

2 Answers 2

2

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:

  • rethink your database shema
  • construct objects from query result manually
  • forcibly clear the session cache by calling clear() or detach()
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the help, I can't change db schema, entityManager.clear() fixed the issue.
@user908452: Note that clear() removes all entities from the session cache, that may create unexpected side effects if some of these entities was loaded by different methods. Perhaps it would be better to use detach() to explicitly remove only the entities you just loaded.
0

Are you sure that the rows variable doesn't actually contain the LAST of your returned results? how do you initialize rows and what do you do with the variable inside the loop? If you want to get all the results from all the queries you have to add each one (inside the loop) to a list/set variable declared BEFORE the loop starts.

Or you could use some proper SQL and do:

SELECT * FROM Table1 [JOIN/UNION] Table2 etc...

1 Comment

I am sure the rows variable does NOT contain the last data, I was initializing it outside of the loop - but I tried to initializing it inside of the loop too and still it didn't work. I iterate over the rows ArrayList and add it to a Map of a Map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.