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 <[email protected]> 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 <[email protected]> > 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) >
