Thanks Kevin, You immediately showed me the right direction! CascadeType.REFRESH solved my problem. I was thinking that my refreshing the root object User then when calling getHobbyList() I would also get a fresh version of each Hobby. I implemented the CascadeType.REFRESH and now it works.
Kevin Sutter wrote > If you are calling EM.refresh() directly, you should get a refreshed > version of the entity that is committed to the database. Any > relationships > to other entities will only be refreshed if you are using the > CascadeType.REFRESH (or ALL) specified on your relationship [1]. The > other > thing that can affect refreshing is the use of the L2 data cache. Any > chance you have that enabled? A copy of your persistence.xml is always > helpful with understanding your environment. Thanks. > > [1] > http://ci.apache.org/projects/openjpa/trunk/docbook/manual.html#jpa_overview_meta_cascade > > > On Thu, May 22, 2014 at 10:22 AM, sim085 < > sim085@ > > wrote: > >> Hello, >> >> I am working on a web project where the underlying database can get >> updated >> by an other systems. My scenario is as follows: I have the following >> tables; >> USERS and HOBBY. I mapped a User object with the USERS table and a Hobby >> object with the HOBBY table. A User can have one ore more Hobby so I have >> a >> List > <Hobby> > in User mapped using the annotation @OneToMany. A Hobby can be >> linked with only one User so I use the annotation @ManyToOne from Hobby. >> >> My problem, as indicated int he first sentence, is that the HOBBY table >> may >> be updated through another system (outside of JPA world). When this >> happens, >> when I call User user = em.find(1) the object I get does not reflect the >> real data there is in the the database but the same data I had for this >> User >> as it was when I last interacted with the EntityManager for this >> particular >> user. >> >> In other words, if User 1 had four Hobby and the other system add another >> two Hobby to User 1 then when I go to the screen that shows me all Hobby >> for >> User 1 I get four because last time I got User 1 using JPA this User had >> four Hobby! >> >> I went to the Java API for the EntityManager and I found two methods: >> >> *refresh(java.lang.Object entity) * >> /Refresh the state of the instance from the database, overwriting changes >> made to the entity, if any./ >> >> *flush() * >> /Synchronize the persistence context to the underlying database./ >> >> I tried them both but none are working for me. Does anyone know what I am >> doing wrong? >> >> >> >> >> -- >> View this message in context: >> http://openjpa.208410.n2.nabble.com/Why-is-EntityManager-refresh-not-working-for-me-tp7586653.html >> Sent from the OpenJPA Users mailing list archive at Nabble.com. >> -- View this message in context: http://openjpa.208410.n2.nabble.com/Why-is-EntityManager-refresh-not-working-for-me-tp7586653p7586655.html Sent from the OpenJPA Users mailing list archive at Nabble.com.