I am using a query to return a list of records.
Look at that scenario:
I change record X but do not persist/flush it!
Then I make a query by entityManager.createNamedQuery(..).
This query does contain the origin record X, I call it X1.
The problem here is, that the new retrieved record X1 has the same state as the transient X. How can I avoid this?
I do want to retrieve the records by its origin state even there are records in transient state which might to be changed by me.
When using flush or refresh the record X is also be refreshed/flushed.
Is the only solution to copy the record X to another object-instance?
In your case, since X already exists, it is not transient but managed and the behaviour is as intended. When you change an entity you don't have to call em.persist() and em.flush() explicitly. Read section 3.2
Entity Instance’s Life Cycle of the EJB persistence spec or any reference on how entities are managed.