users@glassfish.java.net

Re: Entities refreshing in JPA 2.0 in GlassFish V3

From: Mitesh Meswani <Mitesh.Meswani_at_Sun.COM>
Date: Tue, 02 Mar 2010 18:13:33 -0800

On 3/2/2010 4:29 AM, glassfish_at_javadesktop.org wrote:
> I have a relation (in Derby 10.5.3.0 ) like Order --> OrderLine.
>
> In Order is
>
> @Basic(fetch= FetchType.EAGER )
> public Collection<OrderLines> getOrderLinesCollection() {
> if ( orderLinesCollection== null )
> orderLinesCollection= new ArrayList();
> return orderLinesCollection;
> }
>
> The problem is
>
> a) If new OrderLine is added to db separately like.
>
> em.persist(orderLine)
> em.refresh(orderLine)
>
> and then Order is fetched like
> SELECT o FROM Order o WHERE o.id = :orderId "
>
> the new ordeline is not in orderlines collection.
>
> WHY ?
>
JPA spec requires you to maintain both side of a relationship. You are
getting the cached order entity at this point which does not know about
OrderLine added to it externally. You can instruct the provider to
refresh the instance while executing a query by specifying a provider
specific hint. For if you are using EclipseLink, you can use
http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#Refresh
<http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#Refresh>

> => How the new orderline can be refreshed in EntityManager ?
>
> b) If new OrderLine is added thru Order like
>
> order.getOrderLinesCollection.add(currentWire);
> getFacade().edit(order);
>
> Now the query presented up finds the new orderline just
> inserted to database.
> [Message sent by forum member 'steelyx2' (sakari.isoniemi_at_tieto.com)]
>
> http://forums.java.net/jive/thread.jspa?messageID=389604
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>