Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Spring Data Neo4j - Handling simple objects as NodeEntity properties

  1. #1
    Join Date
    Aug 2008
    Posts
    9

    Default Spring Data Neo4j - Handling simple objects as NodeEntity properties

    Hi,

    I'm just playing around with spring-data neo4j ... Suppose I have a Profile class containing a set of Address
    @NodeEntity
    class Profile {
    @GraphId Long id;
    String name;
    Set<Address> addresses;
    }
    class Address {
    String doorNumber;
    String street;
    String city;
    }

    I want to be able to build a Profile object, and using a ProfileRepository, call repository.save(myProfile) and have the addresses automatically saved. But Address is a simple object. Do I have to make Address an NodeEntity and give it an ID property ? I tried it and when I get back the Profile object back from the repository, there is one address in the list and all the fields are null except ID.

    What are my options ?

    Thanks

  2. #2
    Join Date
    Jan 2011
    Location
    Dresden, Germany
    Posts
    525

    Default

    You can add a Spring-Converter to your spring config which is able to convert between Address and String e.g to JSON.
    Code:
    // Could be either a GenericConverter or a Converter<Address,String> + Converter<String,Address>
    
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
            <property name="converters">
                <set>
                    <bean class="org.example.converter.AddressToStringConverter"/>
                </set>
            </property>
        </bean>
    Michael

  3. #3
    Join Date
    Aug 2008
    Posts
    9

    Default

    Thanks Michael.
    I will try the conversion solution, but I still have a problem using both classes (Profile, Address) as NodeEntities. The case is simpler now, Profile only contains one Address annotated with @RelatedTo.

    @NodeEntity
    class Profile {
    @GraphId Long id;
    String name;
    @RelatedTo
    Address address;
    }
    @NodeEntity
    class Address {
    @GraphId Long id;
    String doorNumber;
    String street;
    String city;
    }

    If I retrieve a profile by id and call profile.getAddress, the Address returned only have the id property set, everything else is null. If I retrieve the Address object directly by id using the AddressRepository, all the data is there. It's working fine in the advanced aspectj mode, but when using the simpler pojo mode, I get a bunch of null fields. Any thoughts ?

  4. #4
    Join Date
    Aug 2008
    Posts
    9

    Default

    I created a small project with a test to illustrate the problem.

    https://github.com/dreamagex/neo4j-profileaddress-test

  5. #5
    Join Date
    Jan 2011
    Location
    Dresden, Germany
    Posts
    525

    Default

    If you use simple mapping mode, you have to add @Fetch to the Address or the Collection<Address> field to be loaded eagerly.

    Sorry for not seeing that as the issue immediately.


    Michael

  6. #6
    Join Date
    Aug 2008
    Posts
    9

    Default

    I thought the address field would have been replaced with a proxy for lazy loading ? Because the way I see it, if you don't put @Fetch on a field, it's pretty useless, isn't it ?

  7. #7
    Join Date
    Jan 2011
    Location
    Dresden, Germany
    Posts
    525

    Default

    I didn't want to go into the ugly realm of lazy proxies.

    You can always load those objects/collections with template.fetch() later if you need them.

    Michael

  8. #8
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    You know, I have tried template.fetch before and that didn't load my associated objects. I had to do a separate Cypher query to get the associated objects myself.

    Mark

  9. #9
    Join Date
    Jan 2011
    Location
    Dresden, Germany
    Posts
    525

    Default

    Do you have a test-case for that?

    It should do it.

    Michael

  10. #10
    Join Date
    Aug 2008
    Posts
    9

    Default

    I updated my example with Neo4jTemplate.fetch and it's working just fine. Thanks Michael.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •