Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Using neo4j without magic

  1. #1
    Join Date
    Aug 2011
    Posts
    22

    Default Using neo4j without magic

    Code:
    package org.springframework.data.neo4j.examples.hellograph;
    
    import org.neo4j.graphdb.GraphDatabaseService;
    import org.neo4j.kernel.EmbeddedGraphDatabase;
    import org.springframework.data.neo4j.core.GraphDatabase;
    import org.springframework.data.neo4j.repository.GraphRepository;
    import org.springframework.data.neo4j.support.DelegatingGraphDatabase;
    import org.springframework.data.neo4j.support.Neo4jTemplate;
    
    public class App {
        public static void main( String[] args ) throws Exception {
        	GraphDatabaseService gds = new EmbeddedGraphDatabase("testdb");
        	GraphDatabase gd = new DelegatingGraphDatabase(gds);
        	Neo4jTemplate neo4jTemplate = new Neo4jTemplate(gd);    	
        	neo4jTemplate.postConstruct();
        	
            final GraphRepository<World> repo = neo4jTemplate.repositoryFor(World.class);        
            
            World w = new World("Andrey", 123);
    	repo.save(w);
    		        
    	for(World ww : repo.findAll()) {
    		System.out.printf("w[%s,%d]", ww.getName(), ww.getMoons());
    	}
        }
    }
    This says:
    Code:
    Exception in thread "main" org.neo4j.graphdb.NotInTransactionException
    	at org.neo4j.kernel.impl.persistence.PersistenceManager.getResource(PersistenceManager.java:252)
    	at org.neo4j.kernel.impl.persistence.PersistenceManager.nodeCreate(PersistenceManager.java:155)
    	at org.neo4j.kernel.impl.core.NodeManager.createNode(NodeManager.java:213)
    	at org.neo4j.kernel.AbstractGraphDatabase.createNode(AbstractGraphDatabase.java:906)
    	at org.springframework.data.neo4j.support.DelegatingGraphDatabase.createNode(DelegatingGraphDatabase.java:89)
    	at org.springframework.data.neo4j.support.mapping.EntityStateHandler.useOrCreateState(EntityStateHandler.java:131)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.write(Neo4jEntityConverterImpl.java:145)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.write(Neo4jEntityPersister.java:176)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:238)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:227)
    	at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:311)
    	at org.springframework.data.neo4j.repository.AbstractGraphRepository.save(AbstractGraphRepository.java:108)
    	at org.springframework.data.neo4j.examples.hellograph.App.main(App.java:20)
    How do I make it work?

    Spring Data Neo4j version is 2.1.0.BUILD-SNAPSHOT

  2. #2
    Join Date
    Aug 2011
    Posts
    22

    Default

    It feels like my "how do I make it work" sounds too newbish, so nobody's going to answer :-)
    Let me clarify. I just need help making Spring Data Neo4j _externally_ use as little Spring as possible. None of it ideally.

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

    Default

    You can start, stop your transactions manually around the repository methods using Neo4jTemplate

    The repositories and other parts of SDN use the Spring stuff heavily though.

    If you want to use object-graph-mapping w/o Spring you might want to look into jo4neo.

    Code:
    Transaction tx = neo4jTemplate.beginTx();
    try {
       // mutating neo4j operations here & repository methods
       tx.success();
    } finally {
       tx.finish();
    }

  4. #4
    Join Date
    Aug 2011
    Posts
    22

    Default

    Thanks for your answer, Michael. Here's the complete code that includes your snippet:

    Code:
    package org.springframework.data.neo4j.examples.hellograph;
    
    import org.neo4j.graphdb.GraphDatabaseService;
    import org.neo4j.graphdb.Transaction;
    import org.neo4j.kernel.EmbeddedGraphDatabase;
    import org.springframework.data.neo4j.core.GraphDatabase;
    import org.springframework.data.neo4j.repository.GraphRepository;
    import org.springframework.data.neo4j.support.DelegatingGraphDatabase;
    import org.springframework.data.neo4j.support.Neo4jTemplate;
    
    public class App {
        public static void main( String[] args ) throws Exception {
        	GraphDatabaseService gds = new EmbeddedGraphDatabase("testdb");
        	GraphDatabase gd = new DelegatingGraphDatabase(gds);
        	Neo4jTemplate neo4jTemplate = new Neo4jTemplate(gd);    	
        	neo4jTemplate.postConstruct();
        	
        	final GraphRepository<World> repo = neo4jTemplate.repositoryFor(World.class);    
            
            Transaction tx = neo4jTemplate.beginTx();
            try {
                World w = new World("Andrey", 123);
                repo.save(w);
        			        
                for(World ww : repo.findAll()) {
                  System.out.printf("w[%s,%d]", ww.getName(), ww.getMoons());
                }
    
                tx.success();
            } finally {
                tx.finish();
            }	    
        }
    }
    Here's what it says this time:
    Code:
    Exception in thread "main" java.lang.NullPointerException
    	at org.springframework.data.neo4j.support.Neo4jTemplate.beginTx(Neo4jTemplate.java:289)
    	at org.springframework.data.neo4j.examples.hellograph.App.main(App.java:20)

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

    Default

    Oh, sorry, if you don't use the spring config/PlatformTransactionManager you have to use the underlying Neo4j transaction system the template still uses the Spring-Tx-Manager.

    Code:
    Transaction tx= gds.beginTx();

  6. #6
    Join Date
    Aug 2011
    Posts
    22

    Default

    Thanks for your support, Michael. After applying your last snippet:

    Code:
    Exception in thread "main" java.lang.NullPointerException
    	at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.getTemplate(SourceStateTransmitter.java:82)
    	at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.copyPropertiesTo(SourceStateTransmitter.java:116)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.write(Neo4jEntityConverterImpl.java:149)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.write(Neo4jEntityPersister.java:176)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:238)
    	at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:227)
    	at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:311)
    	at org.springframework.data.neo4j.repository.AbstractGraphRepository.save(AbstractGraphRepository.java:108)
    	at org.springframework.data.neo4j.examples.hellograph.App.main(App.java:23)
    (line 23 is a call to repo.save(w) )

    Any other ideas? :-)

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

    Default

    This should be normally set up correctly with the Template.postConstruct();

    I'll have a look at it.

    Thanks for pointing it out. I'll add a test similar to your app to SDN (there is a open ticket that should resolve this issue anyway.)

    Perhaps you can comment on that and link to this thread: https://jira.springsource.org/browse/DATAGRAPH-206

  8. #8
    Join Date
    Aug 2011
    Posts
    22

    Default

    Thanks, Michael - I'll be waiting for the fix.

  9. #9
    Join Date
    Apr 2012
    Posts
    8

    Default

    Hello!

    I ran into a similar problem.

    My Main class is:
    Code:
    package neo4jSpringDataTestPackage;
    
    import org.neo4j.graphdb.GraphDatabaseService;
    import org.neo4j.graphdb.Transaction;
    import org.neo4j.kernel.EmbeddedGraphDatabase;
    import org.springframework.data.neo4j.core.GraphDatabase;
    import org.springframework.data.neo4j.support.DelegatingGraphDatabase;
    import org.springframework.data.neo4j.support.Neo4jTemplate;
    
    public class Main {
        private static Neo4jTemplate template;
        
        public static void main(String[] args) {
            GraphDatabaseService gds = new EmbeddedGraphDatabase("testdb");
            try {
                GraphDatabase gd = new DelegatingGraphDatabase(gds);
                template = new Neo4jTemplate(gd);
                template.postConstruct();
                
                Transaction tx = gds.beginTx();
                try {
                    template.save(new Movie("1", "Test Movie")); 
                    tx.success();
                } finally {
                    tx.finish();
                }
            } finally {
                gds.shutdown();
            }     
        }
    }
    I use a simpler version of the Movie class from the example projects:
    Code:
    package neo4jSpringDataTestPackage;
    
    import org.springframework.data.neo4j.annotation.*;
    import org.springframework.data.neo4j.support.index.IndexType;
    
    @NodeEntity
    public class Movie {
        @GraphId Long nodeId;
    
        @Indexed
        String id;
    
        @Indexed(indexType=IndexType.FULLTEXT, indexName = "search")
        String title;
    
        public Movie() {
        }
    
        public Movie(String id, String title) {
            this.id = id;
            this.title = title;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
    
            Movie movie = (Movie) o;
            if (nodeId == null) return super.equals(o);
            return nodeId.equals(movie.nodeId);
    
        }
    
        @Override
        public int hashCode() {
            return nodeId != null ? nodeId.hashCode() : super.hashCode();
        }
    
    }
    I get the following exception using both the *.2.0.1.RELEASE and the *.2.1.0.M1 jar files.
    Code:
    Exception in thread "main" java.lang.NullPointerException
        at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.getTemplate(SourceStateTransmitter.java:82)
        at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.copyPropertiesTo(SourceStateTransmitter.java:116)
        at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.write(Neo4jEntityConverterImpl.java:149)
        at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.write(Neo4jEntityPersister.java:176)
        at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:238)
        at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:227)
        at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:295)
        at neo4jSpringDataTestPackage.Main.main(Main.java:22)
    How can I get round this problem?

    Thanks,
    szarnyasg
    Last edited by szarnyasg; Apr 29th, 2012 at 07:07 PM. Reason: mixed tabs/spaces in the code

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

    Default

    Getting the template as a standalone (not injected from the spring context) component set up is currently worked on.

    Can you try until then have it injected from the spring context?

    Thanks

    Michael

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
  •