Neo4j 嵌套关系加载使用 spring 数据存储库
Neo4j nested relations loading using spring data repository
我有以下类
import java.util.Set;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
@NodeEntity
public class Client {
@Id
@GeneratedValue
private Long id;
private String name;
@Relationship(type = "HAS_CONFIGURED", direction = Relationship.OUTGOING)
public Set<Environment> environments;
public Client () {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Environment> getEnvironments() {
return environments;
}
public void setEnvironments(Set<Environment> environments) {
this.environments = environments;
}
}
import java.util.Set;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
@NodeEntity
public class Environment {
@Id
@GeneratedValue
private Long id;
private String name;
@Relationship(type = "HAS_INSTALLED", direction = Relationship.OUTGOING)
public Set<Application> applications;
public Environment() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Application> getApplications() {
return applications;
}
public void setApplications(Set<Application> applications) {
this.applications = applications;
}
}
import java.util.Set;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import org.neo4j.ogm.annotation.RelationshipEntity;
@NodeEntity
public class Application {
@Id
@GeneratedValue
private Long id;
private String name;
@Relationship(type = "CAN_THROW", direction = Relationship.OUTGOING)
public Set<Error> errors;
public Application() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Error> getErrors() {
return errors;
}
public void setErrors(Set<Error> errors) {
this.errors = errors;
}
}
我正在尝试使用 crud 存储库加载它们
import java.util.List;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import com.nic.loganalyzer.loganalyzer.model.entity.graph.Client;
public interface ClientRepository extends Neo4jRepository<Client, Long> {
List<Client> findAllByName(String name);
}
层次结构类似于 Client-->Environment-->Application--Error
。
问题是它只加载到应用程序,但应用程序不加载环境。
是不是急于加载?
否则我该如何加载整个结构并遍历。
我遍历的方式如下
List<Client> findAllByName = clientRepository.findAllByName(eventLog.getClient());
for (Client client: findAllByName) {
List<Environment> environments = client.getEnvironments().stream()
.filter(e -> e.getName().equalsIgnoreCase(eventLog.getEnvironment()))
.collect(Collectors.toList());
for (Environment environment : environments) {
List<Application> applications = environment.getApplications().stream()
.filter(e -> e.getName().equalsIgnoreCase(eventLog.getApplication()))
.collect(Collectors.toList());
for (Application application : applications) {
List<Error> errors = application.getErrors().stream()
.filter(e -> eventLog.getError().contains(e.getName()))
.collect(Collectors.toList());
据我了解,派生查找器的默认 "depth" 值为 1。
如果您想加载整个层次结构,请尝试在派生的查找器之上添加@Depth(value = 3)。
您的方法应该如下所示:
@Depth(value = 3)
List<Client> findAllByName(String name);
我有以下类
import java.util.Set;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
@NodeEntity
public class Client {
@Id
@GeneratedValue
private Long id;
private String name;
@Relationship(type = "HAS_CONFIGURED", direction = Relationship.OUTGOING)
public Set<Environment> environments;
public Client () {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Environment> getEnvironments() {
return environments;
}
public void setEnvironments(Set<Environment> environments) {
this.environments = environments;
}
}
import java.util.Set;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
@NodeEntity
public class Environment {
@Id
@GeneratedValue
private Long id;
private String name;
@Relationship(type = "HAS_INSTALLED", direction = Relationship.OUTGOING)
public Set<Application> applications;
public Environment() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Application> getApplications() {
return applications;
}
public void setApplications(Set<Application> applications) {
this.applications = applications;
}
}
import java.util.Set;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import org.neo4j.ogm.annotation.RelationshipEntity;
@NodeEntity
public class Application {
@Id
@GeneratedValue
private Long id;
private String name;
@Relationship(type = "CAN_THROW", direction = Relationship.OUTGOING)
public Set<Error> errors;
public Application() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Error> getErrors() {
return errors;
}
public void setErrors(Set<Error> errors) {
this.errors = errors;
}
}
我正在尝试使用 crud 存储库加载它们
import java.util.List;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import com.nic.loganalyzer.loganalyzer.model.entity.graph.Client;
public interface ClientRepository extends Neo4jRepository<Client, Long> {
List<Client> findAllByName(String name);
}
层次结构类似于 Client-->Environment-->Application--Error
。
问题是它只加载到应用程序,但应用程序不加载环境。
是不是急于加载?
否则我该如何加载整个结构并遍历。
我遍历的方式如下
List<Client> findAllByName = clientRepository.findAllByName(eventLog.getClient());
for (Client client: findAllByName) {
List<Environment> environments = client.getEnvironments().stream()
.filter(e -> e.getName().equalsIgnoreCase(eventLog.getEnvironment()))
.collect(Collectors.toList());
for (Environment environment : environments) {
List<Application> applications = environment.getApplications().stream()
.filter(e -> e.getName().equalsIgnoreCase(eventLog.getApplication()))
.collect(Collectors.toList());
for (Application application : applications) {
List<Error> errors = application.getErrors().stream()
.filter(e -> eventLog.getError().contains(e.getName()))
.collect(Collectors.toList());
据我了解,派生查找器的默认 "depth" 值为 1。
如果您想加载整个层次结构,请尝试在派生的查找器之上添加@Depth(value = 3)。
您的方法应该如下所示:
@Depth(value = 3)
List<Client> findAllByName(String name);