Spring Data Neo4j 4-如何在课堂上实现复合属性? [英] Spring Data Neo4j 4 - How to implement Composite Attribute at Class?

查看:259
本文介绍了Spring Data Neo4j 4-如何在课堂上实现复合属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找复合属性"示例,但找不到随处可见的示例.我的课很简单:

I am looking for Composite Attribute example but can not find it everywhere. I have simple class :

@NodeEntity
public class MyClass{
@GraphId Long id;
Double diameter; 
//Property diameter should be from composite attribute (inDiameter and outDiameter)
}

MyClass具有属性直径,它应该是inDiameter或outDiameter中的值(可以在某些情况下同时用作值)

MyClass have property diameter, it should be value from inDiameter or outDiameter (can use both as value with some condition)

我还有另一个这样的班级:

And I have the other class like this:

@NodeEntity
public class MyClass2{
@GraphId Long id;
String name; 
//Property name should be from composite attribute (firstName and lastName)
}

我该如何实现?

我需要像这样为假直径创建类吗?

Do I need to create class for dummy diameter like this?

//without @NodeEntity
Class NominalDiameter{
     Double outsideDiameter
     Double insideDiameter
}

然后更改MyClass的属性:

And I change my property of MyClass :

Double diameter -> NominalDiameter diameter;

我正在使用Spring Boot父代1.4.1.RELEASE,neo4j-ogm 2.0.5,Spring Data Neo4j 4

I'm using Spring boot parent 1.4.1.RELEASE, neo4j-ogm 2.0.5, Spring Data Neo4j 4

推荐答案

在OGM的最新版本-2.1.0-SNAPSHOT中,有一个@CompositeAttributeConverter可用于此目的.

In the latest version of OGM - 2.1.0-SNAPSHOT there is a @CompositeAttributeConverter that can be used for this purpose.

将多个节点实体属性映射到一个类型的单个实例上的示例:

/**
* This class maps latitude and longitude properties onto a Location  type that encapsulates both of these attributes.
*/
public class LocationConverter implements CompositeAttributeConverter<Location> {

    @Override
    public Map<String, ?> toGraphProperties(Location location) {
        Map<String, Double> properties = new HashMap<>();
        if (location != null)  {
            properties.put("latitude", location.getLatitude());
            properties.put("longitude", location.getLongitude());
        }
        return properties;
    }

    @Override
    public Location toEntityAttribute(Map<String, ?> map) {
        Double latitude = (Double) map.get("latitude");
        Double longitude = (Double) map.get("longitude");
        if (latitude != null && longitude != null) {
            return new Location(latitude, longitude);
        }
        return null;
    }

}

要使用:

@NodeEntity
public class Person {

   @Convert(LocationConverter.class)
   private Location location;
   ...
}

要依赖此版本:

repositories {
    mavenLocal()
    mavenCentral()
    maven { url "http://m2.neo4j.org/content/repositories/snapshots/" }
    maven { url "https://repo.spring.io/libs-snapshot" }
}

这篇关于Spring Data Neo4j 4-如何在课堂上实现复合属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