Hi,
I think, all this EntityManager(Factory)Producers should be obsolete with
DeltaSpike.
Instead we should provide a deltaspike-way to configure the EntityManager.
The way Seam-Persistence does this, seems a quite good solution:
@Produces
@DeltaSpikeManaged
@RequestScoped
private EntityManager entityManager;
or
@Produces
@DeltaSpikeManaged
@RequestScoped
@CustomQualified
private EntityManager entityManager;
Thus the user can specify the scope of an EntityManager that is managed by
DeltaSpike.
I just don't know how the user should specify the name of the persistence
unit then.
Three possibilities:
1.
@Produces@DeltaSpikeManaged
@RequestScoped
@CustomQualified
@PersistenceContext(unitName = "myPU")
private EntityManager entityManager;
2.
@Produces@DeltaSpikeManaged
@RequestScoped
@CustomQualified
@Named("myPU")
private EntityManager entityManager;
3.
@Produces@DeltaSpikeManaged
@RequestScoped
@CustomQualified
private EntityManager entityManager;
@Produces
@CustomQualified
@PersistenceUnitName
private String unit = "myPU";
The third solution would have the advantage that one could configure
different unitNames for different ProjectStagesÅ
Cheers,
Arne
Am 27.08.12 21:11 schrieb "Cody Lerum" unter <[email protected]>:
>This is really an area where we should spend some time in the docs
>discussing the pros/cons of each EM approach as well as the endorsed
>ways of setting them in a project.
>
>CODI already has a good start -
>https://cwiki.apache.org/EXTCDI/jpa-usage.html but I'm not sure the
>relationship between each and TX support is clear.
>
>On Sun, Aug 26, 2012 at 5:30 PM, Stuart Douglas <[email protected]>
>wrote:
>>
>> On 24/08/2012, at 7:47 PM, Mark Struberg <[email protected]> wrote:
>>
>>> Well, we might miss some explanation for this lines
>>>
>>>
>>>> @PersistenceContext(unitName="default")
>>>> private EntityManager entityManager;
>>> This only works in SE. In an EE container you will get a Container
>>>Managed EM, which is not manageable by the user, but strictly bound to
>>>EJBs.
>>>
>>> Even the sample of softwaremill will not work on every EE container
>>>
>>>> @PersistenceUnit(name="pu")
>>>> private EntityManagerFactory emf;
>>>
>>> On a few servers the EE - injected EMF will only allow you to create
>>>managed EMs.
>>>
>>
>> This is a complete violation of the JPA spec, which containers do this?
>>
>> Stuart
>>
>>
>>> The best approach is to either create an additional
>>>EntityManagerFactoryProducer where you can even stuff in JPA properties
>>>in a central place, and inject it via CDI:
>>>
>>>> @Inject
>>>> private EntityManagerFactory emf;
>>> Or you can simply do:
>>>
>>>> @ApplicationScoped // important!
>>>> public class EntityManagerProducer {
>>>> private EntityManagerFactory emf =
>>>>Persistence.createEntityManagerFactory();
>>>>
>>>> @Produces @RequestScoped
>>>> public EntityManager getEntityManager() {
>>>> return new EntityManagerTxEnlistDecorator
>>>>(emf.createEntityManager());
>>>> }
>>>>
>>>> public void close(@Disposes EntityManager em) {
>>>> em.close();
>>>> }
>>>> }
>>>>
>>>
>>> Please note that this is the classic way to get a non-JTA EM! If you
>>>like to use UserTransactions, then check our JPA module. Guess Gerhard
>>>added a sample for it.
>>>
>>> LieGrue,
>>> strub
>>>
>>>
>>>
>>>
>>> ----- Original Message -----
>>>> From: Adrian Gonzalez <[email protected]>
>>>> To: "[email protected]"
>>>><[email protected]>
>>>> Cc:
>>>> Sent: Friday, August 24, 2012 11:38 AM
>>>> Subject: RequestScoped entityManager
>>>>
>>>> Hello,
>>>>
>>>> Does DS provides a requestScopes em ?
>>>>
>>>> I tried to to something like
>>>>https://cwiki.apache.org/EXTCDI/jpa-usage.html, but
>>>> I had an error (don't remember which one, I think it was because em
>>>> wasn't associated with current tx)
>>>>
>>>> For the moment, I'm using softwaremill EntityManagerTxEnlistDecorator
>>>>
>>>>(https://github.com/softwaremill/softwaremill-common/tree/master/softwa
>>>>remill-cdi/src/main/java/pl/softwaremill/common/cdi/persistence
>>>> and doing something like (I'm doing it from memory so, there can be
>>>>some
>>>> errors) :
>>>>
>>>> public class EntityManagerProducer {
>>>> @PersistenceUnit(name="pu")
>>>> private EntityManagerFactory emf;
>>>>
>>>> @Produces @RequestScoped
>>>> public EntityManager getEntityManager() {
>>>> return new EntityManagerTxEnlistDecorator
>>>>(emf.createEntityManager());
>>>> }
>>>>
>>>> public void close(@Disposes EntityManager em) {
>>>> em.close();
>>>> }
>>>> }
>>>>
>>>> Not sure if it deserves a special classe in DS (perhaps at least
>>>> softwaremill EntityManagerTxEnlistDecorator ?)
>>>>
>>>> Thanks
>>>>
>>