Spring Data Neo4j Repository Composition error: No property XXXX found for type YYYY

江枫思渺然 提交于 2020-01-14 10:37:57

问题


This should be a simple question about something that I'm probably missing from the Spring Data documentation.

I'm trying to implement a repository extension as described in: http://docs.spring.io/spring-data/data-neo4j/docs/3.0.2.RELEASE/reference/html/programming-model.html#d0e2970.

The code is really simple. I just have a repository and an extension interface (and implementation).

First a repository for MyType class:

public interface MyTypeRepository extends 
        GraphRepository<MyType>, MyTypeRepositoryExtension { }

Then the extension interface:

public interface MyTypeRepositoryExtension {
    void anyMethodNameForQuery();
}

And its implementation:

public class MyTypeRepositoryExtensionImpl {

     public void anyMethodNameForQuery() {
          //custom query code
     }
 }

With this code, Spring Data throws an error while bootstrapping its mapping infrastructure:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property any found for type MyType!

As what I'm understanding from this error, any method that is added to the extension interface Spring Data tries to map to a property of the respective type. But that is the only use of repository extensions? That is not what I was understanding from the documentation (linked above). If that is true what are the alternatives for custom queries?

A final observation is that I'm aware of @Query annotation, but my use case require a specific traversal and dynamic query composition.


EDIT: FURTHER INFORMATION

Looking at the documentation again I found some information that might be related to this. In order to configure repository composition it is necessary to change repository scan path from org.example.repository to org.springframework.data.neo4j. That is why any method that I put in the MyTypeRepositoryExtension interface Spring Data was trying to map to a MyType property.

I've set Spring Data Neo4j repository scan path as documented, but I've got many runtime class not found errors such as: java.io.FileNotFoundException: class path resource [javax/enterprise/inject/spi/Extension.class] cannot be opened because it does not exist".

In a try-and-error-totally-adhoc manner I have managed to find some dependencies that could fill out the missing classes. I've ended up with these dependencies:

    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-core</artifactId>
        <version>3.3.3</version>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-lucene4</artifactId>
        <version>3.3.3</version>
        <optional>true</optional>
        <exclusions>
            <exclusion>
                <groupId>org.apache.lucene</groupId>
                <artifactId>lucene-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
        <version>3.3.3</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>1.0-SP1</version>
    </dependency>

That eliminated the class not found errors, but now, since I changed the repository path, Spring is not able to inject the repositories.

The documentation tries to say something about this I think, but I couldn't understand what "put it behind" means in the phrase (from the documentation): "If you use <context:component-scan> in your spring config, please make sure to put it behind <neo4j:repositories>, as the RepositoryFactoryBean adds new bean definitions for all the declared repositories, the context scan doesn't pick them up otherwise".

To sum up I really need some orientation here :-) ... Also, I think that the documentation needs to define the needed dependencies. And describe how extension repositories and standard repositories (interface only) can be mixed in a project.


回答1:


I know after one year probably the answer won't be useful for you, but maybe can help other people who is struggling with spring-data-neo4j, like myself these days (see question)

Just point two things you need to change:

1) Following the spring-data repository naming conventions, the name of the implementation of your custom repository has to be EntityRepsitoryImpl (although the default sufix Impl can be changed by configuration). So, you need to change MyTypeRepositoryExtensionImpl for MyTypeRepositoryImpl

2) You don't need to change the neo4j:repositories package. org.example.repository is fine. Don't use org.springframework.data.neo4j

3) Concerning the dependencies needed, if you don't change the package repository as mentioned in the point 2, is enough with the basics:

  • org.springframework.data.spring-data-neo4j
  • javax.validation.validation-api
  • com.sun.jersey.jersey-client

I agree with you that spring-data-neo4j repository composition is a bit confusing and there are very few good examples online... So, finally I've decided to create a sample project on GitHub, with a basic example showing how we can do that.

Hope it help other people in the future.

See on GitHub: neo4jCustomRepository



来源:https://stackoverflow.com/questions/23547885/spring-data-neo4j-repository-composition-error-no-property-xxxx-found-for-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!