You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@deltaspike.apache.org by Rodrigo Uchôa <ro...@gmail.com> on 2015/01/02 18:02:42 UTC

Setting the persistence-unit name declaratively with @PersistenceContext annotation

Hi everyone!

I'm developing an API, a simple utility jar, for persistence (generic)
operations. The lib itself does not provide any persistence.xml file, it
takes into account that all its clients will have one to configure their
own persistence unit.

Inside my API I can't make assumptions about the persistence unit name for
the clients, so I'm simply doing something like this:

*public class MyGenericDAO {*
*    @Inject*
*    EntityManager em;*
*}*


... And every client who depends on the API will have a "Resource Producer":

*public class ResourcesProducer {*
*    @Produces*
*  @PersistenceContext( unitName = "some-pu")*
*    private EntityManager entityManager;*
*}*


This works as expected as long as each client have a single persistence
unit configured. The injection point in MyGenericDAO will resolve to
"some-pu" because there's only one. But if there are multiple persistence
units it won't work.

This was easy to solve with Seam (and also Spring) as we could
declaratively initialize any bean (component) property. As far as my
knowledge on CDI goes, this can't be done with CDI/JavaEE alone. So I was
wondering with Deltaspike has any feature to help with this issue.

Regards,

Re: Setting the persistence-unit name declaratively with @PersistenceContext annotation

Posted by Gerhard Petracek <ge...@gmail.com>.
hi rodrigo,

daniel was talking about something similar to [1].
just inject multiple entity-mangers (via @PersistenceContext) in the class
of your producer.
in a producer-method you can inspect e.g. the injection-point (see [1]) or
use any other logic to select and return one of the available
entity-managers.

regards,
gerhard

[1]
https://github.com/apache/deltaspike/blob/deltaspike-project-1.2.1/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerFactoryProducer.java#L74

http://www.irian.at

Your JavaEE powerhouse -
JavaEE Consulting, Development and
Courses in English and German

Professional Support for Apache
MyFaces, DeltaSpike and OpenWebBeans

2015-01-02 19:10 GMT+01:00 Rodrigo Uchôa <ro...@gmail.com>:

> Daniel,
>
> That would create an *application managed* EntityManager, wouldn't it? It
> seems like an alternative, but I hope there's a more elegant approach that
> would not prevent me from using a *container managed* EntityManager.
>
> On Fri, Jan 2, 2015 at 3:29 PM, Daniel Cunha <da...@gmail.com> wrote:
>
> > Can injectionPoint fix it?
> >
> > You can create your annotation where you set the unitName.
> > So, you set the unitName.
> > In your produce, get the InjectionPoint, read the annotation and set
> > unitName in Persistence.createEntityManagerFactory(...);
> > So, you can create the entityManger with this EntityManagerFactory and
> > specific unitname.
> >
> > I think that can be a way.
> >
> >
> >
> http://docs.oracle.com/javaee/7/api/javax/persistence/Persistence.html#createEntityManagerFactory(java.lang.String)
> >
> >
> http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManagerFactory.html#createEntityManager()
> >
> >
> http://docs.oracle.com/javaee/7/api/javax/enterprise/inject/spi/InjectionPoint.html#getAnnotated()
> >
> >
> http://docs.oracle.com/javaee/7/api/javax/enterprise/inject/spi/Annotated.html#getAnnotation(java.lang.Class)
> >
> > On Fri, Jan 2, 2015 at 5:02 PM, Rodrigo Uchôa <ro...@gmail.com>
> > wrote:
> >
> > > Hi everyone!
> > >
> > > I'm developing an API, a simple utility jar, for persistence (generic)
> > > operations. The lib itself does not provide any persistence.xml file,
> it
> > > takes into account that all its clients will have one to configure
> their
> > > own persistence unit.
> > >
> > > Inside my API I can't make assumptions about the persistence unit name
> > for
> > > the clients, so I'm simply doing something like this:
> > >
> > > *public class MyGenericDAO {*
> > > *    @Inject*
> > > *    EntityManager em;*
> > > *}*
> > >
> > >
> > > ... And every client who depends on the API will have a "Resource
> > > Producer":
> > >
> > > *public class ResourcesProducer {*
> > > *    @Produces*
> > > *  @PersistenceContext( unitName = "some-pu")*
> > > *    private EntityManager entityManager;*
> > > *}*
> > >
> > >
> > > This works as expected as long as each client have a single persistence
> > > unit configured. The injection point in MyGenericDAO will resolve to
> > > "some-pu" because there's only one. But if there are multiple
> persistence
> > > units it won't work.
> > >
> > > This was easy to solve with Seam (and also Spring) as we could
> > > declaratively initialize any bean (component) property. As far as my
> > > knowledge on CDI goes, this can't be done with CDI/JavaEE alone. So I
> was
> > > wondering with Deltaspike has any feature to help with this issue.
> > >
> > > Regards,
> > >
> >
> >
> >
> > --
> > Daniel Cunha (soro)
> >
>

