Spring Data Neo4J template.fetch()没有返回整个集合

时间:2015-03-14 01:08:27

标签: spring-data-neo4j

我对此完全感到困惑,并且我不确定我还能做些什么才能让它发挥作用。

在关系上使用@Fetch会急切地获取该关系。

如果属性未使用@fetch注释,则可以使用Template.fetch获取关系,即延迟加载。这种类型的延迟加载是否适用于集合?

我有一个名为Project的节点实体,它有一组扫描。当我尝试获取扫描集时,我只进行1次扫描。当我修改Project类以使用@Fetch时,我得到3次扫描。适量的扫描是3.使用template.fetch不会给我整个集合。问题是为什么?我的代码中有什么问题吗?

这里的问题是ProjectServiceImpl.findAllScans();

以下是我的源代码,从我的maven pom文件开始。

    <!-- Spring Version -->
    <properties>
        <java-version>1.8</java-version>
        <spring.version>4.0.9.RELEASE</spring.version>
        <spring.security.version>3.2.5.RELEASE</spring.security.version>
        <jackson.version>2.3.0</jackson.version>
        <guava.version>15.0</guava.version>
        <spring.neo4j.version>3.1.5.RELEASE</spring.neo4j.version>
        <spring.mongodb.version>1.6.2.RELEASE</spring.mongodb.version>
        <ehcache.version>2.9.0</ehcache.version>
        <logback.version>1.1.2</logback.version>
        <jcloverslf4j.version>1.7.10</jcloverslf4j.version>
        <javax.servlet.version>3.1.0</javax.servlet.version>
        <javax.validation.version>1.1.0.Final</javax.validation.version>
        <spring.swagger.version>1.0.0</spring.swagger.version>
        <spring.swagger.ui.version>0.4</spring.swagger.ui.version>
    </properties>

    <repositories>
        <repository>
            <id>jcenter-release</id>
            <name>jcenter</name>
            <url>http://oss.jfrog.org/artifactory/oss-release-local/</url>
        </repository>
        <repository>
            <id>oss-jfrog-artifactory</id>
            <name>oss-jfrog-artifactory-releases</name>
            <url>http://oss.jfrog.org/artifactory/oss-release-local</url>
        </repository>
    </repositories>

    <dependencies>

        <dependency>
            <groupId>org.ajar</groupId>
            <artifactId>swagger-spring-mvc-ui</artifactId>
            <version>${spring.swagger.ui.version}</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.mangofactory</groupId>
            <artifactId>swagger-springmvc</artifactId>
            <version>${spring.swagger.version}</version>
        </dependency>

        <!-- Spring Framework Core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Jackson JSON Dependencies-->

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <!-- Servlet 3.1 and Java EE Validation API-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${javax.servlet.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>${javax.validation.version}</version>
        </dependency>

        <!-- Spring Data MongoDB -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>${spring.mongodb.version}</version>
        </dependency>

        <!-- Spring Data Neo4J -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
            <version>${spring.neo4j.version}</version>
        </dependency>


        <!-- Ehcache -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>${ehcache.version}</version>
        </dependency>

        <!-- Google Guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>

        <!-- Logback -->

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${jcloverslf4j.version}</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>TEST</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

项目Pojo:

@NodeEntity
public class Project {

    // Properties
    @GraphId
    private Long id;
    @Indexed(unique = true)
    private String name;
    @Indexed
    private String xxxxId;

    // Relationships
    @RelatedTo(type = "HAS_SCAN", elementClass = Scan.class)
    private Set<Scan> scans;

下面的代码片段是我使用的扫描类。

    @NodeEntity
public class Scan {

    // Properties
    @GraphId
    private Long id;
    @Indexed
    private String xxxxId;
    @Indexed
    private String projectVersion;
    @Indexed
    private Date date;

    //Relationships
    @RelatedTo(type = "NEXT_SCAN")
    private Scan scan;
    @RelatedTo(type = "HAS_ISSUE", elementClass = Issue.class)
    private Set<Issue> issues;
    @RelatedTo(type = "HAS_DEPENDENCY", elementClass = Dependency.class)
    private Set<Dependency> dependencies;
    @RelatedTo(type = "HAS_PACKAGE", elementClass = Package.class)
    private Set<Package> packages;

下面的下一个代码片段是我使用的存储库类。

    @Repository
public interface ProjectRepository extends GraphRepository<Project> {

    @Query("MATCH (p:Project) RETURN p")
    public Set<Project> findAllProjects();

    @Query("MATCH (p:Project {xxxId:{0}}) RETURN p")
    public Project findProjectById(String projectId);

}

下面的类是我使用的服务类的片段,我在这里调用template.fetch

    @Service
@Transactional
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private Neo4jTemplate template;
    @Autowired
    private ProjectRepository projectRepository;
    @Autowired
    private ScanRepository scanRepository;

    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectServiceImpl.class);

    @Override
    public ProjectsDTO findAllProjects() {
        return DTOTransformerUtil.transformProjectSetToProjectsDTO(projectRepository.findAllProjects());
    }

    @Override
    public ProjectDTO findProject(String projectId) {

        if (projectId != null) {
            return DTOTransformerUtil.transformProjectToProjectDTO(projectRepository.findProjectById(projectId));
        }
        return null;
    }

    @Override
    public ScansDTO findAllScans(String projectId) {

        if (projectId != null) {
            Project project = projectRepository.findProjectById(projectId);
            return DTOTransformerUtil.transformScanSetToScansDTO(template.fetch(project.getScans()));
        }
        return null;
    }

我完全不知道问题是什么,我目前正在尝试使用旧版本的Spring数据neo4j,这可能有助于解决问题。

0 个答案:

没有答案