Neo4j RelationshipEntity和Spring JPA

时间:2017-07-03 18:33:57

标签: neo4j spring-data-neo4j neo4j-ogm

我定义了以下节点和关系: CarMaker and Models

  1. CarModel在多年内成为CarMaker,并且表示为MADE_IN关系的属性。
  2. CarModel仅由一个CarMaker制作。
  3. CarMaker可以在多年内制作多个CarModel。
  4. 我已经定义了以下类来表示节点:CarModel,CarMaker和关系MADE_IN

    CarModel

    @NodeEntity
    public class CarModel {
    
        private Long id;
        private String name;
        @Relationship (type="MADE_IN", direction = Relationship.UNDIRECTED)
        private Set<MadeIn> madeIns = new HashSet<MadeIn>();
        private Set<String> years = new HashSet<String>();
    
        public CarModel() {     
        }
    
        public Long getId() {
            return id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    
        public void addMadeIn(MadeIn madeIn) {
            System.out.println ("Found CarMaker: " + madeIn.getCarMaker());
            this.madeIns.add(madeIn);
        }
    
        private Set<MadeIn> getMadeIn() {
            return madeIns;     
        }
    
        public Set<String> getYears() {
            Iterator<MadeIn> itr = madeIns.iterator();
            while (itr.hasNext()) {
                years.add(((MadeIn) itr.next()).getYear());
            }
    
            Set<String> sortedYears = years.stream().collect(Collectors.toCollection(TreeSet::new));
            return sortedYears;
        }
    
    
    }
    

    的汽车制造商

    public class CarMaker {
    
        @GraphId private Long id;
        private String name;    
        @Relationship (type="MADE_IN", direction = Relationship.UNDIRECTED)
        private Set<CarModel> carModels = new HashSet<>();
    
    
        public CarMaker() {
    
        }
    
        public Long getId() {
            return id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    
    
        public Set<CarModel> getCarModels() {
            return carModels;
        }
    
        public void setCarModels(CarModel carModel) {
            carModels.add(carModel);
    
        }
    
    
    }
    

    MADE_IN

    @RelationshipEntity(type="MADE_IN")
    public class MadeIn {
        @GraphId private Long relationshipId;
        @Property private String year;
        @StartNode private CarMaker carMaker;
        @EndNode private CarModel carModel;
    
    
        public MadeIn() {
    
        }
    
        public MadeIn(CarMaker carMaker, CarModel carModel, String year) {
            this.carMaker = carMaker;
            this.carModel = carModel;
            this.year = year;
        }
    
        public Long getRelationshipId() {
            return relationshipId;
        }
    
        public void setCarMaker(CarMaker carMaker) {
            this.carMaker = carMaker;
        }
    
        public CarMaker getCarMaker() {
            return this.getCarMaker();
        }
    
        public void setCarModel(CarModel carModel) {
            this.carModel = carModel;
        }
    
        public CarModel getCarModel() {
            return this.getCarModel();
        }
    
    
        public String getYear() {
            return year;
        }
    
        public void setYear(String year) {
            this.year = year;
        }
    }
    

    当我提出检索CarModel的请求时,我会收到一个回复​​,其中包含该模型的详细信息及其生产年份:

    {
        "id": 260248,
        "name": "Ulysse",
        "years": [
            "1994",
            "1995",
            "1996",
            "1997",
            "1998",
            "1999",
            "2000",
            "2001",
            "2004",
            "2005",
            "2006",
            "2007",
            "2008",
            "2009",
            "2010",
            "2011",
            "2012"
        ]
    }
    

    问题是当我尝试请求CarMaker制作的CarModel时:

    {     “id”:4152072,     “名字”:“宝马”,     “carModels”:[] }

    我注意到,如果我在MadeIn类上反转注释@StartNode和@EndNode,我会获得有关CarMaker制作的CarModel的信息,但是我将不再获得有关制作这些模型的年份的信息。

    {     “id”:4152072,     “名字”:“宝马”,     “carModels”:[         {             “id”:260852,             “名字”:“120”,             “年”:[]         },         {             “id”:261430,             “名字”:“Z18”,             “年”:[]         },         {             “id”:262044,             “名字”:“L7”,             “年”:[]         },

    我想知道我错过了什么,或者我做错了什么?

    提前感谢您的帮助。

    - MD

0 个答案:

没有答案