java - @QueryResult 映射错误

标签 java spring neo4j spring-boot spring-data-neo4j-4

我正在使用

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-server</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

当我对查询进行单元测试时,得到的异常是

10:27:24.803 [http-nio-8080-exec-8] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/rest-api] threw exception [Request processing failed; nested exception is org.neo4j.ogm.metadata.MappingException: Error mapping to ad-hoc class com.lyreco.lab.neo4j.entity.CategoryResult.  At present, only @QueryResult types that are discovered by the domain entity package scanning can be mapped.] with root cause
org.neo4j.ogm.metadata.MappingException: Error mapping to ad-hoc class com.lyreco.lab.neo4j.entity.CategoryResult.  At present, only @QueryResult types that are discovered by the domain entity package scanning can be mapped.

这是我的 CategoryResult 类

package com.lyreco.lab.neo4j.entity;
import org.springframework.data.neo4j.annotation.QueryResult;
import com.lyreco.lab.bean.CategoryVO;

@QueryResult
public class CategoryResult {
    private Long neo4jId;
    private String uuid;
    private String name;
    private CategoryVO parent = new CategoryVO();
    private long numberItems;
...

这是我的应用程序类

package com.lyreco.lab.configuration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@EnableHypermediaSupport(type = { EnableHypermediaSupport.HypermediaType.HAL })
@EnableSpringDataWebSupport
@ComponentScan(basePackages = { "com.lyreco.lab.api", "com.lyreco.lab.service", "com.lyreco.lab.neo4j",
        "com.lyreco.lab.security" })
@Import({ SwaggerConfig.class, Neo4jConfig.class, SpringSecurityConfig.class })
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {

        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
                ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

                container.addErrorPages(error401Page, error404Page, error500Page);
            }
        };
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {

            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/rest-api/**").allowedOrigins("http://localhost:9000");
            }
        };
    }
}

这是我的 session 工厂

package com.lyreco.lab.configuration;

import java.util.UUID;

import org.neo4j.ogm.session.SessionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.event.BeforeSaveEvent;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.server.Neo4jServer;
import org.springframework.data.neo4j.server.RemoteServer;
import org.springframework.data.neo4j.template.Neo4jOperations;
import org.springframework.data.neo4j.template.Neo4jTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.lyreco.lab.neo4j.entity.DomainEntity;

@Configuration
@EnableNeo4jRepositories("com.lyreco.lab.neo4j.repository")
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
class Neo4jConfig extends Neo4jConfiguration {

    @Value("${neo4j.server.url}")
    private String serverAddress;

    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory("com.lyreco.lab.neo4j.entity");
    }

    @Override
    public Neo4jServer neo4jServer() {
        return new RemoteServer(serverAddress);
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    ApplicationListener<BeforeSaveEvent> beforeSaveEventApplicationListener() {
        return new ApplicationListener<BeforeSaveEvent>() {
            @Override
            public void onApplicationEvent(BeforeSaveEvent event) {
                DomainEntity entity = (DomainEntity) event.getEntity();
                entity.setUuid(UUID.randomUUID().toString());
            }
        };
    }

    // TODO : Bug fix
    // cf.
    // http://stackoverflow.com/questions/30604863/spring-data-neo4j-4-0-0-beforesaveevent-not-firing
    // cf. https://jira.spring.io/browse/DATAGRAPH-710
    @Bean
    public Neo4jOperations getNeo4jTemplate() throws Exception {
        return new Neo4jTemplate(getSession());
    }
}

不确定我的代码有什么问题。

最佳答案

此异常表明包含@QueryResult com.lyreco.lab.neo4j.entity 的包未被扫描。请检查提供给 SessionFactory 的包列表。

关于java - @QueryResult 映射错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37609671/

相关文章:

java - 将 "this"从 Java 接口(interface)传递到另一个类

java - Autowiring 数据源数据库项目 bean

indexing - Neo4j - 通过ID查找节点 - 如何获取ID进行查询?

java - Spring-data-neo4j 获取子项时出现 NullPointerException

neo4j 按连接顺序获取节点

java - 如何使用 Java 仅获取与特定过滤器匹配的 MongoDB 文档

java - 创建允许 N 个复选框的按钮组

Java switch case 未运行

java - 将 Spring 任务 XML 配置转换为代码配置

java - 使用 Spring 框架获取 'Named query not found'