Skip to content

Commit

Permalink
DATAGRAPH-450 Updated HelloWorld example code
Browse files Browse the repository at this point in the history
  • Loading branch information
nickithewatt authored and jexp committed Mar 29, 2014
1 parent 532aeb3 commit 0136f45
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 92 deletions.
114 changes: 66 additions & 48 deletions README.textile
Expand Up @@ -125,18 +125,26 @@ h2. Spring configuration
* Configure Spring Data Neo4j for Neo4j in your application using the provided XML namespace:

<pre>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:datagraph="http://www.springframework.org/schema/data/graph"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/graph http://www.springframework.org/schema/data/graph/datagraph-1.0.xsd
">
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">

<context:spring-configured/>
<context:annotation-config/>
<datagraph:config storeDirectory="target/config-test"/>
<context:component-scan base-package="org.springframework.data.neo4j.examples.hellograph" />

<neo4j:config storeDirectory="target/neo4j-db-plain"
base-package="org.springframework.data.neo4j.examples.hellograph.domain"/>
<neo4j:repositories base-package="org.springframework.data.neo4j.examples.hellograph.repositories"/>

<tx:annotation-driven />
</beans>
</pre>

Expand All @@ -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 )
Expand Down Expand Up @@ -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.

<pre>
public interface WorldRepository extends GraphRepository<World>, NamedIndexRepository<World> {}
public interface WorldRepository extends GraphRepository<World> {}

@Repository
public class WorldRepositoryImpl implements WorldRepositoryExtension {
@Service
@Transactional
public class GalaxyService {

@Autowired
private WorldRepository worldRepository;
@Autowired
private WorldRepository worldRepository;

@Transactional
public Collection<World> 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<World> getAllWorlds() {
return worldRepository.findAll();
}

public World findWorldIdentifiedBy( long id ) {
return worldRepository.findById( id );
}
public World findWorldById(Long id) {
return worldRepository.findOne(id);
}

public Iterable<World> 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<World> findAllByNumberOfMoons(int numberOfMoons) {
return worldRepository.findAllByPropertyValue("moons", numberOfMoons);
}

public World findWorldNamed( String name ) {
return worldRepository.findByPropertyValue( "name", name );
}
public Collection<World> makeSomeWorlds() {
Collection<World> worlds = new ArrayList<World>();

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<World> findWorldsWithMoons( int moonCount ) {
return worldRepository.findAllByPropertyValue( "moons", "moons", moonCount );
}
}

</pre>


Expand Down
@@ -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
Expand All @@ -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
//
Expand All @@ -37,7 +41,8 @@ public Iterable<World> 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);
}
Expand Down

This file was deleted.

@@ -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;
Expand All @@ -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;

Expand Down
@@ -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<World> {}
Expand Up @@ -18,8 +18,9 @@
<context:annotation-config/>
<context:component-scan base-package="org.springframework.data.neo4j.examples.hellograph" />

<neo4j:config storeDirectory="target/neo4j-db-aspect" base-package="org.springframework.data.neo4j.examples.hellograph"/>
<neo4j:repositories base-package="org.springframework.data.neo4j.examples.hellograph"/>
<neo4j:config storeDirectory="target/neo4j-db-aspect"
base-package="org.springframework.data.neo4j.examples.hellograph.domain"/>
<neo4j:repositories base-package="org.springframework.data.neo4j.examples.hellograph.repositories"/>

<tx:annotation-driven mode="aspectj"/>
</beans>
Expand Down
Expand Up @@ -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;
Expand Down
@@ -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
Expand All @@ -27,7 +31,8 @@ public Iterable<World> 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);
}
Expand Down

This file was deleted.

@@ -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;
Expand All @@ -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;

Expand Down
@@ -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<World> {}
Expand Up @@ -23,8 +23,9 @@
<constructor-arg value="SubRef"/>
</bean>

<neo4j:config storeDirectory="target/neo4j-db-plain-subref" base-package="org.springframework.data.neo4j.examples.hellograph"/>
<neo4j:repositories base-package="org.springframework.data.neo4j.examples.hellograph"/>
<neo4j:config storeDirectory="target/neo4j-db-plain-subref"
base-package="org.springframework.data.neo4j.examples.hellograph.domain"/>
<neo4j:repositories base-package="org.springframework.data.neo4j.examples.hellograph.repositories"/>

</beans>

Expand Up @@ -18,8 +18,10 @@
<context:annotation-config/>
<context:component-scan base-package="org.springframework.data.neo4j.examples.hellograph" />

<neo4j:config storeDirectory="target/neo4j-db-plain" base-package="org.springframework.data.neo4j.examples.hellograph"/>
<neo4j:repositories base-package="org.springframework.data.neo4j.examples.hellograph"/>
<neo4j:config storeDirectory="target/neo4j-db-plain"
base-package="org.springframework.data.neo4j.examples.hellograph.domain"/>
<neo4j:repositories base-package="org.springframework.data.neo4j.examples.hellograph.repositories"/>

<tx:annotation-driven />
</beans>

Expand Up @@ -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;
Expand All @@ -22,9 +22,8 @@
@ContextConfiguration(locations = "classpath:/spring/helloWorldContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@TransactionConfiguration(defaultRollback = false)
public class GalaxyServiceTest {

@Autowired
private GalaxyService galaxyService;

Expand Down Expand Up @@ -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);
}
}
Expand Down
Expand Up @@ -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;
Expand Down

0 comments on commit 0136f45

Please sign in to comment.