data « EntityManager « JPA Q&A

1. Running a nativeQuery in a loop doesn't return correct data

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(); ...

2. entityManager.merge and already exists data

3. Hibernate EntityManager gives stale data randomly

I've done some more debugging and one option that I tried was to call EntityManager.clear() in @PostConstruct method of all SFSBs that use the injected EntityManagers. This indeed solved the problem of getting stale data from session cache, but it also introduced another problem: when using multiple DAOs in one (http) request (I'm using entitymanager-per-request pattern), looking up (JNDI) additional (more ...