2017-09-29 1 views
1

DB로 Java SpringBoot 및 Neo4j를 사용하고 있습니다.Java SpringBoot에서 일반 시작 노드를 가질 수 있고 나중에 유형을 할당 할 수 있습니까?

일반적인 StartNode로 RelationshipEntity를 선언하고 싶습니다. 그런 다음 객체를 만들 때 관계를 연결하려는 NodeEntity를 전달합니다.

각 시작/끝 노드 유형에 대해 복제하지 않도록 관계 클래스를 어떻게 수행 할 수 있습니까?

예 :

인격 및 회사 노드 클래스의 다음
@RelationshipEntity(type = "RESIDES_AT") 
public class ResidesAt { 

    @StartNode 
    private Object startNode; //Can be Company or Person 
     ... 
    @EndNode 
    private Address address; 
} 

내가 가진 :

@NodeEntity (label="Company") 
    public class Company { 
     @Relationship(type="RESIDES_AT", direction=Relationship.OUTGOING) 
     Set<ResidesAt> residesAt = new HashSet<>(); 
... 
    } 

을 그리고 실행에 내가 좋아하는 뭔가를 할 것이다 : 그러나

Company createCompany = new Company("Top Mechanic"); 
Person createPerson = new Person("John", "Doe"); 
Address createAddress = new Address("John's Home", "123 Mystery Lane", null, "Big City", "UT", "84123", null, "Occupied"); 
createPerson.residesAt(createAddress, "Home Owner"); 
createCompany.residesAt(createAddress, "John's Business Mailing Address"); 

companyRepository.save(createCompany); 
personRepository.save(createPerson); 

을;

2017-09-29 16:26:26.832 WARN 7564 --- [   main] org.neo4j.ogm.metadata.ClassInfo   : Failed to find an @StartNode on trn.justin.model.relationships.ResidesAt 
2017-09-29 16:26:26.832 WARN 7564 --- [   main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'companyService': Unsatisfied dependency expressed through field 'companyRepository'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'companyRepository': Unsatisfied dependency expressed through method 'setSession' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.data.neo4j.transaction.SharedSessionCreator#0': Cannot resolve reference to bean 'sessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.neo4j.ogm.session.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.NullPointerException 
2017-09-29 16:26:26.832 INFO 7564 --- [   main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 
2017-09-29 16:26:26.848 WARN 7564 --- [   main] o.s.b.c.e.EventPublishingRunListener  : Error calling ApplicationEventListener 

java.lang.ClassCastException: org.springframework.boot.context.event.ApplicationFailedEvent cannot be cast to org.springframework.boot.web.context.WebServerInitializedEvent 

답변

0

내가 찾을 수있는 가장 좋은 방법은 "NodeEntity"는 "RelationshipEntity"클래스를 사용하도록 일을 구조 조정하는 것입니다, 그리고 RelationshipEntity 클래스 사용 : 나는 SpringBoot 응용 프로그램을 시작하려고 할 때, 나는 다음과 같은 오류가 발생합니다 "RelationshipType"클래스이며 RelationshipType 클래스에 공통 속성을 보유해야합니다. 예 :

@NodeEntity (label="Company") 
public class Company { 
    ... 

    @Relationship(type="RESIDES_AT", direction=Relationship.OUTGOING) 
    Set<CompanyResidesAtAddress> residesAt = new HashSet<>(); 
} 

@NodeEntity (label="Address") 
public class Address { 
    ... 
    @Relationship(type="RESIDES_AT", direction=Relationship.INCOMING) 
    Set<PersonResidesAtAddress> personResidances = new HashSet<>(); 

    @Relationship(type="RESIDES_AT", direction=Relationship.INCOMING) 
    Set<CompanyResidesAtAddress> companyResidances = new HashSet<>(); 
} 

@RelationshipEntity(type = "RESIDES_AT") 
public class CompanyResidesAtAddress { 
    ... 
    @StartNode 
    private Company startNode; 

    private ResidesAt residesAt; 

    @EndNode 
    private Address address; 
} 

public class ResidesAt implements RelationshipType{ 

    ... // Common Attributes & methods 

    @Override 
    public String name() { 
     return this.getClass().getSimpleName().toUpperCase(); 
    } 
} 

그런 다음 실행에 나는 같은 것을 할 : 이것은 나를 위해 일하는, 그리고 내가 찾을 수있는 가장 좋은 방법이 될 것으로 보인다

Company createCompany = new Company("Top Mechanic"); 
    Person createPerson = new Person("John", "Doe"); 
    Address createAddress = new Address("John's Home", "123 Mystery Lane", null, "Salt Lake City", "UT", "84120", null, "Occupied"); 
    createCompany.residesAt(createAddress, "John's Business Mailing Address"); 
    createPerson.residesAt(createAddress, "Home Owner"); 

    companyRepository.save(createCompany); 
    personRepository.save(createPerson); 

.

관련 문제