Announcement Announcement Module
Collapse
No announcement yet.
Spring data neo4j trying to save non-annotated attributes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring data neo4j trying to save non-annotated attributes

    I have a particular case where I want to have a class have an API where a NodeEntity is obtained "indirectly" from another NodeEntity directly related to that class.

    Basically, my model have Concepts which can influence (InfluenceRelationship) other Concepts. An InfluenceRelationship can be moderated by a Concept. Thus I've modelled InfluenceRelationship as a NodeEntity so I could represent this moderation by linking a Concept to an InfluenceRelationship (via an ModerationRelationship). Here is the code:

    Code:
    @NodeEntity
    public class Concept {
    
        @Fetch
        @RelatedTo(type = RelTypeConstants.TARGET_OF_INFLUENCE, direction = Direction.INCOMING)
        private Set<InfluenceRelationship> inInfluenceRelationships;
    
        @Fetch
        @RelatedTo(type = RelTypeConstants.MODERATOR_CONCEPT, direction = Direction.OUTGOING)
        private Set<ModerationRelationship> outModerationRelationships;
    }
    
    @NodeEntity
    public class InfluenceRelationship {
    
        @Fetch
        @RelatedTo(type = RelTypeConstants.SOURCE_OF_INFLUENCE, direction = Direction.INCOMING)
        private ValueConcept fromConcept;
    
        @Fetch
        @RelatedTo(type = RelTypeConstants.TARGET_OF_INFLUENCE, direction = Direction.OUTGOING)
        private VariableConcept toConcept;
    
        @Fetch
        @RelatedTo(type = RelTypeConstants.INFLUENCE_MODERATED, direction = Direction.INCOMING)
        private Set<ModerationRelationship> inModerationsRelationships;
    }
    
    @NodeEntity
    public class ModerationRelationship {
    
        @Fetch
        @RelatedTo(type = RelTypeConstants.MODERATOR_CONCEPT, direction = Direction.INCOMING)
        private VariableConcept fromConcept;
    
        @Fetch
        @RelatedTo(type = RelTypeConstants.INFLUENCE_MODERATED, direction = Direction.OUTGOING)
        private InfluenceRelationship toInfluenceRelationship;
    }
    Please notice that ModerationRelationship is also modelled as a @NodeEntity because it relates to other Nodes.

    Now the problem. I have created a method in Concept class which gets the ModerationRelationships which indirectly influence a Concept (i.e., "inModerationRelationships"). The code looks like this:

    Code:
        private transient Set<ModerationRelationship> inModerationRelationships;
        public Set<ModerationRelationship> getInModerationRelationships() {
            if(inModerationRelationships == null) {
                inModerationRelationships = new HashSet<>();
                for(InfluenceRelationship inInfluence : getInInfluenceRelationships()) {
                    for(ModerationRelationship inModeration : inInfluence.getInModerationsRelationships()) {
                        inModerationRelationships.add(inModeration);
                    }
                }
            }
            return inModerationRelationships;
        }
    If these method is present in the Concept class and I try to save an instance of it, then the Spring Data tries to invoke this method (I'm assuming that it is trying to do the "cascade save" since ModerationRelationship is annotated with @NodeEntity). However, as you could see the variable inModerationRelationships is transient. Plus, another strange behavior is that if the variable inModerationRelationships and the method getInModerationRelationships have different names (say, tempInModerationRelationships and getInModerationRelationships or even if I don't declare the temporary variable) then the method is not called anymore by spring data upon saving the Concept entity.
    Last edited by pasemes; Aug 25th, 2013, 11:27 AM.

  • #2
    Try to use the @Transient annotation from Spring-Data.

    The getter should not be called at all this was fixed some time ago, I think. It was an issue with the default Spring BeanWrapper that goes for methods first and fields later.

    Comment

    Working...
    X