Spring Data Neo4j-复杂类型类成员上的关系属性 [英] Spring Data Neo4j - relationship properties on complex type class members

查看:445
本文介绍了Spring Data Neo4j-复杂类型类成员上的关系属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用@RelationshipEntity注释的类. 此类包含我定义的带有一些整数值的对象. 是否有可能以某种方式定义嵌套对象的成员将另存为关系的属性?

I have a class annotated with @RelationshipEntity. This class contains of object defined by me with some integer values. Is it possible somehow to define that members of the nested object will be saved as properties on the relationship?

Justyna.

推荐答案

是的,但是应该将它们转换为提供自定义Spring转换器的字符串.为了避免为每个需要嵌入的类声明一个转换器,可以扩展一个公共接口(甚至是一个空接口,仅用于声明转换器). 必须在SDN配置文件中声明转换器,如下所示:

Yes, but they should be converted to strings providing customized Spring converters. To avoid declaring a converter for each class you need to embed, you could extend a common interface (even an empty one, just to declare the converters). The converters must be declared in the SDN configuration file as follows:

<bean id="conversionService"
          class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="..."/>
                <bean class="..."/>
            </list>
        </property>
</bean>

您应该定义两个转换器,一个用于将对象转换为字符串,另一个用于将字符串转换为对象的相反转换. 例如,使用Gson:

You should define two converters, one for converting objects to strings and the other for the opposite conversion from string to objects. For example, using Gson:

final class ToStringConverterFactory implements ConverterFactory<MyClass, String> {

    @Override
    public <T extends String> Converter<MyClass, T> getConverter(Class<T> type) {
        return new ToStringConverter(type);
    }

    private final class ToStringConverter<E extends MyClass, S extends String> implements Converter<E, S> {

        private Class<S> stringType;

        public ToStringConverter(Class<S> stringType) {
            this.stringType = stringType;
        }

        @Override
        public S convert(E source) {
            if (source != null) {
                return (S) new Gson().toJson(source);
            } else {
                return null;
            }
        }
    }
}

final class ToObjectConverterFactory implements ConverterFactory<String, MyClass> {

    @Override
    public <T extends MyClass> Converter<String, T> getConverter(Class<T> type) {
        return new ToObjectConverter(type);
    }

    private final class ToObjectConverter<S extends String, E extends MyClass> implements Converter<S, E> {

        private Class<E> objectType;

        public ToObjectConverter(Class<E> objectType) {
            this.objectType = objectType;
        }

        @Override
        public E convert(S source) {
            if (source != null) {
                return (E) new Gson().fromJson(source, objectType);
            } else {
                return null;
            }
        }
    }
}

这篇关于Spring Data Neo4j-复杂类型类成员上的关系属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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