Added neo4j to the example

This commit is contained in:
Jon Brisbin
2013-03-13 11:13:55 -05:00
committed by Jon Brisbin
parent e0c34e1be6
commit 2daec49f6a
8 changed files with 169 additions and 4 deletions

View File

@@ -3,7 +3,6 @@ package org.springframework.data.rest.example;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.data.rest.config.RepositoryRestConfiguration;
import org.springframework.data.rest.example.jpa.Person;
@@ -38,7 +37,8 @@ public class RestExporterExampleRestConfig extends RepositoryRestMvcConfiguratio
config.addResourceMappingForDomainType(Person.class)
.addResourceMappingFor("siblings")
.setRel("siblings")
.setPath("siblings");
.setPath("siblings")
.setExported(false);
}
@Bean public ResourceProcessor<Resource<Person>> personResourceProcessor() {

View File

@@ -4,9 +4,9 @@ import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.data.rest.example.gemfire.GemfireRepositoryConfig;
import org.springframework.data.rest.example.jpa.JpaRepositoryConfig;
import org.springframework.data.rest.example.mongodb.MongoDbRepositoryConfig;
import org.springframework.data.rest.example.neo4j.Neo4jRepositoryConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
@@ -22,7 +22,8 @@ public class RestExporterWebInitializer implements WebApplicationInitializer {
rootCtx.register(
JpaRepositoryConfig.class,
MongoDbRepositoryConfig.class,
GemfireRepositoryConfig.class
//GemfireRepositoryConfig.class,
Neo4jRepositoryConfig.class
);
servletContext.addListener(new ContextLoaderListener(rootCtx));

View File

@@ -16,6 +16,7 @@ import javax.persistence.PrePersist;
import javax.validation.constraints.NotNull;
import org.springframework.data.rest.repository.annotation.Description;
import org.springframework.data.rest.repository.annotation.RestResource;
/**
* An entity that represents a person.
@@ -32,6 +33,7 @@ public class Person {
@NotNull
private String lastName;
@Description("A person's siblings")
@RestResource(exported = false)
private List<Person> siblings = Collections.emptyList();
private Person father;
@Description("Timestamp this person object was created")

View File

@@ -0,0 +1,57 @@
package org.springframework.data.rest.example.neo4j;
import java.util.Set;
import org.neo4j.graphdb.Direction;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.annotation.RelatedTo;
/**
* @author Jon Brisbin
*/
@NodeEntity
public class Friend {
@GraphId
private Long id;
private String name;
@RelatedTo(type = "BFF", direction = Direction.INCOMING)
private Set<Friend> friends;
protected Friend() {
}
public Friend(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Friend> getFriends() {
return friends;
}
public void setFriends(Set<Friend> friends) {
this.friends = friends;
}
@Override public String toString() {
return "Friend{" +
"id=" + id +
", name='" + name + '\'' +
", friends=[" + (null != friends ? friends.size() : 0) + "]" +
'}';
}
}

View File

@@ -0,0 +1,35 @@
package org.springframework.data.rest.example.neo4j;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Jon Brisbin
*/
public class FriendLoader {
@Autowired
private FriendRepository friends;
@SuppressWarnings({"unchecked"})
@Transactional
public void loadFriends() {
Friend bob = new Friend("Bob");
Friend dean = new Friend("Dean");
Friend jim = new Friend("Jim");
Set<Friend> friends = new HashSet<Friend>();
friends.add(dean);
friends.add(jim);
bob.setFriends(friends);
this.friends.save(bob);
System.out.println("Saved Bob: " + bob);
}
}

View File

@@ -0,0 +1,9 @@
package org.springframework.data.rest.example.neo4j;
import org.springframework.data.neo4j.repository.GraphRepository;
/**
* @author Jon Brisbin
*/
public interface FriendRepository extends GraphRepository<Friend> {
}

View File

@@ -0,0 +1,48 @@
package org.springframework.data.rest.example.neo4j;
import java.io.File;
import java.io.IOException;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.neo4j.kernel.impl.util.FileUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.config.JtaTransactionManagerFactoryBean;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.data.neo4j.support.mapping.Neo4jMappingContext;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.TransactionTemplate;
/**
* @author Jon Brisbin
*/
@Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories(basePackages = "org.springframework.data.rest.example.neo4j")
public class Neo4jRepositoryConfig {
static final String DB_PATH = "db/neo4j-test-db";
@Bean(destroyMethod = "shutdown") public EmbeddedGraphDatabase graphDatabaseService() throws IOException {
FileUtils.deleteRecursively(new File(DB_PATH));
return new EmbeddedGraphDatabase(DB_PATH);
}
@Bean public Neo4jTemplate neo4jTemplate() throws IOException {
return new Neo4jTemplate(graphDatabaseService());
}
@Bean public Neo4jMappingContext neo4jMappingContext() {
return new Neo4jMappingContext();
}
@Bean public JtaTransactionManagerFactoryBean transactionManager() throws Exception {
return new JtaTransactionManagerFactoryBean(graphDatabaseService());
}
@Bean(initMethod = "loadFriends") public FriendLoader friendLoader() {
return new FriendLoader();
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">
<neo4j:config id="neo4j" storeDirectory="build/graph.db"/>
<neo4j:repositories base-package="org.springframework.data.rest.example.neo4j"/>
<bean class="org.springframework.data.rest.example.neo4j.FriendLoader" init-method="loadFriends"/>
</beans>