diff --git a/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/RestExporterExampleRestConfig.java b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/RestExporterExampleRestConfig.java index 2042b100e..f24cc3723 100644 --- a/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/RestExporterExampleRestConfig.java +++ b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/RestExporterExampleRestConfig.java @@ -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> personResourceProcessor() { diff --git a/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/RestExporterWebInitializer.java b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/RestExporterWebInitializer.java index e4913a3a6..72bcf5e72 100644 --- a/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/RestExporterWebInitializer.java +++ b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/RestExporterWebInitializer.java @@ -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)); diff --git a/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/jpa/Person.java b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/jpa/Person.java index cdb6bc64e..c8b72bdd3 100644 --- a/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/jpa/Person.java +++ b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/jpa/Person.java @@ -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 siblings = Collections.emptyList(); private Person father; @Description("Timestamp this person object was created") diff --git a/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/neo4j/Friend.java b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/neo4j/Friend.java new file mode 100644 index 000000000..402168dff --- /dev/null +++ b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/neo4j/Friend.java @@ -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 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 getFriends() { + return friends; + } + + public void setFriends(Set friends) { + this.friends = friends; + } + + @Override public String toString() { + return "Friend{" + + "id=" + id + + ", name='" + name + '\'' + + ", friends=[" + (null != friends ? friends.size() : 0) + "]" + + '}'; + } + +} diff --git a/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/neo4j/FriendLoader.java b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/neo4j/FriendLoader.java new file mode 100644 index 000000000..7a14eccd4 --- /dev/null +++ b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/neo4j/FriendLoader.java @@ -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 friends = new HashSet(); + friends.add(dean); + friends.add(jim); + + bob.setFriends(friends); + + this.friends.save(bob); + + System.out.println("Saved Bob: " + bob); + } + +} diff --git a/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/neo4j/FriendRepository.java b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/neo4j/FriendRepository.java new file mode 100644 index 000000000..0a3edbc1a --- /dev/null +++ b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/neo4j/FriendRepository.java @@ -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 { +} diff --git a/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/neo4j/Neo4jRepositoryConfig.java b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/neo4j/Neo4jRepositoryConfig.java new file mode 100644 index 000000000..e43c95598 --- /dev/null +++ b/spring-data-rest-example/src/main/java/org/springframework/data/rest/example/neo4j/Neo4jRepositoryConfig.java @@ -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(); + } + +} diff --git a/spring-data-rest-example/src/main/resources/META-INF/spring/neo4j-config.xml b/spring-data-rest-example/src/main/resources/META-INF/spring/neo4j-config.xml new file mode 100644 index 000000000..a2bb4bc2b --- /dev/null +++ b/spring-data-rest-example/src/main/resources/META-INF/spring/neo4j-config.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file