You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@deltaspike.apache.org by Dominik Obermaier <do...@gmail.com> on 2014/08/17 01:19:27 UTC

Deltaspike Data - Abstract Repositories

I've successfully migrated a self-made repository framework 
implementation to Deltaspike Data and so far it's a breeze!

The only thing I'm wondering is how to build custom abstract 
repositories with convenience methods all concrete repositories should 
inherit.

The scenario is the following:

* All entities extend an AbstractEntity (with fields like 'uuid', 
'lastUpdated' and the 'id')
* I want to introduce an abstract repository (AbstractRepository) 
implementation with methods like "findByUuid", "getLastUpdated", ...
* All concrete repositories are abstract classes and should inherit from 
AbstractRepository


My naive (and non-working) approach was to write the following 4 classes:

Entites:
------------
@MappedSuperclass
public class AbstractEntity implements Serializable { ... }

@Entity
public class User extends AbstractEntity { ... }

Repositories:
--------------------
public abstract class AbstractRepository<T extends AbstractEntity> 
extends AbstractEntityRepository<T, Long> {
}

@Repository
public abstract class UserRepository extends AbstractRepository<User> {
}



When I try to deploy that; I'm getting the following exception:

org.apache.deltaspike.data.impl.RepositoryDefinitionException: 
Repository creation for class class 
com.acme.repository.AbstractRepository failed. Is it associated with a 
valid Entity?
     at 
org.apache.deltaspike.data.impl.meta.RepositoryComponents.extractEntityMetaData(RepositoryComponents.java:118)
     at 
org.apache.deltaspike.data.impl.meta.RepositoryComponents.add(RepositoryComponents.java:56)
     at 
org.apache.deltaspike.data.impl.RepositoryExtension.processAnnotatedType(RepositoryExtension.java:90)
---- SNIP ----



Is there something obvious I'm doing wrong? I'm glad for any help!

All the best,
Dominik



Re: Deltaspike Data - Abstract Repositories

Posted by Dominik Obermaier <do...@gmail.com>.
Ok, got it. Turns out custom abstract repositories apparently aren't 
supported by Deltaspike Data. Fortunately there's the Query Delegates 
concept which allows something similar. My final implementation looks 
like this (using Guava Optionals here, in case someone is wondering):


public interface AbstractEntitySupport<T extends AbstractEntity> {

     public Optional<T> byUuid(final String uuid);
}

public class AbstractEntitySupportExtension<T extends AbstractEntity> 
implements AbstractEntitySupport<T>, DelegateQueryHandler {

     @Inject
     QueryInvocationContext context;

     @Override
     public Optional<T> byUuid(final String uuid) {
        .......
         return Optional.absent();
     }
}

@Repository
public abstract class UserRepository extends 
AbstractEntityRepository<User, Long> implements 
AbstractEntitySupport<User> { .. }


The only thing which is really annoying: If I name this method 
"findByUuid" instead of "byUuid", the AbstractEntitySupportExtension is 
never used but the Query Method Expression magic is used instead. So my 
real implementation is ignored and the autogenerated is used. If anyone 
has a good idea how to disable this behavior, please let me know.

I also filed a JIRA for disabling the Query Method Expressions: 
https://issues.apache.org/jira/browse/DELTASPIKE-687

Best,
Dominik


Dominik Obermaier wrote:
> I've successfully migrated a self-made repository framework 
> implementation to Deltaspike Data and so far it's a breeze!
>
> The only thing I'm wondering is how to build custom abstract 
> repositories with convenience methods all concrete repositories should 
> inherit.
>
> The scenario is the following:
>
> * All entities extend an AbstractEntity (with fields like 'uuid', 
> 'lastUpdated' and the 'id')
> * I want to introduce an abstract repository (AbstractRepository) 
> implementation with methods like "findByUuid", "getLastUpdated", ...
> * All concrete repositories are abstract classes and should inherit 
> from AbstractRepository
>
>
> My naive (and non-working) approach was to write the following 4 classes:
>
> Entites:
> ------------
> @MappedSuperclass
> public class AbstractEntity implements Serializable { ... }
>
> @Entity
> public class User extends AbstractEntity { ... }
>
> Repositories:
> --------------------
> public abstract class AbstractRepository<T extends AbstractEntity> 
> extends AbstractEntityRepository<T, Long> {
> }
>
> @Repository
> public abstract class UserRepository extends AbstractRepository<User> {
> }
>
>
>
> When I try to deploy that; I'm getting the following exception:
>
> org.apache.deltaspike.data.impl.RepositoryDefinitionException: 
> Repository creation for class class 
> com.acme.repository.AbstractRepository failed. Is it associated with a 
> valid Entity?
>     at 
> org.apache.deltaspike.data.impl.meta.RepositoryComponents.extractEntityMetaData(RepositoryComponents.java:118) 
>
>     at 
> org.apache.deltaspike.data.impl.meta.RepositoryComponents.add(RepositoryComponents.java:56) 
>
>     at 
> org.apache.deltaspike.data.impl.RepositoryExtension.processAnnotatedType(RepositoryExtension.java:90) 
>
> ---- SNIP ----
>
>
>
> Is there something obvious I'm doing wrong? I'm glad for any help!
>
> All the best,
> Dominik
>
>