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

  • loki2302
    started a topic Using neo4j without magic

    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

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

    Leave a comment:


  • szarnyasg
    replied
    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, 05:27 AM.

    Leave a comment:


  • MichaelHunger
    replied
    fixed in SNAPSHOT

    Leave a comment:


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

    Leave a comment:


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

    Leave a comment:


  • MichaelHunger
    replied
    You mean except mvn clean install with Maven3 ?

    Leave a comment:


  • szarnyasg
    replied
    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!

    Leave a comment:


  • MichaelHunger
    replied
    It's fixed please check.

    Michael

    Leave a comment:


  • MichaelHunger
    replied
    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

    Leave a comment:


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

    Leave a comment:


  • MichaelHunger
    replied
    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

    Leave a comment:


  • szarnyasg
    replied
    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

    Leave a comment:


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

    Leave a comment:


  • MichaelHunger
    replied
    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

    Leave a comment:

Working...
X