2012-12-19 2 views
35

Ich habe eine Media-Entität mit einigen grundlegenden Feldern für vom Benutzer hochgeladene Dateien. Zum Speichern der Bytes der hochgeladenen Dateien möchte ich ein benutzerdefiniertes Repository erstellen, das diese Funktionalität enthält. Im Anschluss an die Schritte in der Spring documentation habe ich eine Schnittstelle geschaffen, die wie folgt aussieht:Keine Eigenschaft für Typfehler gefunden, wenn versucht wird, ein benutzerdefiniertes Repository mit Spring Data zu erstellen JPA

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; 
} 

Dann bereitgestellt ich eine Implementierung für diese Schnittstelle MediaBytesRepositoryImpl

Damit rief ich die folgende Schnittstelle dann erstellt:

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

Nun, wenn ich den Server starten, erhalte ich den folgenden Stack-Trace:

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 

Ich fand diese similar post, aber die Vorschläge dort (alle im selben Paket, Namenskonvention) sind Dinge, die ich bereits mache. Alle meine Medienklassen und Schnittstellen sind im selben Paket, und ich verwende das Suffix "Impl".

Kann jemand bitte etwas Licht darauf werfen, warum ich diesen Fehler bekomme und wie ich es beheben kann? Vielen Dank.

Antwort

69

Sie schrieb:

die Vorschläge dort (alle im selben Paket, Namenskonvention) sind Dinge, die ich schon tue.

Nein, tun Sie nicht.

Umbenennen Sie MediaBytesRepository zu MediaRepositoryCustom.

Und natürlich benötigen Sie eine Implementierung von MediaRepositoryCustom mit dem Namen MediaRepositoryImpl.

+19

Sieht aus wie ich missverstanden habe, wie die Dinge benannt werden sollen. Nach Ihrer Antwort habe ich mir die Dokumentation nochmals angeschaut und festgestellt, dass der Name meiner implementierenden Klasse der Name der Spring Data Schnittstelle + "Impl" sein muss und nicht der Name meiner Schnittstelle + "Impl". So konnte ich den Namen meiner Schnittstelle 'MediaBytesRepository' behalten und musste nur die Implementierung' MediaRepositoryImpl' anstelle von 'MediaBytesRepositoryImpl' nennen. Danke für die Hilfe. – dnc253

+24

Der gleiche Fehler gemacht ... Um es zusammenzufassen sollte sein: 1) YourInterfaceName 2) YourInterfaceNameCustom 3) YourInterfaceNameImpl –

+1

Sie haben meinen Tag gespeichert. Arbeiten wie Charme –

6

Sie müssen Ihre impl-Klasse als "InterfaceNameImpl" bezeichnen. Standard Postfix für die Umsetzung ist Impl, können wir es ändern, wie wir wollen:

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

Der Name benutzerdefinierte Schnittstellen keine Rolle spielt.

+0

Ist das in der Pom? – santafebound

Verwandte Themen