Re: Setting the persistence-unit name declaratively with @PersistenceContext annotation

Posted by Gerhard Petracek <ge...@gmail.com>.
short addition:
it's std. cdi, however, we will add it to our documentation.
therefore i created [1].

@rodrigo:
there you can have a look at #2 and #3.
everything else should be covered by [2] already.

regards,
gerhard

[1] https://issues.apache.org/jira/browse/DELTASPIKE-816
[2] http://deltaspike.apache.org/documentation/jpa.html



http://www.irian.at

Your JavaEE powerhouse -
JavaEE Consulting, Development and
Courses in English and German

Professional Support for Apache
MyFaces, DeltaSpike and OpenWebBeans

2015-01-02 19:40 GMT+01:00 Daniel Cunha <da...@gmail.com>:

> Can fix it with JNDI lookup?
>
> On Fri, Jan 2, 2015 at 6:10 PM, Rodrigo Uchôa <ro...@gmail.com>
> wrote:
>
> > Daniel,
> >
> > That would create an *application managed* EntityManager, wouldn't it? It
> > seems like an alternative, but I hope there's a more elegant approach
> that
> > would not prevent me from using a *container managed* EntityManager.
> >
> > On Fri, Jan 2, 2015 at 3:29 PM, Daniel Cunha <da...@gmail.com>
> wrote:
> >
> > > Can injectionPoint fix it?
> > >
> > > You can create your annotation where you set the unitName.
> > > So, you set the unitName.
> > > In your produce, get the InjectionPoint, read the annotation and set
> > > unitName in Persistence.createEntityManagerFactory(...);
> > > So, you can create the entityManger with this EntityManagerFactory and
> > > specific unitname.
> > >
> > > I think that can be a way.
> > >
> > >
> > >
> >
> http://docs.oracle.com/javaee/7/api/javax/persistence/Persistence.html#createEntityManagerFactory(java.lang.String)
> > >
> > >
> >
> http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManagerFactory.html#createEntityManager()
> > >
> > >
> >
> http://docs.oracle.com/javaee/7/api/javax/enterprise/inject/spi/InjectionPoint.html#getAnnotated()
> > >
> > >
> >
> http://docs.oracle.com/javaee/7/api/javax/enterprise/inject/spi/Annotated.html#getAnnotation(java.lang.Class)
> > >
> > > On Fri, Jan 2, 2015 at 5:02 PM, Rodrigo Uchôa <rodrigo.uchoa@gmail.com
> >
> > > wrote:
> > >
> > > > Hi everyone!
> > > >
> > > > I'm developing an API, a simple utility jar, for persistence
> (generic)
> > > > operations. The lib itself does not provide any persistence.xml file,
> > it
> > > > takes into account that all its clients will have one to configure
> > their
> > > > own persistence unit.
> > > >
> > > > Inside my API I can't make assumptions about the persistence unit
> name
> > > for
> > > > the clients, so I'm simply doing something like this:
> > > >
> > > > *public class MyGenericDAO {*
> > > > *    @Inject*
> > > > *    EntityManager em;*
> > > > *}*
> > > >
> > > >
> > > > ... And every client who depends on the API will have a "Resource
> > > > Producer":
> > > >
> > > > *public class ResourcesProducer {*
> > > > *    @Produces*
> > > > *  @PersistenceContext( unitName = "some-pu")*
> > > > *    private EntityManager entityManager;*
> > > > *}*
> > > >
> > > >
> > > > This works as expected as long as each client have a single
> persistence
> > > > unit configured. The injection point in MyGenericDAO will resolve to
> > > > "some-pu" because there's only one. But if there are multiple
> > persistence
> > > > units it won't work.
> > > >
> > > > This was easy to solve with Seam (and also Spring) as we could
> > > > declaratively initialize any bean (component) property. As far as my
> > > > knowledge on CDI goes, this can't be done with CDI/JavaEE alone. So I
> > was
> > > > wondering with Deltaspike has any feature to help with this issue.
> > > >
> > > > Regards,
> > > >
> > >
> > >
> > >
> > > --
> > > Daniel Cunha (soro)
> > >
> >
>
>
>
> --
> Daniel Cunha (soro)
>

