users@glassfish.java.net

Re: name, mappedName, unitName... getting confused

From: Sanjeeb Kumar Sahoo <Sanjeeb.Sahoo_at_Sun.COM>
Date: Thu, 16 Nov 2006 09:03:29 +0530

Hi Antonio,

1) For @Stateless or @Stateful, *name* attributes defines the name of
the EJB which is same as <ejb-name> in ejb-jar.xml. The default value
for this is the unqualified name of the EJB class.
2) mappedName when supported by a container can be used to specify a
product specific name which, in case of GlassFish, is the global JNDI
name of the EJB. You have observed this behavior.
The values supplied by name and mappedNamethese can be used in EJB
clients while using @EJB or equivalent constructs. If your client is
packaged in the same application as the EJB, then it can refer the EJB
using *name* of the EJB, else it should refer the EJB using the global
JNDI name.

3) For @PersistenceContext, *name* is the name of the PersistenceContext
reference. It is the name by which the entity manager is made available
in the component's context. It is used when user looks up the entity
manager using JNDI API. If user is injecting an EntityManager, then it
is not required.
4) As you have observed, unitName is the name of the persistence unit
that is used to create the entity manager.

Hope this helps. Please take a look at chapter #5 of the Java EE 5
platform spec where this is very nicely explained.

Thanks,
Sahoo

Antonio Goncalves wrote:
> Hi everybody. I'm getting a bit confused with some EJB annotations
> attributes.
>
> The first one is @Stateless (or @Stateful). What is the name attribute
> used for ? In the specification it says that it's the name of the EJB.
> But on the other hand, when I change it nothing happens but when I use
> the mappedName attribute (which is vendor specific), it really changes
> the name of my EJB in the JNDI tree. For example :
>
> @Stateless(mappedName = "ejb/stateless/Catalog")
> public class CatalogBean implements CatalogRemote, CatalogLocal {}
>
> This changes the name of the EJB to "ejb/stateless/Catalog". But if I
> use the attribute name, the name doesn't change.
>
>
> The other one is @PersistenceContext. The name attribute doesn't lead
> me anywhere (I can't see any difference in using it or not) ant the
> other is unitName that has to be equal to the persitent unit name
> within the persistence.xml file. The spec doesn't talk about any
> vendor specific attribute but I don't understand what the name
> attribute can be used for.
>
> Does any body has clearer ideas than I have ?
>
> Thanks,
>
> Antonio