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