Skip to content
Browse files

Bringing spring configuration to life

  • Loading branch information...
1 parent a3d4b27 commit c6ca6571b20e680e794654fd43ab1c17f5a266c5 Michael Albers committed Apr 23, 2012
View
8 spring-neo4j-example/pom.xml
@@ -23,14 +23,20 @@
<artifactId>spring-data-neo4j</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
-
+
<!-- Neo4j -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>${neo4j.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.neo4j</groupId>
+ <artifactId>neo4j-cypher</artifactId>
+ <version>${neo4j.version}</version>
+ </dependency>
+
<!-- Testing -->
<dependency>
<groupId>junit</groupId>
View
14 spring-neo4j-example/src/main/java/de/flitzr/spring/neo4j/example/SimpleNodeExample.java
@@ -3,21 +3,15 @@
*/
package de.flitzr.spring.neo4j.example;
-import java.io.File;
-import java.util.Iterator;
-
import org.neo4j.cypher.ExecutionEngine;
import org.neo4j.cypher.ExecutionResult;
-import org.neo4j.graphdb.DynamicRelationshipType;
-import org.neo4j.graphdb.GraphDatabaseService;
-import org.neo4j.graphdb.Node;
-import org.neo4j.graphdb.Relationship;
-import org.neo4j.graphdb.RelationshipType;
-import org.neo4j.graphdb.Transaction;
+import org.neo4j.graphdb.*;
import org.neo4j.kernel.EmbeddedGraphDatabase;
-
import scala.Console;
+import java.io.File;
+import java.util.Iterator;
+
/**
* Create nodes in Neo4j (without spring-data)
*
View
91 spring-neo4j-example/src/main/java/de/flitzr/spring/neo4j/example/SpringExample.java
@@ -3,17 +3,23 @@
*/
package de.flitzr.spring.neo4j.example;
-import java.util.HashSet;
-import java.util.Set;
-
+import de.flitzr.spring.neo4j.example.dao.ArtifactRepository;
+import de.flitzr.spring.neo4j.example.model.Artifact;
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.graphdb.Node;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.data.neo4j.conversion.Result;
+import org.springframework.data.neo4j.core.GraphDatabase;
+import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
-import de.flitzr.spring.neo4j.example.dao.ArtifactRepository;
-import de.flitzr.spring.neo4j.example.model.Artifact;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
/**
* @author malbers
@@ -22,30 +28,63 @@
@Component
public class SpringExample implements Runnable {
+ @Autowired
+ private Neo4jTemplate template;
+
@Autowired
private ArtifactRepository artifactDao;
-
- @Transactional
- public void run() {
- Artifact springData = new Artifact("org.springframework.data", "spring-data", "2.0.1.RELEASE");
- artifactDao.save(springData);
-
- Artifact springBeans = new Artifact("org.springframework", "spring-beans", "3.0.7.RELEASE");
- artifactDao.save(springBeans);
-
- Artifact springTx= new Artifact("org.springframework", "spring-tx", "3.0.7.RELEASE");
- artifactDao.save(springTx);
-
- Set<Artifact> dependencies = new HashSet<Artifact>();
- springData.setDependencies(dependencies);
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- context.getBean(SpringExample.class).run();
+ @Transactional
+ public void run() {
+ cleanUp();
+ setupDb();
+ find();
}
+ @Transactional
+ void setupDb() {
+ Artifact springData = new Artifact("org.springframework.data", "spring-data", "2.0.1.RELEASE");
+ Artifact springBeans = new Artifact("org.springframework", "spring-beans", "3.0.7.RELEASE");
+ Artifact springTx= new Artifact("org.springframework", "spring-tx", "3.0.7.RELEASE");
+
+ Set<Artifact> dependencies = new HashSet<Artifact>();
+ dependencies.add(springBeans);
+ dependencies.add(springTx);
+ springData.setDependencies(dependencies);
+ artifactDao.save(springData);
+ }
+
+ @Transactional
+ void cleanUp() {
+ // cleanup
+ for(Artifact toDelete : artifactDao.findAll()){
+ artifactDao.delete(toDelete);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ void find(){
+ Artifact artifact = artifactDao.findByPropertyValue("artifactId", "spring-data");
+ print(artifact, null);
+ }
+
+
+ @Transactional(readOnly = true)
+ void print(Artifact artifact, String prefix){
+ if(prefix == null){
+ prefix = "";
+ }
+ System.out.println(prefix + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion());
+ if(artifact.getDependencies() != null){
+ for(Artifact dependency : artifact.getDependencies()){
+ print(dependency, prefix + "|-");
+ }
+ }
+
+ }
+
+ public GraphDatabaseService getDatabaseService(){
+ return template.getGraphDatabaseService();
+ }
+
}
View
80 spring-neo4j-example/src/main/java/de/flitzr/spring/neo4j/example/SpringRunner.java
@@ -0,0 +1,80 @@
+/**
+ *
+ */
+package de.flitzr.spring.neo4j.example;
+
+import de.flitzr.spring.neo4j.example.dao.ArtifactRepository;
+import de.flitzr.spring.neo4j.example.model.Artifact;
+import org.neo4j.cypher.ExecutionEngine;
+import org.neo4j.cypher.ExecutionResult;
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.data.neo4j.conversion.Result;
+import org.springframework.data.neo4j.core.GraphDatabase;
+import org.springframework.data.neo4j.support.Neo4jTemplate;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
+import scala.Console;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author malbers
+ *
+ */
+public class SpringRunner{
+
+
+ private static GraphDatabaseService database;
+
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
+ SpringExample example = context.getBean(SpringExample.class);
+ database = example.getDatabaseService();
+ example.run();
+
+ // wait for input
+ String query;
+ while(true){
+ query = Console.readLine();
+ if("exit".equals(query)){
+ break;
+ } else {
+ executeQuery(query);
+ }
+ }
+ }
+
+
+ /**
+ * Execute some query string
+ *
+ * @param queryString
+ */
+ private static void executeQuery(String queryString) {
+
+ if(queryString == null || queryString.isEmpty()){
+ return;
+ }
+
+ ExecutionEngine engine = new ExecutionEngine(database);
+
+ // for detailed output see: http://docs.neo4j.org/chunked/stable/tutorials-cypher-java.html
+ try{
+ ExecutionResult result = engine.execute(queryString);
+ Console.out().println(result.dumpToString());
+ } catch (Exception exp) {
+ Console.err().println(exp.getMessage());
+ }
+
+ }
+
+}
View
7 ...ng-neo4j-example/src/main/java/de/flitzr/spring/neo4j/example/dao/ArtifactRepository.java
@@ -3,14 +3,17 @@
*/
package de.flitzr.spring.neo4j.example.dao;
-import org.springframework.data.neo4j.repository.GraphRepository;
-
import de.flitzr.spring.neo4j.example.model.Artifact;
+import org.springframework.data.neo4j.annotation.Query;
+import org.springframework.data.neo4j.repository.GraphRepository;
/**
* @author malbers
*
*/
public interface ArtifactRepository extends GraphRepository<Artifact> {
+ @Query("START dependency=node({0}) MATCH artifact-[:dependsOn]-> dependency RETURN artifact")
+ Iterable<Artifact> getDependentBy(Artifact artifact);
+
}
View
34 spring-neo4j-example/src/main/java/de/flitzr/spring/neo4j/example/model/Artifact.java
@@ -1,33 +1,47 @@
package de.flitzr.spring.neo4j.example.model;
-import java.util.Set;
-
import org.neo4j.graphdb.Direction;
import org.springframework.data.annotation.PersistenceConstructor;
-import org.springframework.data.neo4j.annotation.NodeEntity;
-import org.springframework.data.neo4j.annotation.RelatedTo;
+import org.springframework.data.neo4j.annotation.*;
+
+import java.util.Set;
@NodeEntity
public class Artifact {
+ @GraphId
+ private Long id;
+
+ @Indexed
private String artifactId;
-
+
private String groupId;
-
+
private String version;
- @RelatedTo(direction = Direction.OUTGOING)
+ @RelatedTo(direction = Direction.OUTGOING, type = "dependsOn", elementClass = Artifact.class)
private Set<Artifact> dependencies;
-
- @PersistenceConstructor
+
+ public Artifact(){
+ super();
+ }
+
public Artifact(String groupId, String artifactId, String version) {
super();
this.artifactId = artifactId;
this.groupId = groupId;
this.version = version;
}
- public String getArtifactId() {
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getArtifactId() {
return artifactId;
}
View
26 spring-neo4j-example/src/main/resources/applicationContext.xml
@@ -13,7 +13,27 @@
<context:annotation-config />
<neo4j:repositories base-package="de.flitzr.spring.neo4j.example.dao" />
- <neo4j:config storeDirectory="target/neo4j.db"/>
-
- <tx:annotation-driven/>
+ <neo4j:config storeDirectory="target/neo4j.db"/>
+
+ <!-- configure @Transactional support for neo4j 1.6 -->
+ <!--
+ <tx:annotation-driven mode="aspectj"/>
+ -->
+
+ <!-- configure @Transactional support for neo4j 1.7 -->
+ <bean name="springTransactionManager" class="org.neo4j.kernel.impl.transaction.SpringTransactionManager">
+ <constructor-arg ref="graphDatabaseService"/>
+ </bean>
+
+ <bean name="userTransaction" class="org.neo4j.kernel.impl.transaction.UserTransactionImpl">
+ <constructor-arg ref="springTransactionManager"/>
+ </bean>
+
+ <bean id="neo4jTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
+ <property name="transactionManager" ref="springTransactionManager" />
+ <property name="userTransaction" ref="userTransaction" />
+ </bean>
+
+ <tx:annotation-driven mode="aspectj" transaction-manager="neo4jTransactionManager"/>
+
</beans>

0 comments on commit c6ca657

Please sign in to comment.
Something went wrong with that request. Please try again.