From 0136f45837fcee421b8b4c2dffd3f8bc2451b4af Mon Sep 17 00:00:00 2001 From: Nicki Watt Date: Sun, 16 Mar 2014 23:17:02 +0000 Subject: [PATCH] DATAGRAPH-450 Updated HelloWorld example code --- README.textile | 114 ++++++++++-------- .../examples/hellograph/GalaxyService.java | 17 ++- .../examples/hellograph/WorldRepository.java | 5 - .../hellograph/{ => domain}/World.java | 12 +- .../repositories/WorldRepository.java | 6 + .../resources/spring/helloWorldContext.xml | 5 +- .../hellograph/GalaxyServiceTest.java | 1 + .../examples/hellograph/GalaxyService.java | 13 +- .../examples/hellograph/WorldRepository.java | 5 - .../hellograph/{ => domain}/World.java | 12 +- .../repositories/WorldRepository.java | 6 + .../spring/helloWorldContext-subRef-TRS.xml | 5 +- .../resources/spring/helloWorldContext.xml | 6 +- .../hellograph/GalaxyServiceTest.java | 7 +- .../SubRefBasedTRSGalaxyServiceTest.java | 1 + 15 files changed, 123 insertions(+), 92 deletions(-) delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java rename spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/{ => domain}/World.java (80%) create mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/repositories/WorldRepository.java delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java rename spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/{ => domain}/World.java (80%) create mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/repositories/WorldRepository.java diff --git a/README.textile b/README.textile index d07bcd2396..4bc6249574 100644 --- a/README.textile +++ b/README.textile @@ -125,18 +125,26 @@ h2. Spring configuration * Configure Spring Data Neo4j for Neo4j in your application using the provided XML namespace:
-
+
 
+       xmlns:context="http://www.springframework.org/schema/context"
+       xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
+       xmlns:tx="http://www.springframework.org/schema/tx"
+       xsi:schemaLocation="
+       	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
+		http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd
+		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
+
+	
     
-    
+    
+
+    
+    
+
+    
 
 
@@ -148,10 +156,12 @@ h2. Graph entities @NodeEntity public class World { + // Uses default schema based index @Indexed private String name; - @Indexed( indexName = "moons" ) + // Uses legacy index mechanism + @Indexed(indexType = IndexType.SIMPLE) private int moons; @RelatedTo( type = "REACHABLE_BY_ROCKET", direction = Direction.BOTH, elementClass = World.class ) @@ -179,57 +189,65 @@ public class World { h2. Transactional services -* Create a repository or service to perform typical operations on your entities. The FinderFactory and Finder helper classes make searching easy for common use cases. The complete functionality is covered in the "reference manual":http://static.springsource.org/spring-data/data-neo4j/docs/current/reference/html/#programming-model. +* Create a repository or service to perform typical operations on your entities. The complete functionality is covered in the "reference manual":http://static.springsource.org/spring-data/data-neo4j/docs/current/reference/html/#programming-model.
-public interface WorldRepository extends GraphRepository, NamedIndexRepository {}
+public interface WorldRepository extends GraphRepository {}
 
-@Repository
-public class WorldRepositoryImpl implements WorldRepositoryExtension {
+@Service
+@Transactional
+public class GalaxyService {
 
-    @Autowired
-    private WorldRepository worldRepository;
+	@Autowired
+	private WorldRepository worldRepository;
 
-    @Transactional
-    public Collection makeSomeWorlds()  {
-        World earth = world( "Earth", 1 );
-        World mars = world( "Mars", 2 );
-        mars.addRocketRouteTo( earth );
+	public long getNumberOfWorlds() {
+		return worldRepository.count();
+	}
 
-        return Arrays.asList(
-            world( "Mercury", 0 ), world( "Venus", 0 ), earth, mars,
-            world( "Jupiter", 63 ), world( "Saturn", 62 ),
-            world( "Uranus", 27 ), world( "Neptune", 13 ));
-    }
+	public World createWorld(String name, int moons) {
+		return worldRepository.save(new World(name, moons));
+	}
 
-    private World world(String name, int moons) {
-        return new World(name, moons).persist();
-    }
+	public Iterable getAllWorlds() {
+		return worldRepository.findAll();
+	}
 
-    public World findWorldIdentifiedBy( long id )  {
-        return worldRepository.findById( id );
-    }
+	public World findWorldById(Long id) {
+		return worldRepository.findOne(id);
+	}
 
-    public Iterable findAllWorlds()  {
-        return worldRepository.findAll();
-    }
+    // This is using the schema based index
+	public World findWorldByName(String name) {
+		return worldRepository.findBySchemaPropertyValue("name", name);
+	}
 
-    public long countWorlds()  {
-        return worldRepository.count();
-    }
+    // This is using the legacy index
+	public Iterable findAllByNumberOfMoons(int numberOfMoons) {
+		return worldRepository.findAllByPropertyValue("moons", numberOfMoons);
+	}
 
-    public World findWorldNamed( String name ) {
-        return worldRepository.findByPropertyValue( "name", name );
-    }
+	public Collection makeSomeWorlds() {
+		Collection worlds = new ArrayList();
 
-    public World findWorldWithMoons( long moonCount ) {
-        return worldRepository.findByPropertyValue( "moons", "moons", moonCount );
-    }
+		// Solar worlds
+		worlds.add(createWorld("Mercury", 0));
+		worlds.add(createWorld("Venus", 0));
+
+        World earth = createWorld("Earth", 1);
+        World mars = createWorld("Mars", 2);
+        mars.addRocketRouteTo(earth);
+        worldRepository.save(mars);
+        worlds.add(earth);
+        worlds.add(mars);
+
+        // ... Create more worlds
+
+        return worlds;
+	}
 
-    public Iterable findWorldsWithMoons( int moonCount )  {
-        return worldRepository.findAllByPropertyValue( "moons", "moons", moonCount );
-    }
 }
+
 
diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java index 1442309682..de89b427eb 100644 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java @@ -1,13 +1,17 @@ package org.springframework.data.neo4j.examples.hellograph; -import java.util.ArrayList; -import java.util.Collection; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.neo4j.aspects.core.NodeBacked; +import org.springframework.data.neo4j.examples.hellograph.domain.World; +import org.springframework.data.neo4j.examples.hellograph.repositories.WorldRepository; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.Collection; @Service +@Transactional public class GalaxyService { @Autowired @@ -18,8 +22,8 @@ public long getNumberOfWorlds() { } public World createWorld(String name, int moons) { - // The lin below is just as valid within you code (it assumes) - // the aspectj compilcation has occured, however the cast to + // The line below is just as valid within you code (it assumes) + // the aspectj compilation has occurred, however the cast to // the NodeBacked class is being used here as its helpful to // IDE's which struggle with aspectj stuff // @@ -37,7 +41,8 @@ public Iterable getAllWorlds() { public World findWorldById(Long id) { return worldRepository.findOne(id); } - + + // This is using the schema based index public World findWorldByName(String name) { return worldRepository.findBySchemaPropertyValue("name", name); } diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java deleted file mode 100644 index 350acbf345..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.springframework.data.neo4j.repository.GraphRepository; - -public interface WorldRepository extends GraphRepository {} diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/domain/World.java similarity index 80% rename from spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java rename to spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/domain/World.java index a3bd618be0..523bbe158c 100644 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/domain/World.java @@ -1,11 +1,7 @@ -package org.springframework.data.neo4j.examples.hellograph; +package org.springframework.data.neo4j.examples.hellograph.domain; import org.neo4j.graphdb.Direction; -import org.springframework.data.neo4j.annotation.Fetch; -import org.springframework.data.neo4j.annotation.GraphId; -import org.springframework.data.neo4j.annotation.Indexed; -import org.springframework.data.neo4j.annotation.NodeEntity; -import org.springframework.data.neo4j.annotation.RelatedTo; +import org.springframework.data.neo4j.annotation.*; import org.springframework.data.neo4j.support.index.IndexType; import java.util.Set; @@ -16,10 +12,12 @@ public class World { @GraphId private Long id; - + + // Uses default schema based index @Indexed private String name; + // Uses legacy index mechanism @Indexed(indexType = IndexType.SIMPLE) private int moons; diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/repositories/WorldRepository.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/repositories/WorldRepository.java new file mode 100644 index 0000000000..73dfef6c67 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/repositories/WorldRepository.java @@ -0,0 +1,6 @@ +package org.springframework.data.neo4j.examples.hellograph.repositories; + +import org.springframework.data.neo4j.examples.hellograph.domain.World; +import org.springframework.data.neo4j.repository.GraphRepository; + +public interface WorldRepository extends GraphRepository {} diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/resources/spring/helloWorldContext.xml b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/resources/spring/helloWorldContext.xml index 57abf0fd36..fb1b1bd376 100644 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/resources/spring/helloWorldContext.xml +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/resources/spring/helloWorldContext.xml @@ -18,8 +18,9 @@ - - + + diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java index 8e4ad0cc35..e5a7dba5c2 100644 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java @@ -3,6 +3,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.neo4j.examples.hellograph.domain.World; import org.springframework.data.neo4j.support.Neo4jTemplate; import org.springframework.data.neo4j.support.node.Neo4jHelper; import org.springframework.test.annotation.Rollback; diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java index 93188c5018..f2ffc743ed 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java @@ -1,12 +1,16 @@ package org.springframework.data.neo4j.examples.hellograph; -import java.util.ArrayList; -import java.util.Collection; - import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.neo4j.examples.hellograph.domain.World; +import org.springframework.data.neo4j.examples.hellograph.repositories.WorldRepository; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.Collection; @Service +@Transactional public class GalaxyService { @Autowired @@ -27,7 +31,8 @@ public Iterable getAllWorlds() { public World findWorldById(Long id) { return worldRepository.findOne(id); } - + + // This is using the schema based index public World findWorldByName(String name) { return worldRepository.findBySchemaPropertyValue("name", name); } diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java deleted file mode 100644 index 350acbf345..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.springframework.data.neo4j.repository.GraphRepository; - -public interface WorldRepository extends GraphRepository {} diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/domain/World.java similarity index 80% rename from spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java rename to spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/domain/World.java index a3bd618be0..523bbe158c 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/domain/World.java @@ -1,11 +1,7 @@ -package org.springframework.data.neo4j.examples.hellograph; +package org.springframework.data.neo4j.examples.hellograph.domain; import org.neo4j.graphdb.Direction; -import org.springframework.data.neo4j.annotation.Fetch; -import org.springframework.data.neo4j.annotation.GraphId; -import org.springframework.data.neo4j.annotation.Indexed; -import org.springframework.data.neo4j.annotation.NodeEntity; -import org.springframework.data.neo4j.annotation.RelatedTo; +import org.springframework.data.neo4j.annotation.*; import org.springframework.data.neo4j.support.index.IndexType; import java.util.Set; @@ -16,10 +12,12 @@ public class World { @GraphId private Long id; - + + // Uses default schema based index @Indexed private String name; + // Uses legacy index mechanism @Indexed(indexType = IndexType.SIMPLE) private int moons; diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/repositories/WorldRepository.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/repositories/WorldRepository.java new file mode 100644 index 0000000000..73dfef6c67 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/repositories/WorldRepository.java @@ -0,0 +1,6 @@ +package org.springframework.data.neo4j.examples.hellograph.repositories; + +import org.springframework.data.neo4j.examples.hellograph.domain.World; +import org.springframework.data.neo4j.repository.GraphRepository; + +public interface WorldRepository extends GraphRepository {} diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/helloWorldContext-subRef-TRS.xml b/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/helloWorldContext-subRef-TRS.xml index 942f585e30..c90285f744 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/helloWorldContext-subRef-TRS.xml +++ b/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/helloWorldContext-subRef-TRS.xml @@ -23,8 +23,9 @@ - - + + diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/helloWorldContext.xml b/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/helloWorldContext.xml index 256f491b75..d6ec72e335 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/helloWorldContext.xml +++ b/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/helloWorldContext.xml @@ -18,8 +18,10 @@ - - + + + diff --git a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java index e5ccf7e87a..56a63d2fcb 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java +++ b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java @@ -3,13 +3,13 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.neo4j.examples.hellograph.domain.World; import org.springframework.data.neo4j.support.Neo4jTemplate; import org.springframework.data.neo4j.support.node.Neo4jHelper; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.BeforeTransaction; -import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; import java.util.Collection; @@ -22,9 +22,8 @@ @ContextConfiguration(locations = "classpath:/spring/helloWorldContext.xml") @RunWith(SpringJUnit4ClassRunner.class) @Transactional -@TransactionConfiguration(defaultRollback = false) public class GalaxyServiceTest { - + @Autowired private GalaxyService galaxyService; @@ -59,7 +58,7 @@ public void shouldFindWorldsById() { galaxyService.makeSomeWorlds(); for(World world : galaxyService.getAllWorlds()) { - World foundWorld = galaxyService.findWorldById(world.getId()); + World foundWorld = galaxyService.findWorldById(world.getId()); assertNotNull(foundWorld); } } diff --git a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/SubRefBasedTRSGalaxyServiceTest.java b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/SubRefBasedTRSGalaxyServiceTest.java index 03ef1f2696..4263ff70a3 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/SubRefBasedTRSGalaxyServiceTest.java +++ b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/SubRefBasedTRSGalaxyServiceTest.java @@ -3,6 +3,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.neo4j.examples.hellograph.domain.World; import org.springframework.data.neo4j.support.Neo4jTemplate; import org.springframework.data.neo4j.support.node.Neo4jHelper; import org.springframework.test.annotation.Rollback;