title: How can I make a node to be of multiple types?
body: <p>I'm doing a test movie project to learn neo4j and SDN and here is a problem I'm facing:<br /><br />
  As you know movie director may be a producer or a writer or even an actor. In my <strong>java</strong> class architecture I have <em>Person</em> superclass. And it has children: <em>Producer, Director, Actor</em> and <em>Writer</em>. All these children nodes are on the same level, thus they are incompatible types.<br /><br />
  While on the other hand, in <strong>neo4j</strong> I have nodes that are at the same time Producer, Director and Writer. <br /><br />
  <img src="https://i.stack.imgur.com/8whOB.png" alt="example of node that has several labels"><br />
  So I have a problem when I want to get all directors via repository.findAll() method (or via custom method with Cypher query). Spring tells me:</p>
  
  <blockquote>
    <p>java.lang.IllegalArgumentException: Can not set java.util.Set field
    com.test.db.domain.Producer.producedMovies to
    com.test.db.domain.Director</p>
  </blockquote>
  
  <p>I use Neo4j 2.0.1 and Spring Data Neo4j 3.0.0.RELEASE.</p>
  
  <p>What is the right way to solve such kind of issue?</p>
  
  <hr>
  
  <p>Update:<br />
  Here is the code: <br /></p>
  
  <pre class="lang-java prettyprint-override"><code>public class App {
      public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("service-beans.xml");
          Neo4jTemplate neo4jTemplate = context.getBean(Neo4jTemplate.class);
  
          DirectorRepository repository = context.getBean(DirectorRepository.class);
          try (Transaction transaction = neo4jTemplate.getGraphDatabase().beginTx()) {
              EndResult&lt;Director&gt; all = repository.findAll_Upd(); //repository.findAll();
              Iterator&lt;Director&gt; iterator = all.iterator();
              while(iterator.hasNext()) {
                  System.out.println(iterator.next());
              }
              transaction.success();
          }
      }
  }
  
  @Transactional
  public interface DirectorRepository extends GraphRepository&lt;Director&gt; {
      @Query("match(n:Director) return n")
      EndResult&lt;Director&gt; findAll_Upd();
  }
  
  @NodeEntity
  public class Person implements Comparable&lt;Person&gt; {
      @GraphId
      Long nodeId;
  //    @Indexed(unique=true)
      String id;
      @Indexed(indexType= IndexType.FULLTEXT, indexName = "people")
      String name;
      private Short born;
      private Date birthday;
      private String birthplace;
      private String biography;
      private Integer version;
      private Date lastModified;
      private String profileImageUrl;
  ...
  }
  
  public class Director extends Person {
      @Fetch @RelatedTo(elementClass = Movie.class, type = RelationshipConstants.DIRECTED)
      private Set&lt;Movie&gt; directedMovies = new HashSet&lt;&gt;();
  }
  
  public class Producer extends Person {
      @Fetch @RelatedTo(elementClass = Movie.class, type = RelationshipConstants.PRODUCED)
      private Set&lt;Movie&gt; producedMovies = new HashSet&lt;&gt;();
  }
  
  public class Actor extends Person  {
      @RelatedToVia(type = RelationshipConstants.ACTED_IN)
      List&lt;Role&gt; roles;
  }
  
  @NodeEntity
  public class Movie implements Comparable&lt;Movie&gt; {
      @GraphId
      Long nodeId;    
      @Indexed(unique = true)
      String id;    
      @Indexed(indexType= IndexType.FULLTEXT, indexName = "search")
      String title;    
      int released;    
      String tagline;    
      @Fetch @RelatedTo(type = RelationshipConstants.ACTED_IN, direction = INCOMING)
      Set&lt;Actor&gt; actors;    
      @Fetch @RelatedTo(type = RelationshipConstants.PRODUCED, direction = INCOMING)
      Set&lt;Producer&gt; producers;    
      @RelatedToVia(type = RelationshipConstants.ACTED_IN, direction = INCOMING)
      List&lt;Role&gt; roles;
  }
  </code></pre>
  
  <p>And the full Exception:</p>
  
  <blockquote>
    <p>Exception in thread "main"
    org.springframework.data.mapping.model.MappingException: Setting
    property producedMovies to null on Rob Reiner [null]  at
    org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.setProperty(SourceStateTransmitter.java:85)
      at
    org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.copyEntityStatePropertyValue(SourceStateTransmitter.java:91)
      at
    org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.access$000(SourceStateTransmitter.java:40)
      at
    org.springframework.data.neo4j.support.mapping.SourceStateTransmitter$2.doWithAssociation(SourceStateTransmitter.java:61)
      at
    org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:291)
      at
    org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.copyPropertiesFrom(SourceStateTransmitter.java:57)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityFetchHandler.fetchValue(Neo4jEntityFetchHandler.java:75)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityFetchHandler.fetch(Neo4jEntityFetchHandler.java:60)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl$1.doWithAssociation(Neo4jEntityConverterImpl.java:135)
      at
    org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:291)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.cascadeFetch(Neo4jEntityConverterImpl.java:125)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.loadEntity(Neo4jEntityConverterImpl.java:114)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.read(Neo4jEntityConverterImpl.java:104)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.read(Neo4jEntityPersister.java:170)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.createEntityFromState(Neo4jEntityPersister.java:189)
      at
    org.springframework.data.neo4j.support.Neo4jTemplate.createEntityFromState(Neo4jTemplate.java:223)
      at
    org.springframework.data.neo4j.fieldaccess.RelationshipHelper.createEntitySetFromRelationshipEndNodes(RelationshipHelper.java:150)
      at
    org.springframework.data.neo4j.fieldaccess.RelatedToFieldAccessor.createEntitySetFromRelationshipEndNodes(RelatedToFieldAccessor.java:86)
      at
    org.springframework.data.neo4j.fieldaccess.RelatedToCollectionFieldAccessorFactory$RelatedToCollectionFieldAccessor.getValue(RelatedToCollectionFieldAccessorFactory.java:76)
      at
    org.springframework.data.neo4j.fieldaccess.DefaultEntityState.getValue(DefaultEntityState.java:97)
      at
    org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.copyEntityStatePropertyValue(SourceStateTransmitter.java:90)
      at
    org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.access$000(SourceStateTransmitter.java:40)
      at
    org.springframework.data.neo4j.support.mapping.SourceStateTransmitter$2.doWithAssociation(SourceStateTransmitter.java:61)
      at
    org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:291)
      at
    org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.copyPropertiesFrom(SourceStateTransmitter.java:57)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.loadEntity(Neo4jEntityConverterImpl.java:112)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.read(Neo4jEntityConverterImpl.java:104)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.read(Neo4jEntityPersister.java:170)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.createEntityFromState(Neo4jEntityPersister.java:189)
      at
    org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.projectTo(Neo4jEntityPersister.java:216)
      at
    org.springframework.data.neo4j.support.Neo4jTemplate.projectTo(Neo4jTemplate.java:240)
      at
    org.springframework.data.neo4j.support.conversion.EntityResultConverter.doConvert(EntityResultConverter.java:73)
      at
    org.springframework.data.neo4j.conversion.DefaultConverter.convert(DefaultConverter.java:44)
      at
    org.springframework.data.neo4j.support.conversion.EntityResultConverter.convert(EntityResultConverter.java:165)
      at
    org.springframework.data.neo4j.conversion.QueryResultBuilder$1.convert(QueryResultBuilder.java:103)
      at
    org.springframework.data.neo4j.conversion.QueryResultBuilder$1.access$300(QueryResultBuilder.java:81)
      at
    org.springframework.data.neo4j.conversion.QueryResultBuilder$1$1.underlyingObjectToObject(QueryResultBuilder.java:121)
      at
    org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47)
      at com.test.util.App.main(App.java:34)  at
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)     at
    com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    Caused by: java.lang.IllegalArgumentException: Can not set
    java.util.Set field com.test.db.domain.Producer.producedMovies to
    com.test.db.domain.Director   at
    sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
      at
    sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
      at
    sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
      at
    sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75)
      at java.lang.reflect.Field.set(Field.java:741)  at
    org.springframework.util.ReflectionUtils.setField(ReflectionUtils.java:102)
      at
    org.springframework.data.mapping.model.BeanWrapper.setProperty(BeanWrapper.java:90)
      at
    org.springframework.data.mapping.model.BeanWrapper.setProperty(BeanWrapper.java:68)
      at
    org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.setProperty(SourceStateTransmitter.java:83)
      ... 43 more</p>
  </blockquote>
  
  <hr>
  
  <p>Update <br />
  I have also tried projectTo () method (<a href="http://docs.spring.io/spring-data/data-graph/snapshot-site/reference/html/#reference:programming-model:projection" rel="nofollow noreferrer">projections</a>), but still I get the same exception:</p>
  
  <pre class="lang-java prettyprint-override"><code>Director director = neo4jTemplate.projectTo(person, Director.class);
  </code></pre>