web-dev-qa-db-fra.com

Aucune propriété trouvée pour l'erreur de type lorsque vous essayez de créer un référentiel personnalisé avec Spring Data JPA

J'ai une entité Media qui contient des champs de base pour les fichiers téléchargés par l'utilisateur. Pour enregistrer les octets des fichiers téléchargés, je souhaite créer un référentiel personnalisé contenant cette fonctionnalité. En suivant les étapes de la documentation Spring , j'ai créé une interface qui ressemble à ceci:

public interface MediaBytesRepository
{
    public byte[] getBytes(Media media) throws IOException;
    public void saveBytes(Media media, byte[] bytes) throws IOException;
    public void appendBytes(Media media, byte[] bytes) throws IOException;
    public void deleteBytes(Media media) throws IOException;
    public boolean bytesExist(Media media) throws IOException;
}

J'ai ensuite fourni une implémentation pour cette interface appelée MediaBytesRepositoryImpl

Avec cela, j'ai ensuite créé l'interface suivante:

public interface MediaRepository extends JpaRepository<Media, Long>, MediaBytesRepository
{
}

Maintenant, lorsque je démarre le serveur, j'obtiens la trace de pile suivante:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mediaRepository': FactoryBean threw exception on object creation; nested exception is Java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract void com.foo.bar.core.media.MediaBytesRepository.saveBytes(com.foo.bar.core.media.Media,byte[]) throws Java.io.IOException!
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.Java:149)
.....
Caused by: Java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract void com.foo.bar.core.media.MediaBytesRepository.saveBytes(com.foo.bar.core.media.Media,byte[]) throws Java.io.IOException!
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.Java:92)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.Java:162)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.Java:68)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.Java:280)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.Java:148)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.Java:125)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.Java:41)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.Java:142)
    ... 20 more
 Caused by: Java.lang.IllegalArgumentException: No property save found for type class com.foo.bar.core.media.Media
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.Java:73)
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.Java:92)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.Java:319)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.Java:333)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.Java:301)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.Java:265)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.Java:239)
    at org.springframework.data.repository.query.parser.Part.<init>(Part.Java:70)
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.Java:180)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.Java:260)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.Java:240)
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.Java:68)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.Java:57)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.Java:90)
    ... 27 more

J'ai trouvé cela article similaire , mais les suggestions là-bas (toutes dans le même paquet, convention de dénomination) sont des choses que je fais déjà. Toutes mes classes de média et interfaces sont dans le même package, et j'utilise le suffixe "Impl".

Quelqu'un peut-il nous expliquer pourquoi je reçois cette erreur et comment la corriger? Merci.

39
dnc253

Tu as écrit:

les suggestions là-bas (toutes dans le même paquet, convention de dénomination) sont des choses que je fais déjà.

Non, non.

Renommez-vous MediaBytesRepository en MediaRepositoryCustom.

Et bien sûr, vous avez besoin d'une implémentation de MediaRepositoryCustom avec le nom MediaRepositoryImpl.

74
Ralph

Vous devez nommer votre classe impl comme "InterfaceNameImpl". Le postfix par défaut pour l'implémentation est Impl, nous pouvons le changer comme nous le souhaitons:

<repositories base-package="com.acme.repository" repository-impl-postfix="FooBar" />

Le nom des interfaces personnalisées n'a pas d'importance.

6
equalto