Thomas / John, thanks for your input on this.


Here is what I have so far.  I must apologise I am unable to test it here in 
the UK, but will propose it to the (offshore) team if you guys don't see any 
immediate issues.



Here is the interface with the add() method:


public interface PersistentCollection<E> {
    void add(E entity);
}



And the handler which implements DelegateQueryHandler and provides the 
implementation:



public class PersistentCollectionHandler<E> implements PersistentCollection<E>, 
DelegateQueryHandler {
    @Inject
    private QueryInvocationContext context;

    @RequiresTransaction
    public void add(E entity) {
        context.getEntityManager().persist(entity);
    }
}



Our project is called Whale (don’t ask), so a Repository will no longer extend 
EntityRepository directly but will instead extend WhaleRepository:



public interface WhaleRepository<E, PK extends Serializable> extends 
EntityRepository<E, PK>, PersistentCollection<E> {
}



One of our persistent Domain objects is called Family; here is that Repository 
interface now using the WhaleRepository parent:



@ApplicationScoped
@Repository
@EntityManagerConfig(entityManagerResolver = WhaleEntityManagerResolver.class)
public interface FamilyRepository extends WhaleRepository<Family, FamilyPK> {
    Family findByFamilyCode(String familyCode);
}



IntelliJ is happy that FamilyRepository instances now have an add(Family) 
method:



[cid:[email protected]]



So, given that I am unable actually to test, does this look feasible?



Thanks again, Robin.





-----Original Message-----
From: Thomas Hug [mailto:[email protected]]
Sent: 26 November 2015 14:50
To: [email protected]
Subject: Re: Extending EntityRepository interface and EntityRepositoryHandler 
class



Hi Robin



Did you already look at the Query Delegates?

http://deltaspike.apache.org/documentation/data.html#Extensions



To summarize it for your context:

- You can define a new interface with the add method

- Provide an implementation of it which implements the new interface as well as 
DelegateQueryHandler. You can access the entityManager over an injected 
QueryInvocationContext (as in the docs).

- Have your repositories implement / extend the new interface

- Then ... No that's actually all, use the new add method :)



Hope this helps.



Cheers,

Thomas





On Thu, Nov 26, 2015 at 1:27 PM, Roos, Robin 
<[email protected]<mailto:[email protected]>>

wrote:



> Of course, the method would be called add() and would return

> void....this is consistent with the notion that Repositories model a

> persistent Collection.

>

> @RequiresTransaction

> public void add(E entity) {

>     this.entityManager().persist(entity);

> }

>

> -----Original Message-----

> From: Roos, Robin [mailto:[email protected]]

> Sent: 26 November 2015 12:21

> To: [email protected]<mailto:[email protected]>

> Subject: Extending EntityRepository interface and

> EntityRepositoryHandler class

>

> Hi Folks

>

> My developers are building a JPA application in which Domain objects

> remain entirely within the transactional context.  This means we never

> do detach/merge, whether implicitly or explicitly.

>

> I am concerned that the default Save() implementation in

> EntityRepositoryHandler would incur a merge() if an already-persistent

> instance was passed to save().

>

>

> @RequiresTransaction

> public E save(E entity) {

>     if(this.context.isNew(entity)) {

>         this.entityManager().persist(entity);

>         return entity;

>     } else {

>         return this.entityManager().merge(entity);

>     }

> }

>

> To preclude this I would like to extend EntityRepositoryHandler to

> provide an implementation of a new method add(), as follows:

>

> @RequiresTransaction

> public E save(E entity) {

>     this.entityManager().persist(entity);

>     return entity;

> }

>

> Presumably I would put this method signature into interface

> MyEntityRepository extends EntityRepository, and the implementation

> into class MyEntityRepositoryHandler extends EntityRepositoryHandler.

> Repository interfaces would then implement MyEntityRepository.  (We do

> not have any Repository "implementations" since DeltaSpike takes care

> of that for us.)

>

> But how do I get it all joined up, i.e.  how to I nominate my new

