Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: Using neo4j without magic

  1. #11
    Join Date
    Apr 2012
    Posts
    8

    Default

    Thanks for the quick answer! It worked with dependency injection (I based the code on the cineast project).

    Thanks to yesterdays release (http://www.springsource.org/node/3547), the instantiation of the Neo4jTemplate class now works correctly. However, I'm unable to start a transaction---both the GraphDatabaseService and the Neo4jTemplate class' beginTx() throw a NullPointerException.
    I noticed the postConstruct() method is missing, so I assume Neo4jTemplate's constructor takes care of that automatically.

    Code:
            GraphDatabaseService gds = new EmbeddedGraphDatabase("testdb");
            try {
                GraphDatabase gd = new DelegatingGraphDatabase(gds);
                template = new Neo4jTemplate(gd);
                
                Transaction tx = gds.beginTx();
                // or
                Transaction tx = template.beginTx();
                try {
                    template.save(new Movie("1", "Test Movie")); 
                    tx.success();
                } finally {
                    tx.finish();
                }
            } finally {
                gds.shutdown();
            }
    gds.beginTx() throws:
    Code:
    Exception in thread "main" java.lang.NullPointerException
    	at org.springframework.data.neo4j.support.Neo4jTemplate.beginTx(Neo4jTemplate.java:277)
    	at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.copyPropertiesTo(SourceStateTransmitter.java:112)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.write(Neo4jEntityConverterImpl.java:150)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.write(Neo4jEntityPersister.java:177)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:239)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:228)
    	at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:291)
    	at neo4jSpringDataTestPackage.Main.main(Main.java:30)
    template.beginTx() throws:
    Code:
    Exception in thread "main" java.lang.NullPointerException
    	at org.springframework.data.neo4j.support.Neo4jTemplate.beginTx(Neo4jTemplate.java:277)
    	at neo4jSpringDataTestPackage.Main.main(Main.java:28)
    I resolved the dependencies with Maven, version set to 2.1.0.BUILD-SNAPSHOT.

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

    Default

    Thanks for pointing it out, it will be fixed in the final 2.1.0.RELEASE

    see: https://jira.springsource.org/browse/DATAGRAPH-233

    Michael

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

    Default

    It's fixed please check.

    Michael

  4. #14
    Join Date
    Apr 2012
    Posts
    8

    Default

    Can you please give me a hint about which plugin:goal to use to build the https://github.com/SpringSource/spring-data-neo4j project with mvn? Thanks!

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

    Default

    You mean except mvn clean install with Maven3 ?

  6. #16
    Join Date
    Apr 2012
    Posts
    8

    Default

    Thanks! The build ran successfully with "mvn clean install". I created the following code in the spring-data-neo4j-hello-worlds project:

    Code:
    try {
        GraphDatabaseService graphDatabaseService =  new EmbeddedGraphDatabase("testdb");
        GraphDatabase graphDatabase = new DelegatingGraphDatabase(graphDatabaseService);
        PlatformTransactionManager transactionManager = new JtaTransactionManager(new SpringTransactionManager(graphDatabaseService));
        Neo4jTemplate template = new Neo4jTemplate(graphDatabase, transactionManager);
        Transaction tx = template.beginTx();
        
        // (1)
        Node n = template.createNode();
        n.setProperty("prop", "some value");
        // (2)
        template.save(new Movie("1", "Test Movie"));
        
        tx.success();
        tx.finish();
    } catch (Exception e) {
        e.printStackTrace();
    }
    (1) on its own runs fine, I can query the node from Neoclipse (using Cypher), but (2) throws a NullPointerException:

    Code:
    java.lang.IllegalArgumentException: java.lang.NullPointerException
    	at org.springframework.data.neo4j.support.mapping.AbstractConstructorEntityInstantiator.createEntityFromState(AbstractConstructorEntityInstantiator.java:63)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedInstantiator.createEntityFromState(Neo4jEntityPersister.java:134)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedInstantiator.createEntityFromState(Neo4jEntityPersister.java:121)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.read(Neo4jEntityConverterImpl.java:85)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.read(Neo4jEntityPersister.java:169)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.createEntityFromState(Neo4jEntityPersister.java:187)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:240)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:228)
    	at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:296)
    	at org.springframework.data.neo4j.examples.hellograph.TestClass.testTemplate(TestClass.java:28)
    	at org.springframework.data.neo4j.examples.hellograph.App.main(App.java:24)
    Caused by: java.lang.NullPointerException
    	at org.springframework.data.neo4j.support.node.NodeEntityInstantiator.setPersistentState(NodeEntityInstantiator.java:46)
    	at org.springframework.data.neo4j.support.node.NodeEntityInstantiator.setState(NodeEntityInstantiator.java:42)
    	at org.springframework.data.neo4j.support.node.NodeEntityInstantiator.setState(NodeEntityInstantiator.java:31)
    	at org.springframework.data.neo4j.support.mapping.AbstractConstructorEntityInstantiator$2.create(AbstractConstructorEntityInstantiator.java:110)
    	at org.springframework.data.neo4j.support.mapping.AbstractConstructorEntityInstantiator.createEntityFromState(AbstractConstructorEntityInstantiator.java:56)
    	... 10 more
    Any ideas why? Thanks in advance.

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

    Default

    Thanks for pointing it out, I'll try to look at it but I'm on vacation till Thursday, so no promises

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

    Default

    fixed in SNAPSHOT

  9. #19
    Join Date
    Apr 2012
    Posts
    8

    Default

    Thanks! The following is working fine:
    Code:
    try {
        GraphDatabaseAPI graphDatabaseService =  new EmbeddedGraphDatabase("testdb");
        GraphDatabase graphDatabase = new DelegatingGraphDatabase(graphDatabaseService);
        PlatformTransactionManager transactionManager = new JtaTransactionManager(new SpringTransactionManager(graphDatabaseService));
        Neo4jTemplate template = new Neo4jTemplate(graphDatabase, transactionManager);
        Transaction tx = template.beginTx();
        
        template.save(new Movie("Node 1", "Test 1"));
        template.save(new Movie("Node 2", "Test 2"));
        
        tx.success();
        tx.finish();
    
        graphDatabaseAPI.shutdown();
    } catch (Exception e) {
        e.printStackTrace();
    }
    1) Are there any alternatives to the deprecated beginTx()?

    Neoclipse doesn't visualize the graph, but a simple Cypher query lists the nodes:

    Code:
    [{"n":{"relation":[],"id":0,"propertyMap":{}}},{"n":{"relation":[],"id":1,"propertyMap":{"moons":0,"b":"Test 1","__type__":"Movie","a":"Node 1"}}},{"n":{"relation":[],"id":2,"propertyMap":{"moons":0,"b":"Test 2","__type__":"Movie","a":"Node 2"}}}]
    2) I guess it has to do something with the #0 root node. How can I visualize at least one of the Movie nodes?
    Last edited by szarnyasg; Jul 9th, 2012 at 05:27 AM.

  10. #20
    Join Date
    Apr 2012
    Posts
    8

    Default

    I think I found the answer for my questions.

    1) Use the GraphDatabaseAPI's beginTx() method:

    Code:
    try {
        GraphDatabaseAPI graphDatabaseAPI =  new EmbeddedGraphDatabase("testdb");
        GraphDatabase graphDatabase = new DelegatingGraphDatabase(graphDatabaseAPI);
        PlatformTransactionManager transactionManager = new JtaTransactionManager(new SpringTransactionManager(graphDatabaseAPI));
        Neo4jTemplate template = new Neo4jTemplate(graphDatabase, transactionManager);
        Transaction tx = graphDatabaseAPI.beginTx();
        
        template.save(new Movie("Node 1", "Test 1"));
        template.save(new Movie("Node 2", "Test 2"));
        
        tx.success();
        tx.finish();
        
        graphDatabaseAPI.shutdown();
    } catch (Exception e) {
        e.printStackTrace();
    }
    2) The graph must contain a connected subgraph in order to be visualized.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •