users@glassfish.java.net

Re: Style question about returning JPA entities from EJB

From: <glassfish_at_javadesktop.org>
Date: Mon, 12 Nov 2007 13:03:29 PST

> This reminds me of an other related question. My web
> project uses EJB
> local session beans. Since they are not remote there
> are no stubs

Actually, in Glassfish there are no developer-visible stubs in the Remote case either. If the caller and target EJB are in the same .ear, there is no difference in terms of the packaging requirements between the Local EJB view and the Remote EJB view.

>, and
> therefore my web project has a build dependency on
> the EJB project. I
> can't think of an other way to use a local EJB
> without having a build
> dependency on the EJB project. Is that correct?

All that matters is that the calling component has visibility to the classes it needs to invoke the EJB. At minimum, that means the Remote or Local interface itself, but it would also include any supporting classes or interfaces. If the caller is in a web application, those .class files can be put in WEB-INF/classes, in a .jar file in WEB-INF/lib, or refactored into a library .jar at the top-level of the .ear.


> If
> that's the case,
> then JPA entities could live in the EJB project
> instead of a separate
> shared JAR.
>

Where you package the entity classes and where you define the PU are two different issues. If the entities are used by more than one module it's much cleaner to refactor and package the classes once at the top-level of the .ear. Technically, if the web application doesn't actually use the EntityManager API it doesn't need visibility to the persistence-unit, so it would be ok to define the PU in the ejb-jar itself. However, if that's likely to change you could just define the PU at the top-level of the .ear.

BTW, we're paying a lot of attention to the combined web/EJB case in EJB 3.1. We'll be adding requirements that allow EJBs to be defined directly in the .war, so that the web->ejb->JPA use-case can be fully realized with only a stand-alone .war and no ejb-jar / .ear.

>
> Thanks,
> Ryan
>
> Ryan de Laplante wrote:
> > That makes sense, I forgot that JPA can be used in
> Java SE. I thought
> > that the persistence.xml must be in an EJB jar or
> WAR file.
> >
> > Thanks,
> > Ryan
> >
> > Marina Vatkina wrote:
> >> Normally it will leave with the entities, which
> will also allow you
> >> to test your PU outside the container. But if you
> have some reasons
> >> to exclude it, it can be placed with the component
> (with the
> >> <jar-file> element (see the spec) "specified
> relative to the root of
> >> the persistence unit (e.g., utils/myUtils.jar).".
> >>
> >> Regards,
> >> -marina
> >>
> >> Ryan de Laplante wrote:
> >>> Thank you. The persistence.xml would still live
> inside of the EJB
> >>> project, right?
> >>>
> >>> Thanks,
> >>> Ryan
> >>>
> >>> Marina Vatkina wrote:
> >>>
> >>>> I think your best option would be to use a
> separate jar for the
> >>>> entities that is placed under <ear>/lib
> directory.
> >>>>
> >>>> Regards,
> >>>> -marina
> >>>>
> >>>> Ryan de Laplante wrote:
> >>>>
> >>>>> I'm not sure where else to ask a question like
> this.
> >>>>>
> >>>>> Back in the EJB 2.x days with CMP, the CPM
> entities were not
> >>>>> returned by session beans. The entity would be
> converted into
> >>>>> POJO value objects before being returned. I
> used to put the value
> >>>>> object POJOs in a common jar file that the EJB
> project and EJB
> >>>>> client project would depend on.
> >>>>> In the Java EE5 world, I have been returning
> the actual JPA
> >>>>> entities from my EJB3 session beans. Since the
> JPA entity classes
> >>>>> live inside of the EJB jar file, the
> persistence.xml lives in
> >>>>> there too, and my web project has a build
> dependency on the EJB
> >>>>> project. This hasn't been a big deal for me
> since I use EJB
> >>>>> Local, but I've always wondered if this is the
> right way to do it.
> >>>>>
> >>>>> Can anyone share with me the Java EE5 patterns
> you use, or links
> >>>>> to pages that talk about this?
> >>>>>
> >>>>>
> >>>>> Thanks,
> >>>>> Ryan
>
> ------------------------------------------------------
> ---------------
> To unsubscribe, e-mail:
> users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail:
> users-help_at_glassfish.dev.java.net
[Message sent by forum member 'ksak' (ksak)]

http://forums.java.net/jive/thread.jspa?messageID=245111