Change example entities. Added some Neo4J entities, added a data loader for JPA.
This commit is contained in:
@@ -20,8 +20,8 @@ public class RestExporterWebInitializer implements WebApplicationInitializer {
|
||||
@Override public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
AnnotationConfigWebApplicationContext rootCtx = new AnnotationConfigWebApplicationContext();
|
||||
rootCtx.register(
|
||||
JpaRepositoryConfig.class,
|
||||
MongoDbRepositoryConfig.class,
|
||||
//JpaRepositoryConfig.class,
|
||||
//MongoDbRepositoryConfig.class,
|
||||
//GemfireRepositoryConfig.class,
|
||||
Neo4jRepositoryConfig.class
|
||||
);
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.springframework.data.rest.example.jpa;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin
|
||||
*/
|
||||
public class JpaLoader {
|
||||
|
||||
private final static Logger LOG = LoggerFactory.getLogger(JpaLoader.class);
|
||||
@Autowired
|
||||
PersonRepository people;
|
||||
|
||||
@Transactional
|
||||
public void loadData() {
|
||||
Person billyBob = people.save(new Person("Billy Bob", "Thornton"));
|
||||
LOG.info("Saved {}", billyBob);
|
||||
Person john = people.save(new Person("John", "Doe"));
|
||||
LOG.info("Saved {}", john);
|
||||
Person jane = people.save(new Person("Jane", "Doe"));
|
||||
LOG.info("Saved {}", jane);
|
||||
john.setSiblings(Arrays.asList(jane));
|
||||
john.setFather(billyBob);
|
||||
jane.setSiblings(Arrays.asList(john));
|
||||
jane.setFather(billyBob);
|
||||
|
||||
people.save(Arrays.asList(john, jane));
|
||||
|
||||
LOG.info("Person count: {}", people.count());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -57,4 +57,8 @@ public class JpaRepositoryConfig {
|
||||
return txManager;
|
||||
}
|
||||
|
||||
@Bean(initMethod = "loadData") public JpaLoader jpaLoader() {
|
||||
return new JpaLoader();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -109,4 +109,15 @@ public class Person {
|
||||
this.created = Calendar.getInstance().getTime();
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return "Person{" +
|
||||
"id=" + id +
|
||||
", firstName='" + firstName + '\'' +
|
||||
", lastName='" + lastName + '\'' +
|
||||
", siblings=[" + siblings.size() + "]" +
|
||||
", father=" + father +
|
||||
", created=" + created +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package org.springframework.data.rest.example.jpa;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin
|
||||
*/
|
||||
@Component
|
||||
public class PersonLoader implements InitializingBean {
|
||||
|
||||
@Autowired
|
||||
PersonRepository people;
|
||||
|
||||
@Override public void afterPropertiesSet() throws Exception {
|
||||
Person billyBob = people.save(new Person("Billy Bob", "Thornton"));
|
||||
|
||||
Person john = new Person("John", "Doe");
|
||||
Person jane = new Person("Jane", "Doe");
|
||||
john.addSibling(jane);
|
||||
john.setFather(billyBob);
|
||||
jane.addSibling(john);
|
||||
jane.setFather(billyBob);
|
||||
|
||||
people.save(Arrays.asList(john, jane));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.springframework.data.rest.example.neo4j;
|
||||
|
||||
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 Movie {
|
||||
|
||||
@GraphId
|
||||
private Long id;
|
||||
private String name;
|
||||
@RelatedTo(enforceTargetType = true)
|
||||
private Studio studio;
|
||||
|
||||
public Movie() {
|
||||
}
|
||||
|
||||
public Movie(String name, Studio studio) {
|
||||
this.name = name;
|
||||
this.studio = studio;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Studio getStudio() {
|
||||
return studio;
|
||||
}
|
||||
|
||||
public void setStudio(Studio studio) {
|
||||
this.studio = studio;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.springframework.data.rest.example.neo4j;
|
||||
|
||||
import org.springframework.data.neo4j.repository.GraphRepository;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin
|
||||
*/
|
||||
public interface MovieRepository extends GraphRepository<Movie> {
|
||||
}
|
||||
@@ -3,20 +3,27 @@ package org.springframework.data.rest.example.neo4j;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin
|
||||
*/
|
||||
public class FriendLoader {
|
||||
public class Neo4jLoader {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Neo4jLoader.class);
|
||||
@Autowired
|
||||
private FriendRepository friends;
|
||||
@Autowired
|
||||
private MovieRepository movies;
|
||||
@Autowired
|
||||
private StudioRepository studios;
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
@Transactional
|
||||
public void loadFriends() {
|
||||
public void loadData() {
|
||||
Friend bob = new Friend("Bob");
|
||||
Friend dean = new Friend("Dean");
|
||||
Friend jim = new Friend("Jim");
|
||||
@@ -29,7 +36,12 @@ public class FriendLoader {
|
||||
|
||||
this.friends.save(bob);
|
||||
|
||||
System.out.println("Saved Bob: " + bob);
|
||||
LOG.info("Loaded {}", bob);
|
||||
|
||||
Studio studio = studios.save(new Studio("MGM"));
|
||||
Movie wizardOfOz = movies.save(new Movie("The Wizard of Oz", studio));
|
||||
|
||||
LOG.info("Loaded {} and {}", studio, wizardOfOz);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,10 +9,10 @@ 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.Neo4jExceptionTranslator;
|
||||
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
|
||||
@@ -41,8 +41,12 @@ public class Neo4jRepositoryConfig {
|
||||
return new JtaTransactionManagerFactoryBean(graphDatabaseService());
|
||||
}
|
||||
|
||||
@Bean(initMethod = "loadFriends") public FriendLoader friendLoader() {
|
||||
return new FriendLoader();
|
||||
@Bean public Neo4jExceptionTranslator exceptionTranslator() {
|
||||
return new Neo4jExceptionTranslator();
|
||||
}
|
||||
|
||||
@Bean(initMethod = "loadData") public Neo4jLoader neo4jLoader() {
|
||||
return new Neo4jLoader();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.springframework.data.rest.example.neo4j;
|
||||
|
||||
import org.springframework.data.neo4j.annotation.GraphId;
|
||||
import org.springframework.data.neo4j.annotation.NodeEntity;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin
|
||||
*/
|
||||
@NodeEntity
|
||||
public class Studio {
|
||||
|
||||
@GraphId
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public Studio() {
|
||||
}
|
||||
|
||||
public Studio(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.springframework.data.rest.example.neo4j;
|
||||
|
||||
import org.springframework.data.neo4j.repository.GraphRepository;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin
|
||||
*/
|
||||
public interface StudioRepository extends GraphRepository<Studio> {
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
<?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>
|
||||
Reference in New Issue
Block a user