> Handler to be used when CDI is working its magic on the Repository interfaces?

>

> Thanks, Robin.

>

>

> **********************************************************************

> *************************************

> Nissan Motor Manufacturing (UK) Limited is a limited liability company

> registered in England and Wales under number 01806912 with its

> registered office at Washington Road, Sunderland, Tyne and Wear SR5 3NS.

>

> CONFIDENTIALITY NOTICE AND DISCLAIMER

>

> This message including any attachments to it (Message) is private and

> confidential and may contain proprietary or legally privileged

> information.  If you have received this Message in error, please send

> an email to 
> [email protected]<mailto:[email protected]> 
> with a copy of this

> Message and remove it from your system. You must not, directly or

> indirectly, use, disclose, distribute, print or copy any part of this

> Message if you are not the intended recipient. The NISSAN EUROPE S.A.S

> group of companies (NISSAN) reserve the right to monitor all e-mail 
> communications through its networks.

>

> NISSAN is not liable for the proper/complete transmission or any delay

> in the receipt of this Message .  Whilst NISSAN takes care to protect

> its systems from electronic virus attack or other harmful event,

> NISSAN gives no warranty that this Message is free of any virus or

> other harmful matter and accepts no liability for any loss or damage

> resulting from the recipient receiving, opening or using it.

>

> Any views or opinions expressed in this Message are those of the

> author and do not necessarily represent those of NISSAN.

>

> **********************************************************************

> *************************************

>

>

> **********************************************************************

> *************************************

> Nissan Motor Manufacturing (UK) Limited is a limited liability company

> registered in England and Wales under number 01806912 with its

> registered office at Washington Road, Sunderland, Tyne and Wear SR5 3NS.

>

> CONFIDENTIALITY NOTICE AND DISCLAIMER

>

> This message including any attachments to it (Message) is private and

> confidential and may contain proprietary or legally privileged

> information.  If you have received this Message in error, please send

> an email to 
> [email protected]<mailto:[email protected]> 
> with a copy of this

> Message and remove it from your system. You must not, directly or

> indirectly, use, disclose, distribute, print or copy any part of this

> Message if you are not the intended recipient. The NISSAN EUROPE S.A.S

> group of companies (NISSAN) reserve the right to monitor all e-mail 
> communications through its networks.

>

> NISSAN is not liable for the proper/complete transmission or any delay

> in the receipt of this Message .  Whilst NISSAN takes care to protect

> its systems from electronic virus attack or other harmful event,

> NISSAN gives no warranty that this Message is free of any virus or

> other harmful matter and accepts no liability for any loss or damage

> resulting from the recipient receiving, opening or using it.

>

> Any views or opinions expressed in this Message are those of the

> author and do not necessarily represent those of NISSAN.

>

> **********************************************************************

> *************************************

>

>

***********************************************************************************************************
Nissan Motor Manufacturing (UK) Limited is a limited liability company 
registered in England and Wales under number 01806912 with its registered 
office at Washington Road, Sunderland, Tyne and Wear SR5 3NS.

CONFIDENTIALITY NOTICE AND DISCLAIMER 

This message including any attachments to it (Message) is private and 
confidential and may contain proprietary or legally privileged information.  If 
you have received this Message in error, please send an email to 
[email protected] with a copy of this Message and remove it from 
your system. You must not, directly or indirectly, use, disclose, distribute, 
print or copy any part of this Message if you are not the intended recipient. 
The NISSAN EUROPE S.A.S group of companies (NISSAN) reserve the right to 
monitor all e-mail communications through its networks.
 
NISSAN is not liable for the proper/complete transmission or any delay in the 
receipt of this Message .  Whilst NISSAN takes care to protect its systems from 
electronic virus attack or other harmful event, NISSAN gives no warranty that 
this Message is free of any virus or other harmful matter and accepts no 
liability for any loss or damage resulting from the recipient receiving, 
opening or using it. 
 
Any views or opinions expressed in this Message are those of the author and do 
not necessarily represent those of NISSAN.
***********************************************************************************************************

Reply via email to