persistence@glassfish.java.net

Re: JPA issue - not able to view an entity's collection

From: Mitesh Meswani <Mitesh.Meswani_at_Sun.COM>
Date: Fri, 18 Jul 2008 18:38:05 -0700

Toplink gives you the object from second level cache. This
(http://weblogs.java.net/blog/guruwons/archive/2006/09/understanding_t_1.html)
might help you understand it better.

lwinkenb wrote:
>
> Mitesh Meswani wrote:
>
>> You have to set the relationship from both sides. That is add new
>> attribute to the list of attributes from device.
>>
>>
>
> but even if I try and get the Device again, the attribute will not show up.
> So for instance:
>
> Device device = new Device();
> EntityManager em = emf.createEntityManager();
> EntityTransaction tx = em.getTransaction();
> tx.begin();
> em.persist(device);
> tx.commit();
> em.close();
> // ...
> Attribute attribute = new Attribute();
> attribute.setDevice(device);
> EntityManager em = emf.createEntityManager();
> EntityTransaction tx = em.getTransaction();
> tx.begin();
> em.persist(attribute);
> tx.commit();
> em.close();
>
> Device device2;
> EntityManager em = emf.createEntityManager();
> EntityTransaction tx = em.getTransaction();
> tx.begin();
> device2 = (Device)em.find(Device.class, device.getPrimaryKey());
> tx.commit();
> em.close();
>
> In this case device2.getAttributeCollection() returns a size of zero. I
> don't understand how this is possible. If I look in the database, the
> attribute exists there. For some reason TopLink never does a SELECT from
> the attribute table.
>