Results 1 to 9 of 9

Thread: Polymorphism with SDN

  1. #1
    Join Date
    Sep 2006
    Location
    Hartford, CT
    Posts
    145

    Default Polymorphism with SDN

    In brief, I'm wondering if there is a comprehensive example of polymorphic persistence with SDN. I've read all the documentation and can't find an example.

    In more detail...

    I'm using the simple mapping mode in 2.0.1.RELEASE. I have what I thought was a simple use case, but I'm having difficulty getting this to work properly in SDN...

    Users have many Badges (rewards that they have earned). Badge is abstract and there are many concrete subclasses. In case one wonders why this would be, badges aren't merely data. In my case, they contain complex logic that is specific to each subclass- for instance complex criteria for determining when a badge has been earned and other complex behaviors as well.

    Code:
    @NodeEntity
    public class User {
    
        @GraphId
        Long id
    
        @Fetch
        @RelatedTo(type = "HAS_BADGE", direction = Direction.OUTGOING)
        Set <Badge> badges = new HashSet <Badge> ()
    
        // etc...
    
    }
    
    @NodeEntity
    public abstract class Badge {
    
        @GraphId
        Long id
    
        // etc...
    
    }
    
    public class FooBadge extends Badge {
    
    }
    
    public interface UserRepository extends GraphRepository <User> { }
    The problem I am encountering is this...

    When I retrieve a user, it DOES have all his/her badges, but they're lacking their ids! This means that when I modify the user and save, SDN thinks these are NEW badge instances. Now I've got dupes! That's no good. (Because now I can't explore user relationships through badges in common.)

    If I move the @GraphId Long id down to the concrete subclasses, I get an error at startup that the dynamic UserRepository implementation can't be created because Badge has no id.

    If instead of moving the field, I duplicate/override it on the concrete subclass, I end up with two id fields where one gets set and the other does not. (I can observe this in the debugger.) But even in this case, the original problem leading to duplicates still exists.

    So this takes me back to the original question. Is there a comprehensive example somewhere of how to do polymorphic persistence with SDN? Or is it simply not supported? (Hoping that isn't the case.)

    Any and all assistance is greatly appreciated!
    Last edited by krancour; May 8th, 2012 at 03:14 PM.
    Kent Rancourt
    DevOps Engineer

  2. #2
    Join Date
    Aug 2011
    Posts
    22

    Default

    Code:
    @NodeEntity
    public class User {
    
        @GraphId // this seems to be applied to badges
    
        @Fetch
        @RelatedTo(type = "HAS_BADGE", direction = Direction.OUTGOING)
        Set <Badge> badges = new HashSet <Badge> ()
    
        // etc...
    
    }

  3. #3
    Join Date
    Sep 2006
    Location
    Hartford, CT
    Posts
    145

    Default

    Sorry... that was a typo in the post; not in the actual code.

    Editing my original post to fix that.
    Kent Rancourt
    DevOps Engineer

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

    Default

    Yes it is, b/c it _is_ on badges.

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

    Default

    Interesting,

    this should work w/o problems. The users's badges should also have their id's set when being loaded.

    Would it be possible to share a sample project with your classes and setup ?

    Thanks a lot

    Michael

  6. #6
    Join Date
    Sep 2006
    Location
    Hartford, CT
    Posts
    145

    Default

    Certainly, but it will be a day or two before I get around to it.

    Appreciate you looking at this.
    Kent Rancourt
    DevOps Engineer

  7. #7
    Join Date
    Sep 2006
    Location
    Hartford, CT
    Posts
    145

    Default

    Michael,

    I put together a very simple project that demonstrates the unexpected behavior I am observing. I attached it to a JIRA issue I opened just now.

    https://jira.springsource.org/browse/DATAGRAPH-235
    Kent Rancourt
    DevOps Engineer

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

    Default

    Thanks a lot for reporting it.

    Michael

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

    Default

    Kent,

    I looked at your test project, thanks again for providing it. You're right with 2.0.1.RELEASE it fails.

    If I update it to 2.1.0.RC1 though it all works as expected.

    With the availability of 2.1.0.RC1 and the imminent release of 2.1.0.RELEASE I'd rather not focus on bugfixing 2.0.1.RELASE and would like to ask you to upgrade your project to RC1 for the time being and the RELEASE after that.

    Thanks a lot and sorry for the inconvenience,

    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
  •