Announcement Announcement Module
Collapse
No announcement yet.
Using neo4j without magic Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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();
      }

      Comment


      • #4
        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)

        Comment


        • #5
          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();

          Comment


          • #6
            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? :-)

            Comment


            • #7
              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

              Comment


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

                Comment


                • #9
                  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, 07:07 PM. Reason: mixed tabs/spaces in the code

                  Comment


                  • #10
                    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

                    Comment


                    • #11
                      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.

                      Comment


                      • #12
                        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

                        Comment


                        • #13
                          It's fixed please check.

                          Michael

                          Comment


                          • #14
                            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!

                            Comment


                            • #15
                              You mean except mvn clean install with Maven3 ?

                              Comment

                              Working...
                              X