Announcement Announcement Module
Collapse
No announcement yet.
Help using Spring Data Graph 1.0 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help using Spring Data Graph 1.0

    I've been using the last few milestones and Spring Data Graph RC1 with no issues. This morning, however, when I tried to plug in release 1.0, none of my NodeGraphRepository instances are getting initialized. This has all been working in the past, so I'm wondering if someone could explain what changed between Spring Data Graph RC1 and 1.0 that could affect this behavior. Here's a snippet of my sample PersonDao repository that I've been playing around with, and as alluded to earlier was working until now:

    Code:
    @Repository
    public class PersonDao
    {
    
      @Autowired
      private DirectGraphRepositoryFactory directGraphRepositoryFactory;
    
      private final DynamicRelationshipType OWNS = DynamicRelationshipType
          .withName("owns");
      private NodeGraphRepository<Person> graphRepository;
    
      @PostConstruct
      public void init()
      {
        graphRepository = directGraphRepositoryFactory
            .createNodeEntityRepository(Person.class);
      }
    
      public Person add(Person person)
      {
        return person.persist();
      }
    
      public List<Person> getAll()
      {
        List<Person> retVal = new LinkedList<Person>();
        graphRepository.findAll().iterator();
    
        Iterator<Person> itr = graphRepository.findAll().iterator();
    
        while (itr.hasNext())
        {
          retVal.add(itr.next());
        }
        return retVal;
      }
    }
    Here's the test case that fails. The call to personDao.getAll() fails with a NullPointerException when attempting to access the NodeGraphRepository instance (graphRepository variable).

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader = AnnotationConfigContextLoader.class, value = "org.mycompany.graph.config.AppConfig")
    public class PersonDaoTests
    {
      private Log logger = LogFactory.getLog(PersonDaoTests.class);
      @Autowired
      private PersonDao personDao;
    
      @Test
      @Transactional
      public void testGetAll()
      {
        Person p = new Person(1, "John Smith");
        personDao.add(p);
    
        List<Person> persons = personDao.getAll();
        assertThat(persons.size(), CoreMatchers.is(1));
        logger.info(persons);
      }
    }

  • #2
    UPDATE...I was able to get around this issue by changing the @PostConstruct to the following:

    Code:
      @PostConstruct
      public void init()
      {
        graphRepository = (NodeGraphRepository<Person>) directGraphRepositoryFactory
            .createGraphRepository(Person.class);
      }
    I'll have to do more reading to understand why this has changed.
    Last edited by jzcfk9; Apr 19th, 2011, 08:14 AM.

    Comment


    • #3
      Some API cleanup and simplification of repository.

      Normally you would declare your PersonRepository as an interface

      interface PersonRepository extends NodeGraphRepository<Person> {}

      and declare the <datagraph:repositories base-package="com.example.repository"/> in your config and get

      @Autowired PersonRepository personRepository;

      injected.

      Cheers

      Michael

      P.S: do you really need the cast? That shouldn't be the case. Will check it.

      Comment


      • #4
        OK, I added the <datagraph:repositories .../> config you suggested and defined my PersonRepository as you suggested:

        NOTE: I wasn't able to extend NodeGraphRepository, since PersonRepository is an interface.

        Code:
        @Repository
        public interface PersonRepository extends GraphRepository<Person>,
            NamedIndexRepository<Person>
        {}
        I then re-ran my tests, and the test that searches by person id was now failing:

        Code:
          @Test
          @Transactional
          public void testGetById()
          {
            Person p = new Person("1", "John Smith");
            Person person = personRepository.findByPropertyValue("principal_id_index", "id", "1");
            assertThat(person, CoreMatchers.equalTo(p));
            logger.info(person);
          }
        Code:
        Apr 20, 2011 7:19:12 AM org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
        INFO: @TestExecutionListeners is not present for class [class org.mycompany.graph.person.PersonDaoTests]: using defaults.
        Apr 20, 2011 7:19:12 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
        INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@743399: startup date [Wed Apr 20 07:19:12 EDT 2011]; root of context hierarchy
        Apr 20, 2011 7:19:12 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
        INFO: Loading XML bean definitions from class path resource [META-INF/spring/graphRepository-config.xml]
        Apr 20, 2011 7:19:13 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
        INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18fb1f7: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,appConfig,org.mycompany.graph.config.AppFeatures#0,org.springframework.data.graph.neo4j.config.Neo4jConfiguration#0,graphDatabaseContext,conversionService,graphRelationshipInstantiator,graphEntityInstantiator,directGraphRepositoryFactory,neo4jRelationshipBacking,relationshipEntityStateFactory,neo4jNodeBacking,nodeEntityStateFactory,transactionManager,configurationCheck,persistenceExceptionTranslator,org.mycompany.graph.config.DatabaseConfig#0,dataSource,graphDatabaseService,personRepository,org.springframework.data.repository.support.RepositoryInterfaceAwareBeanPostProcessor#0,appFeatures,databaseConfig,noteDao]; root of factory hierarchy
        Apr 20, 2011 7:19:13 AM org.springframework.transaction.jta.JtaTransactionManager checkUserTransactionAndTransactionManager
        INFO: Using JTA UserTransaction: org.neo4j.kernel.impl.transaction.UserTransactionImpl@1e78c96
        Apr 20, 2011 7:19:13 AM org.springframework.transaction.jta.JtaTransactionManager checkUserTransactionAndTransactionManager
        INFO: Using JTA TransactionManager: [email protected]
        Apr 20, 2011 7:19:13 AM org.springframework.test.context.transaction.TransactionalTestExecutionListener startNewTransaction
        INFO: Began transaction (1): transaction manager [[email protected]]; rollback [true]
        Apr 20, 2011 7:19:13 AM org.springframework.data.graph.neo4j.fieldaccess.DelegatingFieldAccessorFactory factoryForField
        INFO: Factory org.springframework.data.graph.neo4j.fieldaccess.OneToNRelationshipFieldAccessorFactory@3a9d95 used for field: private java.util.Set org.mycompany.graph.model.Person.aliases
        Apr 20, 2011 7:19:13 AM org.springframework.data.graph.neo4j.fieldaccess.DelegatingFieldAccessorFactory factoryForField
        INFO: Factory org.springframework.data.graph.neo4j.fieldaccess.PropertyFieldAccessorFactory@e99ce5 used for field: private java.lang.String org.mycompany.graph.model.acl.PrincipalImpl.id
        Apr 20, 2011 7:19:13 AM org.springframework.data.graph.neo4j.fieldaccess.DelegatingFieldAccessorFactory factoryForField
        INFO: Factory org.springframework.data.graph.neo4j.fieldaccess.PropertyFieldAccessorFactory@e99ce5 used for field: private java.lang.String org.mycompany.graph.model.acl.PrincipalImpl.name
        Apr 20, 2011 7:19:13 AM org.springframework.test.context.transaction.TransactionalTestExecutionListener endTransaction
        INFO: Rolled back transaction after test execution for test context [[TestContext@1b32627 testClass = PersonDaoTests, locations = array<String>['org.mycompany.graph.config.AppConfig'], testInstance = org.mycompany.graph.person.PersonDaoTests@8dcd5d, testMethod = testGetById@PersonDaoTests, testException = java.lang.AssertionError: 
        Expected: <Person [aliases=null, PrincipalImpl [id=1, name=John Smith]]>
             got: null
        ]]
        Here's my Person model again:

        Code:
        @NodeEntity
        public class Person extends PrincipalImpl
        {
          @RelatedTo(type = "alias", elementClass = Person.class)
          private Set<Person> aliases;
        }
        
        @NodeEntity
        public abstract class PrincipalImpl implements Principal
        {
          @Indexed(indexName = "principal_id_index")
          private String id;
          @Indexed(indexName = "principal_name_index")
          private String name;
        }
        Last edited by jzcfk9; Apr 20th, 2011, 06:39 AM.

        Comment


        • #5
          Nevermind, I have it figured out now. It would help if I first persisted the Person before I tried to retrieve it from the Repository!

          Comment


          • #6
            Great that you figured it out.
            I know automatic persistence is nice (in some cases) but annoying in others, so we went for the normal model of detached entities on creation.

            Keep the feedback coming

            Cheers

            Michael

            Comment

            Working...
            X