Re: Setting the persistence-unit name declaratively with @PersistenceContext annotation

Posted by Daniel Cunha <da...@gmail.com>.
Can fix it with JNDI lookup?

On Fri, Jan 2, 2015 at 6:10 PM, Rodrigo Uchôa <ro...@gmail.com>
wrote:

> Daniel,
>
> That would create an *application managed* EntityManager, wouldn't it? It
> seems like an alternative, but I hope there's a more elegant approach that
> would not prevent me from using a *container managed* EntityManager.
>
> On Fri, Jan 2, 2015 at 3:29 PM, Daniel Cunha <da...@gmail.com> wrote:
>
> > Can injectionPoint fix it?
> >
> > You can create your annotation where you set the unitName.
> > So, you set the unitName.
> > In your produce, get the InjectionPoint, read the annotation and set
> > unitName in Persistence.createEntityManagerFactory(...);
> > So, you can create the entityManger with this EntityManagerFactory and
> > specific unitname.
> >
> > I think that can be a way.
> >
> >
> >
> http://docs.oracle.com/javaee/7/api/javax/persistence/Persistence.html#createEntityManagerFactory(java.lang.String)
> >
> >
> http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManagerFactory.html#createEntityManager()
> >
> >
> http://docs.oracle.com/javaee/7/api/javax/enterprise/inject/spi/InjectionPoint.html#getAnnotated()
> >
> >
> http://docs.oracle.com/javaee/7/api/javax/enterprise/inject/spi/Annotated.html#getAnnotation(java.lang.Class)
> >
> > On Fri, Jan 2, 2015 at 5:02 PM, Rodrigo Uchôa <ro...@gmail.com>
> > wrote:
> >
> > > Hi everyone!
> > >
> > > I'm developing an API, a simple utility jar, for persistence (generic)
> > > operations. The lib itself does not provide any persistence.xml file,
> it
> > > takes into account that all its clients will have one to configure
> their
> > > own persistence unit.
> > >
> > > Inside my API I can't make assumptions about the persistence unit name
> > for
> > > the clients, so I'm simply doing something like this:
> > >
> > > *public class MyGenericDAO {*
> > > *    @Inject*
> > > *    EntityManager em;*
> > > *}*
> > >
> > >
> > > ... And every client who depends on the API will have a "Resource
> > > Producer":
> > >
> > > *public class ResourcesProducer {*
> > > *    @Produces*
> > > *  @PersistenceContext( unitName = "some-pu")*
> > > *    private EntityManager entityManager;*
> > > *}*
> > >
> > >
> > > This works as expected as long as each client have a single persistence
> > > unit configured. The injection point in MyGenericDAO will resolve to
> > > "some-pu" because there's only one. But if there are multiple
> persistence
> > > units it won't work.
> > >
> > > This was easy to solve with Seam (and also Spring) as we could
> > > declaratively initialize any bean (component) property. As far as my
> > > knowledge on CDI goes, this can't be done with CDI/JavaEE alone. So I
> was
> > > wondering with Deltaspike has any feature to help with this issue.
> > >
> > > Regards,
> > >
> >
> >
> >
> > --
> > Daniel Cunha (soro)
> >
>



-- 
Daniel Cunha (soro)

Re: Setting the persistence-unit name declaratively with @PersistenceContext annotation

Posted by Rodrigo Uchôa <ro...@gmail.com>.
Daniel,

That would create an *application managed* EntityManager, wouldn't it? It
seems like an alternative, but I hope there's a more elegant approach that
would not prevent me from using a *container managed* EntityManager.

On Fri, Jan 2, 2015 at 3:29 PM, Daniel Cunha <da...@gmail.com> wrote:

