Announcement Announcement Module
Collapse
No announcement yet.
Neo4J: Write custom converters to save non-primitives as node-properties Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Neo4J: Write custom converters to save non-primitives as node-properties

    Hi,

    i want to use joda-time[1] as Datetime-Lib for handling date/time-purposes within my J2EE-App. Additional, i want to save a "Currency" as a node-property:

    Code:
    import org.joda.time.DateTime;
    import java.util.Currency;
    
    @NodeEntity
    public class Company{
      protected DateTime createdAt;
      protected Currency currency;
    }
    Both values, "createdAt" and "currency", are not saved to the graph.

    The Spring Data Neo4J-Documentation[2] explains me the reason: The values are not primitive, and i guess there are not spring-converters for them. So I'd like to wirte converters for this types, how can I do this?

    Btw. both types are serializable[3][4] - does spring data neo4j not try to save properties by serializing/unserializing properties, if there are non-primitive but implement Serializable?

    I am using Sprint data neo4j 2.1RC2 in standalone-mode.

    Greetz,
    Markus

    [1] http://joda-time.sourceforge.net/
    [2] http://static.springsource.org/sprin...e/html/#d5e825
    [3] http://joda-time.sourceforge.net/apidocs/index.html
    [4] http://docs.oracle.com/javase/6/docs.../Currency.html
    Last edited by MarkusSchulte; Aug 17th, 2012, 06:40 AM.

  • #2
    AFAIK, custom conversion is still buggy. If you search SDN JIRA, you will find some issues related to it.

    BTW, I also tried using Joda-Time lib, and no success. In the mean time, I perform custom conversion in my getters/setters, whereas the mapped field in my domain class is plain long primitive, and it has to be named differently than getter/setter,

    Comment


    • #3
      Markus,

      As you said, this should work, as documented in http://static.springsource.org/sprin...e/html/#d5e825.

      However, currently you can only convert to String and back (see https://jira.springsource.org/browse/DATAGRAPH-286). And also you have to manually register your converter, something like:
      Code:
      @Configuration
          @EnableNeo4jRepositories
          static class TestConfig extends Neo4jConfiguration {
              @Bean
              GraphDatabaseService graphDatabaseService() {
                  return new ImpermanentGraphDatabase();
              }
      
              @Bean
              protected ConversionService neo4jConversionService() throws Exception {
                  ConversionService conversionService = super.neo4jConversionService();
                  conversionService.addConverter(new MyAwesomeConverter());
                  conversionService.addConverter(new MyAwesomeReverseConverter());
                  return conversionService;
              }
          }
      However, monitor the jira ticket for updates.

      Comment


      • #4
        Hi,

        thanks to your replies. I decided to implement as vmarcinko suggests - for time IMO the easiest way. But not the cleanest.

        Btw, the Issue https://jira.springsource.org/browse/DATAGRAPH-286 will be resolved in RC3.

        Comment


        • #5
          ... And RC3 came out literally an hour ago

          Happy hacking!

          Lasse

          Comment

          Working...
          X