deltaspike-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gerhard Petracek <gerhard.petra...@gmail.com>
Subject Re: Setting the persistence-unit name declaratively with @PersistenceContext annotation
Date Fri, 02 Jan 2015 18:39:20 GMT
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 <rodrigo.uchoa@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 <danielsoro@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)
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message