> Can injectionPoint fix it?
>
> You can create your annotation where you set the unitName.
> So, you set the unitName.
> In your produce, get the InjectionPoint, read the annotation and set
> unitName in Persistence.createEntityManagerFactory(...);
> So, you can create the entityManger with this EntityManagerFactory and
> specific unitname.
>
> I think that can be a way.
>
>
> http://docs.oracle.com/javaee/7/api/javax/persistence/Persistence.html#createEntityManagerFactory(java.lang.String)
>
> http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManagerFactory.html#createEntityManager()
>
> http://docs.oracle.com/javaee/7/api/javax/enterprise/inject/spi/InjectionPoint.html#getAnnotated()
>
> http://docs.oracle.com/javaee/7/api/javax/enterprise/inject/spi/Annotated.html#getAnnotation(java.lang.Class)
>
> On Fri, Jan 2, 2015 at 5:02 PM, Rodrigo Uchôa <ro...@gmail.com>
> wrote:
>
> > Hi everyone!
> >
> > I'm developing an API, a simple utility jar, for persistence (generic)
> > operations. The lib itself does not provide any persistence.xml file, it
> > takes into account that all its clients will have one to configure their
> > own persistence unit.
> >
> > Inside my API I can't make assumptions about the persistence unit name
> for
> > the clients, so I'm simply doing something like this:
> >
> > *public class MyGenericDAO {*
> > *    @Inject*
> > *    EntityManager em;*
> > *}*
> >
> >
> > ... And every client who depends on the API will have a "Resource
> > Producer":
> >
> > *public class ResourcesProducer {*
> > *    @Produces*
> > *  @PersistenceContext( unitName = "some-pu")*
> > *    private EntityManager entityManager;*
> > *}*
> >
> >
> > This works as expected as long as each client have a single persistence
> > unit configured. The injection point in MyGenericDAO will resolve to
> > "some-pu" because there's only one. But if there are multiple persistence
> > units it won't work.
> >
> > This was easy to solve with Seam (and also Spring) as we could
> > declaratively initialize any bean (component) property. As far as my
> > knowledge on CDI goes, this can't be done with CDI/JavaEE alone. So I was
> > wondering with Deltaspike has any feature to help with this issue.
> >
> > Regards,
> >
>
>
>
> --
> Daniel Cunha (soro)
>

Re: Setting the persistence-unit name declaratively with @PersistenceContext annotation

Posted by Daniel Cunha <da...@gmail.com>.
Can injectionPoint fix it?

You can create your annotation where you set the unitName.
So, you set the unitName.
In your produce, get the InjectionPoint, read the annotation and set
unitName in Persistence.createEntityManagerFactory(...);
So, you can create the entityManger with this EntityManagerFactory and
specific unitname.

I think that can be a way.

http://docs.oracle.com/javaee/7/api/javax/persistence/Persistence.html#createEntityManagerFactory(java.lang.String)
http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManagerFactory.html#createEntityManager()
http://docs.oracle.com/javaee/7/api/javax/enterprise/inject/spi/InjectionPoint.html#getAnnotated()
http://docs.oracle.com/javaee/7/api/javax/enterprise/inject/spi/Annotated.html#getAnnotation(java.lang.Class)

On Fri, Jan 2, 2015 at 5:02 PM, Rodrigo Uchôa <ro...@gmail.com>
wrote:

> Hi everyone!
>
> I'm developing an API, a simple utility jar, for persistence (generic)
> operations. The lib itself does not provide any persistence.xml file, it
> takes into account that all its clients will have one to configure their
> own persistence unit.
>
> Inside my API I can't make assumptions about the persistence unit name for
> the clients, so I'm simply doing something like this:
>
> *public class MyGenericDAO {*
> *    @Inject*
> *    EntityManager em;*
> *}*
>
>
> ... And every client who depends on the API will have a "Resource
> Producer":
>
> *public class ResourcesProducer {*
> *    @Produces*
> *  @PersistenceContext( unitName = "some-pu")*
> *    private EntityManager entityManager;*
> *}*
>
>
> This works as expected as long as each client have a single persistence
> unit configured. The injection point in MyGenericDAO will resolve to
> "some-pu" because there's only one. But if there are multiple persistence
> units it won't work.
>
> This was easy to solve with Seam (and also Spring) as we could
> declaratively initialize any bean (component) property. As far as my
> knowledge on CDI goes, this can't be done with CDI/JavaEE alone. So I was
> wondering with Deltaspike has any feature to help with this issue.
>
> Regards,
>



-- 
Daniel Cunha (soro)