├── mongodriver ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── de │ │ └── codecentric │ │ ├── Connection.java │ │ ├── notifications │ │ ├── DocumentProducer.java │ │ └── EventListener.java │ │ ├── transaction │ │ └── SimpleTransaction.java │ │ └── driverclient │ │ ├── OrderExample.java │ │ ├── ReplicaSetTimeout.java │ │ └── SimpleMongoClient.java └── pom.xml ├── .gitignore ├── springdata-jpa ├── .gitignore ├── src │ ├── main │ │ ├── resources │ │ │ └── META-INF │ │ │ │ ├── persistence.xml │ │ │ │ └── orm.xml │ │ └── java │ │ │ └── jpa │ │ │ ├── repo │ │ │ ├── ClassicUserRepository.java │ │ │ └── UserRepository.java │ │ │ └── domain │ │ │ └── User.java │ └── test │ │ ├── resources │ │ └── jpa │ │ │ └── repo │ │ │ └── JpaRepoTest-context.xml │ │ └── java │ │ └── jpa │ │ └── repo │ │ └── JpaRepoTest.java ├── README.md └── pom.xml ├── springdata-mongodb ├── src │ ├── test │ │ ├── resources │ │ │ └── dummy.txt │ │ └── java │ │ │ └── mongodb │ │ │ ├── config │ │ │ ├── LocalhostMongoGridFSConfiguration.java │ │ │ └── LocalhostMongoConfiguration.java │ │ │ ├── user │ │ │ ├── IndexViolationTest.java │ │ │ └── UserRepositoryTest.java │ │ │ ├── order │ │ │ ├── OrderSubListTest.java │ │ │ └── OrderRepositoryTest.java │ │ │ └── geo │ │ │ └── LocationRepositoryTest.java │ ├── main │ │ ├── java │ │ │ └── mongodb │ │ │ │ ├── Adresse.java │ │ │ │ ├── user │ │ │ │ ├── UserRepository.java │ │ │ │ └── User.java │ │ │ │ ├── order │ │ │ │ ├── OrderRepository.java │ │ │ │ ├── Item.java │ │ │ │ ├── OrderBeforeSaveListener.java │ │ │ │ └── Order.java │ │ │ │ ├── geo │ │ │ │ ├── LocationRepository.java │ │ │ │ └── Location.java │ │ │ │ └── PointOfInterest.java │ │ ├── doc │ │ │ └── points.pptx │ │ └── bat │ │ │ ├── masterslave.bat │ │ │ └── shard.bat │ └── misc │ │ ├── resources │ │ ├── MongoDBMassTest-context.xml │ │ └── CappedCollectionManipulationTest-context.xml │ │ └── java │ │ └── mongodb │ │ ├── CappedCollectionManipulationTest.java │ │ └── MongoDBMassTest.java ├── .gitignore ├── single-node.yml ├── README.md ├── rs-dev0.yml └── pom.xml ├── springdata-neo4j ├── .gitignore ├── src │ ├── main │ │ ├── doc │ │ │ ├── example1.pptx │ │ │ ├── neo4j_rest_get.png │ │ │ └── neo4j_databrowser.png │ │ └── java │ │ │ └── neo4j │ │ │ ├── repo │ │ │ └── UserRepository.java │ │ │ └── domain │ │ │ └── User.java │ └── test │ │ └── java │ │ └── neo4j │ │ └── repo │ │ ├── Neo4jTestConfig.java │ │ └── Neo4jRepoTest.java ├── README.md └── pom.xml ├── springdata-redis ├── .gitignore ├── src │ ├── main │ │ └── java │ │ │ └── redis │ │ │ ├── UserRepository.java │ │ │ ├── StringStringRepository.java │ │ │ └── User.java │ └── test │ │ ├── java │ │ └── redis │ │ │ ├── StringStringRepositoryTest.java │ │ │ └── UserRepositoryTest.java │ │ └── resources │ │ └── redis │ │ ├── StringStringRepositoryTest-context.xml │ │ └── UserRepositoryTest-context.xml ├── README.md └── pom.xml ├── spring-boot-mongo ├── src │ ├── main │ │ ├── resources │ │ │ └── application.properties │ │ ├── docker │ │ │ └── Dockerfile │ │ └── java │ │ │ └── de │ │ │ └── codecentric │ │ │ └── mongoboot │ │ │ ├── SpringBootMongoApplication.java │ │ │ ├── OrderRepository.java │ │ │ ├── Item.java │ │ │ └── Order.java │ └── test │ │ └── java │ │ └── de │ │ └── codecentric │ │ └── mongoboot │ │ └── SpringBootMongoApplicationTests.java ├── .gitignore ├── docker-compose.yml ├── pom.xml ├── mvnw.cmd └── mvnw ├── .travis.yml ├── README.md └── pom.xml /mongodriver/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /target/ 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /.settings 3 | .classpath 4 | .project -------------------------------------------------------------------------------- /springdata-jpa/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | 3 | /.settings 4 | 5 | .classpath 6 | .project -------------------------------------------------------------------------------- /springdata-mongodb/src/test/resources/dummy.txt: -------------------------------------------------------------------------------- 1 | A file to be uploaded via GridFS .... -------------------------------------------------------------------------------- /springdata-neo4j/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | 3 | /.settings 4 | 5 | .classpath 6 | .project -------------------------------------------------------------------------------- /springdata-redis/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | 3 | /.settings 4 | 5 | .classpath 6 | .project -------------------------------------------------------------------------------- /springdata-mongodb/src/main/java/mongodb/Adresse.java: -------------------------------------------------------------------------------- 1 | package mongodb; 2 | 3 | public class Adresse { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /spring-boot-mongo/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.data.rest.base-path=/rest 2 | management.security.enabled=false -------------------------------------------------------------------------------- /springdata-mongodb/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /.settings 3 | .classpath 4 | .project 5 | /maven-eclipse.xml 6 | /.externalToolBuilders/ 7 | -------------------------------------------------------------------------------- /springdata-mongodb/src/main/doc/points.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ttrelle/spring-data-examples/HEAD/springdata-mongodb/src/main/doc/points.pptx -------------------------------------------------------------------------------- /springdata-neo4j/src/main/doc/example1.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ttrelle/spring-data-examples/HEAD/springdata-neo4j/src/main/doc/example1.pptx -------------------------------------------------------------------------------- /springdata-neo4j/src/main/doc/neo4j_rest_get.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ttrelle/spring-data-examples/HEAD/springdata-neo4j/src/main/doc/neo4j_rest_get.png -------------------------------------------------------------------------------- /springdata-neo4j/src/main/doc/neo4j_databrowser.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ttrelle/spring-data-examples/HEAD/springdata-neo4j/src/main/doc/neo4j_databrowser.png -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - openjdk11 4 | services: 5 | - mongodb 6 | - redis 7 | addons: 8 | apt: 9 | sources: 10 | - mongodb-3.4-precise 11 | packages: 12 | - mongodb-org-server 13 | -------------------------------------------------------------------------------- /springdata-mongodb/src/main/bat/masterslave.bat: -------------------------------------------------------------------------------- 1 | start mongod --rest --port 8000 --dbpath /var/master --replSet cluster0 2 | start mongod --rest --port 8001 --dbpath /var/slave1 --replSet cluster0 3 | start mongod --rest --port 8002 --dbpath /var/slave2 --replSet cluster0 -------------------------------------------------------------------------------- /springdata-mongodb/single-node.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | 3 | services: 4 | mongodb: 5 | image: mongo:4.0.9 6 | container_name: "mongodb" 7 | volumes: 8 | - ./datasets:/datasets 9 | - ./uebungen:/uebungen 10 | ports: 11 | - 27017:27017 12 | -------------------------------------------------------------------------------- /spring-boot-mongo/src/main/docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM java:8 2 | VOLUME /tmp 3 | ADD spring-boot-mongo*.jar mongorest.jar 4 | RUN sh -c 'touch /mongorest.jar' 5 | ENV JAVA_OPTS="-Xmx256m -Xms128m" 6 | ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /mongorest.jar" ] -------------------------------------------------------------------------------- /springdata-mongodb/src/main/bat/shard.bat: -------------------------------------------------------------------------------- 1 | start mongod --shardsvr --rest --port 9000 --dbpath /var/shard1 2 | start mongod --shardsvr --rest --port 9001 --dbpath /var/shard2 3 | start mongod --configsvr --rest --port 9002 --dbpath /var/conf1 4 | pause 5 | start mongos --port 9003 --configdb tmp-pc:9002 --chunkSize 1 6 | 7 | -------------------------------------------------------------------------------- /spring-boot-mongo/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ 25 | /.mvn/ 26 | -------------------------------------------------------------------------------- /mongodriver/src/main/java/de/codecentric/Connection.java: -------------------------------------------------------------------------------- 1 | package de.codecentric; 2 | 3 | import com.mongodb.MongoClientURI; 4 | 5 | abstract public class Connection { 6 | 7 | public static final MongoClientURI URI = new MongoClientURI( 8 | "mongodb://mongo1:27001,mongo2:27002,mongo3:27003/test?replicatSet=dev0" 9 | // "mongodb://localhost:27001,localhost:27002,localhost:27003/test?replicatSet=dev0" 10 | ); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /springdata-jpa/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | jpa.domain.User 5 | 6 | -------------------------------------------------------------------------------- /spring-boot-mongo/src/main/java/de/codecentric/mongoboot/SpringBootMongoApplication.java: -------------------------------------------------------------------------------- 1 | package de.codecentric.mongoboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootMongoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootMongoApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-boot-mongo/src/test/java/de/codecentric/mongoboot/SpringBootMongoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package de.codecentric.mongoboot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringBootMongoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-mongo/src/main/java/de/codecentric/mongoboot/OrderRepository.java: -------------------------------------------------------------------------------- 1 | package de.codecentric.mongoboot; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.mongodb.repository.MongoRepository; 6 | import org.springframework.data.repository.query.Param; 7 | import org.springframework.data.rest.core.annotation.RepositoryRestResource; 8 | 9 | @RepositoryRestResource() 10 | public interface OrderRepository extends MongoRepository { 11 | 12 | List findByText(@Param("name") String name); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /springdata-mongodb/src/main/java/mongodb/user/UserRepository.java: -------------------------------------------------------------------------------- 1 | package mongodb.user; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.domain.Sort; 6 | import org.springframework.data.mongodb.repository.MongoRepository; 7 | import org.springframework.data.mongodb.repository.Query; 8 | 9 | public interface UserRepository extends MongoRepository { 10 | 11 | @Query("{ fullName: ?0 }") 12 | List findByTheUsersFullName(String fullName); 13 | 14 | List findByFullNameLike(String fullName, Sort sort); 15 | } 16 | -------------------------------------------------------------------------------- /springdata-neo4j/src/main/java/neo4j/repo/UserRepository.java: -------------------------------------------------------------------------------- 1 | package neo4j.repo; 2 | 3 | import java.util.List; 4 | 5 | import neo4j.domain.User; 6 | 7 | import org.springframework.data.neo4j.annotation.Query; 8 | import org.springframework.data.neo4j.repository.GraphRepository; 9 | 10 | /** Neo4j repository. */ 11 | public interface UserRepository extends GraphRepository { 12 | 13 | User findByLogin(String login); 14 | 15 | @Query("START root=node:User(login = 'root') MATCH root-[:knows]->friends RETURN friends") 16 | List findFriendsOfRoot(); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /springdata-jpa/src/main/resources/META-INF/orm.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | SELECT u FROM User u WHERE u.fullName = 'User 2' 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /spring-boot-mongo/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | networks: 4 | servicenet: 5 | driver: bridge 6 | 7 | services: 8 | 9 | boot-rest-mongo: 10 | image: mongoboot/spring-boot-mongo 11 | ports: 12 | - "8080:8080" 13 | networks: 14 | - servicenet 15 | depends_on: 16 | - mongo 17 | environment: 18 | SPRING_DATA_MONGODB_URI: mongodb://mongo:27017/test 19 | 20 | mongo: 21 | image: mongo:3.4.10 22 | ports: 23 | - "27017:27017" 24 | networks: 25 | - servicenet 26 | volumes: 27 | - /data/db:/data/db 28 | -------------------------------------------------------------------------------- /springdata-mongodb/src/main/java/mongodb/order/OrderRepository.java: -------------------------------------------------------------------------------- 1 | package mongodb.order; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.mongodb.repository.MongoRepository; 6 | import org.springframework.data.mongodb.repository.Query; 7 | 8 | public interface OrderRepository extends MongoRepository { 9 | 10 | List findByItemsQuantity(int quantity); 11 | 12 | @Query("{ \"items.quantity\": ?0 }") 13 | List findWithQuery(int quantity); 14 | 15 | @Query( value = "{ custInfo: ?0 }", fields = "{_id:0, items:1}") 16 | List findOnlyItems(String name); 17 | } 18 | -------------------------------------------------------------------------------- /springdata-redis/src/main/java/redis/UserRepository.java: -------------------------------------------------------------------------------- 1 | package redis; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.data.redis.core.RedisTemplate; 5 | 6 | /** 7 | * Repository for key/value pairs of type String/User. 8 | * @author tobias.trelle 9 | */ 10 | public class UserRepository { 11 | 12 | @Autowired 13 | private RedisTemplate template; 14 | 15 | public void add(User user) { 16 | template.opsForValue().set(user.getLogin(), user); 17 | } 18 | 19 | public User get(String key) { 20 | return template.opsForValue().get(key); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /springdata-mongodb/src/test/java/mongodb/config/LocalhostMongoGridFSConfiguration.java: -------------------------------------------------------------------------------- 1 | package mongodb.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.mongodb.gridfs.GridFsTemplate; 6 | 7 | /** 8 | * Test configuration for GridFS using localhost:27017 9 | * @author Tobias Trelle 10 | */ 11 | @Configuration 12 | public class LocalhostMongoGridFSConfiguration extends LocalhostMongoConfiguration { 13 | 14 | @Bean 15 | public GridFsTemplate gridTemplate() throws Exception { 16 | return new GridFsTemplate( mongoDbFactory(), mappingMongoConverter() ); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /springdata-jpa/src/main/java/jpa/repo/ClassicUserRepository.java: -------------------------------------------------------------------------------- 1 | package jpa.repo; 2 | 3 | import java.util.List; 4 | 5 | import javax.persistence.EntityManager; 6 | import javax.persistence.PersistenceContext; 7 | 8 | import jpa.domain.User; 9 | 10 | import org.springframework.stereotype.Repository; 11 | 12 | @Repository 13 | public class ClassicUserRepository { 14 | 15 | @PersistenceContext EntityManager em; 16 | 17 | public List findByFullName(String fullName) { 18 | return getEntityManger() 19 | .createNamedQuery("User.classicQuery", User.class) 20 | .setParameter("fullName", fullName) 21 | .getResultList(); 22 | } 23 | 24 | 25 | private EntityManager getEntityManger() { 26 | return em; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /springdata-mongodb/src/main/java/mongodb/geo/LocationRepository.java: -------------------------------------------------------------------------------- 1 | package mongodb.geo; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.geo.Box; 6 | import org.springframework.data.geo.Circle; 7 | import org.springframework.data.geo.Distance; 8 | import org.springframework.data.geo.Point; 9 | import org.springframework.data.mongodb.repository.MongoRepository; 10 | 11 | /** 12 | * Example of a repo using geospatial queries. 13 | * 14 | * @author Tobias Trelle 15 | */ 16 | public interface LocationRepository extends MongoRepository { 17 | 18 | List findByPositionWithin(Circle c); 19 | 20 | List findByPositionWithin(Box b); 21 | 22 | List findByPositionNear(Point p, Distance d); 23 | } 24 | -------------------------------------------------------------------------------- /springdata-redis/src/main/java/redis/StringStringRepository.java: -------------------------------------------------------------------------------- 1 | package redis; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.data.redis.core.RedisTemplate; 5 | 6 | /** 7 | * Simple repository to store key/value pairs of type String/String. 8 | * @author tobias.trelle 9 | */ 10 | public class StringStringRepository { 11 | 12 | @Autowired 13 | private RedisTemplate template; 14 | 15 | public void add(String key, String value) { 16 | template.opsForValue().set(key, value); 17 | } 18 | 19 | public String getValue(String key) { 20 | return template.opsForValue().get(key); 21 | } 22 | 23 | public void delete(String key) { 24 | template.opsForValue().getOperations().delete(key); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-mongo/src/main/java/de/codecentric/mongoboot/Item.java: -------------------------------------------------------------------------------- 1 | package de.codecentric.mongoboot; 2 | 3 | import org.springframework.data.mongodb.core.mapping.Field; 4 | 5 | public class Item { 6 | 7 | int quantity; 8 | 9 | double price; 10 | 11 | @Field("desc") 12 | String description; 13 | 14 | public int getQuantity() { 15 | return quantity; 16 | } 17 | 18 | public void setQuantity(int quantity) { 19 | this.quantity = quantity; 20 | } 21 | 22 | public double getPrice() { 23 | return price; 24 | } 25 | 26 | public void setPrice(double price) { 27 | this.price = price; 28 | } 29 | 30 | public String getDescription() { 31 | return description; 32 | } 33 | 34 | public void setDescription(String description) { 35 | this.description = description; 36 | } 37 | 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /mongodriver/src/main/java/de/codecentric/notifications/DocumentProducer.java: -------------------------------------------------------------------------------- 1 | package de.codecentric.notifications; 2 | 3 | import org.bson.Document; 4 | 5 | import com.mongodb.MongoClient; 6 | import com.mongodb.client.MongoCollection; 7 | 8 | import de.codecentric.Connection; 9 | 10 | public class DocumentProducer { 11 | 12 | public static void main(String[] args) throws Exception { 13 | 14 | try (MongoClient client = new MongoClient(Connection.URI)) { 15 | MongoCollection eventCollection = 16 | client.getDatabase("test").getCollection("events"); 17 | 18 | long i = 0; 19 | while (true) { 20 | Document doc = new Document(); 21 | doc.put("i", i++); 22 | doc.put("even", i % 2); 23 | eventCollection.insertOne(doc); 24 | //System.out.println("inserted: " + doc); 25 | Thread.sleep(2000L + (long)(1000*Math.random())); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /mongodriver/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | de.codecentric 8 | springdata-examples 9 | 1.0.0 10 | 11 | mongodriver 12 | 13 | 16 | 3.10.0 17 | 18 | 19 | 20 | 21 | org.mongodb 22 | mongo-java-driver 23 | ${mongo.driver.version} 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /springdata-neo4j/src/test/java/neo4j/repo/Neo4jTestConfig.java: -------------------------------------------------------------------------------- 1 | package neo4j.repo; 2 | 3 | import org.neo4j.ogm.session.SessionFactory; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.data.neo4j.config.Neo4jConfiguration; 7 | import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; 8 | import org.springframework.data.neo4j.server.InProcessServer; 9 | import org.springframework.data.neo4j.server.Neo4jServer; 10 | import org.springframework.data.neo4j.template.Neo4jTemplate; 11 | 12 | @Configuration 13 | @EnableNeo4jRepositories("neo4j.repo") 14 | public class Neo4jTestConfig extends Neo4jConfiguration { 15 | 16 | @Override 17 | public Neo4jServer neo4jServer() { 18 | return new InProcessServer(); 19 | } 20 | 21 | @Override 22 | public SessionFactory getSessionFactory() { 23 | return new SessionFactory("neo4j.domain"); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /springdata-redis/src/test/java/redis/StringStringRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package redis; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | @RunWith(SpringJUnit4ClassRunner.class) 14 | @ContextConfiguration 15 | public class StringStringRepositoryTest { 16 | 17 | @Autowired StringStringRepository repo; 18 | 19 | @Before 20 | public void setUp() { 21 | repo.add("foo", "bar"); 22 | } 23 | 24 | @Test 25 | public void shouldFindValue() { 26 | String value = repo.getValue("foo"); 27 | 28 | assertNotNull("Value is ", value); 29 | assertEquals( "Value mismatch" , "bar", value); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /springdata-redis/src/main/java/redis/User.java: -------------------------------------------------------------------------------- 1 | package redis; 2 | 3 | import java.util.Date; 4 | 5 | /** Simple user class. */ 6 | public class User { 7 | 8 | private String login; 9 | 10 | private String fullName; 11 | 12 | private Date lastLogin; 13 | 14 | public User() {} 15 | 16 | public User(String login, String fullName) { 17 | this.login = login; 18 | this.fullName = fullName; 19 | this.lastLogin = new Date(); 20 | } 21 | 22 | public String getLogin() { 23 | return login; 24 | } 25 | 26 | public void setLogin(String login) { 27 | this.login = login; 28 | } 29 | 30 | public String getFullName() { 31 | return fullName; 32 | } 33 | 34 | public void setFullName(String fullName) { 35 | this.fullName = fullName; 36 | } 37 | 38 | public Date getLastLogin() { 39 | return lastLogin; 40 | } 41 | 42 | public void setLastLogin(Date lastLogin) { 43 | this.lastLogin = lastLogin; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /springdata-redis/src/test/java/redis/UserRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package redis; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | @RunWith(SpringJUnit4ClassRunner.class) 14 | @ContextConfiguration 15 | public class UserRepositoryTest { 16 | 17 | @Autowired UserRepository repo; 18 | 19 | @Before 20 | public void setUp() { 21 | repo.add( new User("root", "Superuser") ); 22 | } 23 | 24 | @Test 25 | public void shouldFindValue() { 26 | User user = repo.get("root"); 27 | 28 | assertNotNull("Value is ", user); 29 | assertEquals( "login mismatch" , "root", user.getLogin()); 30 | assertEquals( "login mismatch" , "Superuser", user.getFullName()); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-boot-mongo/src/main/java/de/codecentric/mongoboot/Order.java: -------------------------------------------------------------------------------- 1 | package de.codecentric.mongoboot; 2 | 3 | import java.util.Date; 4 | import java.util.List; 5 | 6 | import org.springframework.data.annotation.Id; 7 | import org.springframework.data.mongodb.core.mapping.Field; 8 | 9 | public class Order { 10 | 11 | @Id String id; 12 | 13 | @Field("custInfo") 14 | String text; 15 | 16 | @Field("date") 17 | Date ordered; 18 | 19 | List items; 20 | 21 | public String getId() { 22 | return id; 23 | } 24 | 25 | public void setId(String id) { 26 | this.id = id; 27 | } 28 | 29 | public String getText() { 30 | return text; 31 | } 32 | 33 | public void setText(String text) { 34 | this.text = text; 35 | } 36 | 37 | public Date getOrdered() { 38 | return ordered; 39 | } 40 | 41 | public void setOrdered(Date ordered) { 42 | this.ordered = ordered; 43 | } 44 | 45 | public List getItems() { 46 | return items; 47 | } 48 | 49 | public void setItems(List items) { 50 | this.items = items; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /springdata-mongodb/src/main/java/mongodb/order/Item.java: -------------------------------------------------------------------------------- 1 | package mongodb.order; 2 | 3 | import org.springframework.data.mongodb.core.mapping.Field; 4 | 5 | public class Item { 6 | 7 | private int quantity; 8 | 9 | private double price; 10 | 11 | @Field("desc") private String description; 12 | 13 | public Item() { 14 | } 15 | 16 | public Item(int quantity, double price, String description) { 17 | super(); 18 | this.quantity = quantity; 19 | this.price = price; 20 | this.description = description; 21 | } 22 | 23 | public int getQuantity() { 24 | return quantity; 25 | } 26 | 27 | public void setQuantity(int quantity) { 28 | this.quantity = quantity; 29 | } 30 | 31 | public double getPrice() { 32 | return price; 33 | } 34 | 35 | public void setPrice(double price) { 36 | this.price = price; 37 | } 38 | 39 | public String getDescription() { 40 | return description; 41 | } 42 | 43 | public void setDescription(String description) { 44 | this.description = description; 45 | } 46 | 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /springdata-mongodb/src/main/java/mongodb/order/OrderBeforeSaveListener.java: -------------------------------------------------------------------------------- 1 | package mongodb.order; 2 | 3 | import java.util.Date; 4 | 5 | import org.bson.Document; 6 | import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener; 7 | import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent; 8 | 9 | import com.mongodb.DBObject; 10 | 11 | /** 12 | * Equivalent of a domain method annotated by PrePersist. 13 | *

14 | * This handler shows how to implement your custom UUID generation. 15 | * @author Tobias Trelle 16 | */ 17 | public class OrderBeforeSaveListener extends AbstractMongoEventListener { 18 | 19 | @Override 20 | public void onBeforeSave(BeforeSaveEvent event) { 21 | Order o = event.getSource(); 22 | Document d = event.getDocument(); 23 | 24 | if ( o.getId() == null ) { 25 | // TODO use a better UUID generator in production 26 | d.put("_id","" + Math.random() ); 27 | } 28 | 29 | if ( o.getDate() == null ) { 30 | d.put("date", new Date()); 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /springdata-redis/src/test/resources/redis/StringStringRepositoryTest-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /springdata-mongodb/src/test/java/mongodb/config/LocalhostMongoConfiguration.java: -------------------------------------------------------------------------------- 1 | package mongodb.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.mongodb.config.AbstractMongoConfiguration; 6 | import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; 7 | 8 | import com.mongodb.Mongo; 9 | import com.mongodb.MongoClient; 10 | 11 | import mongodb.order.OrderBeforeSaveListener; 12 | 13 | /** 14 | * Test configuration using localhost:27017 15 | * @author Tobias Trelle 16 | */ 17 | @Configuration 18 | @EnableMongoRepositories({"mongodb.geo", "mongodb.order", "mongodb.user"}) 19 | public class LocalhostMongoConfiguration extends AbstractMongoConfiguration { 20 | 21 | @Bean 22 | public OrderBeforeSaveListener beforeSaveListener() { 23 | return new OrderBeforeSaveListener(); 24 | } 25 | 26 | @Override 27 | protected String getDatabaseName() { 28 | return "odm_springdata"; 29 | } 30 | 31 | @Override 32 | public MongoClient mongoClient() { 33 | return new MongoClient(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /springdata-mongodb/src/misc/resources/MongoDBMassTest-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /springdata-mongodb/src/misc/resources/CappedCollectionManipulationTest-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /springdata-jpa/src/main/java/jpa/repo/UserRepository.java: -------------------------------------------------------------------------------- 1 | package jpa.repo; 2 | 3 | import java.util.List; 4 | 5 | import jpa.domain.User; 6 | 7 | import org.springframework.data.domain.Pageable; 8 | import org.springframework.data.domain.Sort; 9 | import org.springframework.data.jpa.repository.JpaRepository; 10 | import org.springframework.data.jpa.repository.Query; 11 | import org.springframework.data.repository.query.Param; 12 | import org.springframework.transaction.annotation.Propagation; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | public interface UserRepository extends JpaRepository { 16 | 17 | List findByFullName(String fullName); 18 | 19 | List findByFullName(String fullName, Sort sort); 20 | 21 | List findByFullName(String fullName, Pageable paging); 22 | 23 | List findByUser5(); 24 | 25 | List findByOrm(); 26 | 27 | @Transactional(timeout = 2, propagation = Propagation.REQUIRED) 28 | @Query("SELECT u FROM User u WHERE u.fullName = 'User 3'") 29 | List findByGivenQuery(); 30 | 31 | List findByIdAndFullName(@Param("id") String id, @Param("fullName") String fullname); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /springdata-jpa/README.md: -------------------------------------------------------------------------------- 1 | # Spring Data Blog Series 2 | 3 | This projects holds the Java source examples for my blog post series on the Spring Data project: 4 | 5 | * [GridFS Support in Spring Data MongoDB](http://blog.codecentric.de/en/2012/07/gridfs-support-in-spring-data-mongodb/) 6 | * [Part 6: Spring Data Redis](http://blog.codecentric.de/en/2012/04/spring-data-redis/) 7 | * [Part 5: Spring Data Neo4j](http://blog.codecentric.de/en/2012/02/spring-data-neo4j/) 8 | * [Part 4: Geospatial Queries with Spring Data MongoDB](http://blog.codecentric.de/en/2012/02/spring-data-mongodb-geospatial-queries/) 9 | * [Part 3: Spring Data MongoDB](http://blog.codecentric.de/en/2012/02/spring-data-mongodb/) 10 | * [Part 2: Spring Data JPA](http://blog.codecentric.de/en/2012/01/spring-data-jpa/) 11 | * [Part 1: Spring Data Commons](http://blog.codecentric.de/en/2011/12/spring-data-commons/) 12 | 13 | ## Usage 14 | 15 | The projects are using a Maven based build. To run the tests on the command line use 16 | 17 | mvn clean test 18 | 19 | If you want to use an IDE like Eclipse run 20 | 21 | mvn eclipse:eclipse 22 | 23 | and set the Eclipse variable M2_REPO pointing to your local Maven repository. Or just import the project as an existing Maven project if you are using the m2 plugin. 24 | 25 | -------------------------------------------------------------------------------- /mongodriver/src/main/java/de/codecentric/notifications/EventListener.java: -------------------------------------------------------------------------------- 1 | package de.codecentric.notifications; 2 | 3 | import static com.mongodb.client.model.Filters.and; 4 | import static com.mongodb.client.model.Filters.eq; 5 | import static com.mongodb.client.model.Filters.in; 6 | import static java.util.Arrays.asList; 7 | 8 | import org.bson.Document; 9 | 10 | import com.mongodb.MongoClient; 11 | import com.mongodb.client.ChangeStreamIterable; 12 | import com.mongodb.client.MongoCollection; 13 | import com.mongodb.client.model.Aggregates; 14 | 15 | import de.codecentric.Connection; 16 | 17 | public class EventListener { 18 | 19 | public static void main(String[] args) throws Exception { 20 | try (MongoClient client = new MongoClient(Connection.URI)) { 21 | MongoCollection eventCollection = 22 | client.getDatabase("test").getCollection("events"); 23 | 24 | ChangeStreamIterable changes = eventCollection.watch(asList( 25 | Aggregates.match( and( asList( 26 | in("operationType", asList("insert")), 27 | eq("fullDocument.even", 1L))) 28 | ))); 29 | 30 | changes.iterator().forEachRemaining( 31 | change -> System.out.println("received: " + change.getFullDocument()) 32 | ); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /springdata-mongodb/README.md: -------------------------------------------------------------------------------- 1 | # Spring Data Blog Series 2 | 3 | This projects holds the Java source examples for my blog post series on the Spring Data project: 4 | 5 | * [GridFS Support in Spring Data MongoDB](http://blog.codecentric.de/en/2012/07/gridfs-support-in-spring-data-mongodb/) 6 | * [Part 6: Spring Data Redis](http://blog.codecentric.de/en/2012/04/spring-data-redis/) 7 | * [Part 5: Spring Data Neo4j](http://blog.codecentric.de/en/2012/02/spring-data-neo4j/) 8 | * [Part 4: Geospatial Queries with Spring Data MongoDB](http://blog.codecentric.de/en/2012/02/spring-data-mongodb-geospatial-queries/) 9 | * [Part 3: Spring Data MongoDB](http://blog.codecentric.de/en/2012/02/spring-data-mongodb/) 10 | * [Part 2: Spring Data JPA](http://blog.codecentric.de/en/2012/01/spring-data-jpa/) 11 | * [Part 1: Spring Data Commons](http://blog.codecentric.de/en/2011/12/spring-data-commons/) 12 | 13 | ## Usage 14 | 15 | The projects are using a Maven based build. To run the tests on the command line use 16 | 17 | mvn clean test 18 | 19 | If you want to use an IDE like Eclipse run 20 | 21 | mvn eclipse:eclipse 22 | 23 | and set the Eclipse variable M2_REPO pointing to your local Maven repository. Or just import the project as an existing Maven project if you are using the m2 plugin. 24 | 25 | -------------------------------------------------------------------------------- /springdata-neo4j/README.md: -------------------------------------------------------------------------------- 1 | # Spring Data Blog Series 2 | 3 | This projects holds the Java source examples for my blog post series on the Spring Data project: 4 | 5 | * [GridFS Support in Spring Data MongoDB](http://blog.codecentric.de/en/2012/07/gridfs-support-in-spring-data-mongodb/) 6 | * [Part 6: Spring Data Redis](http://blog.codecentric.de/en/2012/04/spring-data-redis/) 7 | * [Part 5: Spring Data Neo4j](http://blog.codecentric.de/en/2012/02/spring-data-neo4j/) 8 | * [Part 4: Geospatial Queries with Spring Data MongoDB](http://blog.codecentric.de/en/2012/02/spring-data-mongodb-geospatial-queries/) 9 | * [Part 3: Spring Data MongoDB](http://blog.codecentric.de/en/2012/02/spring-data-mongodb/) 10 | * [Part 2: Spring Data JPA](http://blog.codecentric.de/en/2012/01/spring-data-jpa/) 11 | * [Part 1: Spring Data Commons](http://blog.codecentric.de/en/2011/12/spring-data-commons/) 12 | 13 | ## Usage 14 | 15 | The projects are using a Maven based build. To run the tests on the command line use 16 | 17 | mvn clean test 18 | 19 | If you want to use an IDE like Eclipse run 20 | 21 | mvn eclipse:eclipse 22 | 23 | and set the Eclipse variable M2_REPO pointing to your local Maven repository. Or just import the project as an existing Maven project if you are using the m2 plugin. 24 | 25 | -------------------------------------------------------------------------------- /springdata-redis/README.md: -------------------------------------------------------------------------------- 1 | # Spring Data Blog Series 2 | 3 | This projects holds the Java source examples for my blog post series on the Spring Data project: 4 | 5 | * [GridFS Support in Spring Data MongoDB](http://blog.codecentric.de/en/2012/07/gridfs-support-in-spring-data-mongodb/) 6 | * [Part 6: Spring Data Redis](http://blog.codecentric.de/en/2012/04/spring-data-redis/) 7 | * [Part 5: Spring Data Neo4j](http://blog.codecentric.de/en/2012/02/spring-data-neo4j/) 8 | * [Part 4: Geospatial Queries with Spring Data MongoDB](http://blog.codecentric.de/en/2012/02/spring-data-mongodb-geospatial-queries/) 9 | * [Part 3: Spring Data MongoDB](http://blog.codecentric.de/en/2012/02/spring-data-mongodb/) 10 | * [Part 2: Spring Data JPA](http://blog.codecentric.de/en/2012/01/spring-data-jpa/) 11 | * [Part 1: Spring Data Commons](http://blog.codecentric.de/en/2011/12/spring-data-commons/) 12 | 13 | ## Usage 14 | 15 | The projects are using a Maven based build. To run the tests on the command line use 16 | 17 | mvn clean test 18 | 19 | If you want to use an IDE like Eclipse run 20 | 21 | mvn eclipse:eclipse 22 | 23 | and set the Eclipse variable M2_REPO pointing to your local Maven repository. Or just import the project as an existing Maven project if you are using the m2 plugin. 24 | 25 | -------------------------------------------------------------------------------- /springdata-redis/src/test/resources/redis/UserRepositoryTest-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /springdata-mongodb/src/main/java/mongodb/PointOfInterest.java: -------------------------------------------------------------------------------- 1 | package mongodb; 2 | 3 | import org.springframework.data.geo.Point; 4 | import org.springframework.data.mongodb.core.index.Indexed; 5 | import org.springframework.data.mongodb.core.mapping.Document; 6 | import org.springframework.data.mongodb.core.mapping.Field; 7 | 8 | @Document(collection = "pois") 9 | public class PointOfInterest { 10 | 11 | @Indexed private String typ; 12 | private String tags; 13 | 14 | @Field("desc") private String description; 15 | private Adresse adresse; 16 | 17 | private Point location; 18 | 19 | public String getTyp() { 20 | return typ; 21 | } 22 | 23 | public void setTyp(String typ) { 24 | this.typ = typ; 25 | } 26 | 27 | public String getTags() { 28 | return tags; 29 | } 30 | 31 | public void setTags(String tags) { 32 | this.tags = tags; 33 | } 34 | 35 | public String getDescription() { 36 | return description; 37 | } 38 | 39 | public void setDescription(String description) { 40 | this.description = description; 41 | } 42 | 43 | public Adresse getAdresse() { 44 | return adresse; 45 | } 46 | 47 | public void setAdresse(Adresse adresse) { 48 | this.adresse = adresse; 49 | } 50 | 51 | public Point getLocation() { 52 | return location; 53 | } 54 | 55 | public void setLocation(Point location) { 56 | this.location = location; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /springdata-mongodb/rs-dev0.yml: -------------------------------------------------------------------------------- 1 | version: '3.4' 2 | 3 | services: 4 | 5 | # Primary 6 | mongo1: 7 | image: 'bitnami/mongodb:4.0.9' 8 | container_name: 'rs-dev0-1' 9 | environment: 10 | - MONGODB_PORT_NUMBER=27001 11 | - MONGODB_REPLICA_SET_NAME=dev0 12 | - MONGODB_REPLICA_SET_MODE=primary 13 | - MONGODB_ADVERTISED_HOSTNAME=mongo1 14 | ports: 15 | - "27001:27001" 16 | 17 | # Secondary 18 | mongo2: 19 | image: 'bitnami/mongodb:4.0.9' 20 | container_name: 'rs-dev0-2' 21 | depends_on: 22 | - mongo1 23 | ports: 24 | - "27002:27002" 25 | environment: 26 | - MONGODB_PORT_NUMBER=27002 27 | - MONGODB_REPLICA_SET_NAME=dev0 28 | - MONGODB_REPLICA_SET_MODE=secondary 29 | - MONGODB_ADVERTISED_HOSTNAME=mongo2 30 | - MONGODB_PRIMARY_HOST=mongo1 31 | - MONGODB_PRIMARY_PORT_NUMBER=27001 32 | 33 | # Arbiter 34 | mongo3: 35 | image: 'bitnami/mongodb:4.0.9' 36 | container_name: 'rs-dev0-3' 37 | depends_on: 38 | - mongo1 39 | ports: 40 | - "27003:27003" 41 | environment: 42 | - MONGODB_PORT_NUMBER=27003 43 | - MONGODB_REPLICA_SET_NAME=dev0 44 | - MONGODB_REPLICA_SET_MODE=arbiter 45 | - MONGODB_ADVERTISED_HOSTNAME=mongo3 46 | - MONGODB_PRIMARY_HOST=mongo1 47 | - MONGODB_PRIMARY_PORT_NUMBER=27001 48 | 49 | volumes: 50 | mongodb_master_data: 51 | driver: local -------------------------------------------------------------------------------- /springdata-mongodb/src/main/java/mongodb/order/Order.java: -------------------------------------------------------------------------------- 1 | package mongodb.order; 2 | 3 | import java.util.Date; 4 | import java.util.List; 5 | 6 | import org.springframework.data.annotation.Id; 7 | import org.springframework.data.mongodb.core.mapping.Field; 8 | 9 | public class Order { 10 | 11 | @Id private String id; 12 | 13 | private Date date; 14 | 15 | @Field("custInfo") private String customerInfo; 16 | 17 | List items; 18 | 19 | public Order() { 20 | this(null); 21 | } 22 | 23 | public Order(String customerInfo) { 24 | super(); 25 | this.customerInfo = customerInfo; 26 | } 27 | 28 | public Order(String id, String customerInfo) { 29 | this(customerInfo); 30 | this.id = id; 31 | } 32 | 33 | 34 | public String getId() { 35 | return id; 36 | } 37 | 38 | public void setId(String id) { 39 | this.id = id; 40 | } 41 | 42 | public Date getDate() { 43 | return date; 44 | } 45 | 46 | public void setDate(Date date) { 47 | this.date = date; 48 | } 49 | 50 | public String getCustomerInfo() { 51 | return customerInfo; 52 | } 53 | 54 | public void setCustomerInfo(String customerInfo) { 55 | this.customerInfo = customerInfo; 56 | } 57 | 58 | public List getItems() { 59 | return items; 60 | } 61 | 62 | public void setItems(List items) { 63 | this.items = items; 64 | } 65 | 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring Data Blog Series 2 | 3 | This projects holds the Java source examples for my blog post series on the Spring Data project: 4 | 5 | * [GridFS Support in Spring Data MongoDB](http://blog.codecentric.de/en/2012/07/gridfs-support-in-spring-data-mongodb/) 6 | * [Part 6: Spring Data Redis](http://blog.codecentric.de/en/2012/04/spring-data-redis/) 7 | * [Part 5: Spring Data Neo4j](http://blog.codecentric.de/en/2012/02/spring-data-neo4j/) 8 | * [Part 4: Geospatial Queries with Spring Data MongoDB](http://blog.codecentric.de/en/2012/02/spring-data-mongodb-geospatial-queries/) 9 | * [Part 3: Spring Data MongoDB](http://blog.codecentric.de/en/2012/02/spring-data-mongodb/) 10 | * [Part 2: Spring Data JPA](http://blog.codecentric.de/en/2012/01/spring-data-jpa/) 11 | * [Part 1: Spring Data Commons](http://blog.codecentric.de/en/2011/12/spring-data-commons/) 12 | 13 | ## Build Status @ Travis CI ## 14 | [![Build Status](https://travis-ci.org/ttrelle/spring-data-examples.png?branch=master)](https://travis-ci.org/ttrelle/spring-data-examples) 15 | 16 | ## Usage 17 | 18 | The projects are using a Maven based build. To run the tests on the command line use 19 | 20 | mvn clean test 21 | 22 | If you want to use an IDE like Eclipse run 23 | 24 | mvn eclipse:eclipse 25 | 26 | and set the Eclipse variable M2_REPO pointing to your local Maven repository. Or just import the project as an existing Maven project if you are using the m2 plugin. 27 | 28 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | de.codecentric 6 | springdata-examples 7 | 1.0.0 8 | pom 9 | 10 | Spring Data Examples 11 | 12 | 13 | springdata-mongodb 14 | springdata-redis 15 | springdata-neo4j 16 | springdata-jpa 17 | spring-boot-mongo 18 | mongodriver 19 | 20 | 21 | 22 | UTF-8 23 | 1.8 24 | 4.12 25 | 26 | 27 | 28 | 29 | 30 | org.apache.maven.plugins 31 | maven-compiler-plugin 32 | 3.5.1 33 | 34 | ${java.version} 35 | ${java.version} 36 | 37 | 38 | 39 | 40 | 41 | 42 | codecentric AG 43 | http://www.codecentric.de 44 | 45 | 46 | 47 | 48 | Tobias Trelle 49 | tobias.trelle@codecentric.de 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /springdata-mongodb/src/main/java/mongodb/geo/Location.java: -------------------------------------------------------------------------------- 1 | package mongodb.geo; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.mongodb.core.geo.GeoJsonPoint; 5 | 6 | /** 7 | * A named location, using GeoJSON API. 8 | * @author Tobias Trelle 9 | */ 10 | public class Location { 11 | 12 | @Id private String id; 13 | 14 | private GeoJsonPoint position; 15 | 16 | public Location() {} 17 | 18 | public Location(String id, double x, double y) { 19 | this.id = id; 20 | this.position = new GeoJsonPoint(x,y); 21 | } 22 | 23 | public String getId() { 24 | return id; 25 | } 26 | 27 | public void setId(String id) { 28 | this.id = id; 29 | } 30 | 31 | public GeoJsonPoint getPosition() { 32 | return position; 33 | } 34 | 35 | public void setPosition(GeoJsonPoint pos) { 36 | this.position = pos; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return String.format("%s(%1.3f, %1.3f)", id, position.getX(), position.getY()); 42 | } 43 | 44 | @Override 45 | public int hashCode() { 46 | final int prime = 31; 47 | int result = 1; 48 | result = prime * result + ((id == null) ? 0 : id.hashCode()); 49 | return result; 50 | } 51 | 52 | @Override 53 | public boolean equals(Object obj) { 54 | if (this == obj) 55 | return true; 56 | if (obj == null) 57 | return false; 58 | if (getClass() != obj.getClass()) 59 | return false; 60 | Location other = (Location) obj; 61 | if (id == null) { 62 | if (other.id != null) 63 | return false; 64 | } else if (!id.equals(other.id)) 65 | return false; 66 | return true; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /mongodriver/src/main/java/de/codecentric/transaction/SimpleTransaction.java: -------------------------------------------------------------------------------- 1 | package de.codecentric.transaction; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | import java.net.UnknownHostException; 5 | 6 | import org.bson.Document; 7 | 8 | import com.mongodb.MongoClient; 9 | import com.mongodb.MongoException; 10 | import com.mongodb.client.ClientSession; 11 | import com.mongodb.client.MongoCollection; 12 | import com.mongodb.client.MongoDatabase; 13 | 14 | import de.codecentric.Connection; 15 | 16 | /** 17 | * Simple transaction example. 18 | */ 19 | public class SimpleTransaction { 20 | 21 | /** 22 | * CLI call. 23 | * 24 | * @param argv 25 | * command line arguments 26 | * @throws MongoException 27 | * @throws UnknownHostException 28 | * @throws UnsupportedEncodingException 29 | */ 30 | public static void main(String[] argv) throws UnknownHostException, MongoException, UnsupportedEncodingException { 31 | try (MongoClient client = new MongoClient(Connection.URI)) { 32 | // use database "test" 33 | MongoDatabase db = client.getDatabase("test"); 34 | // collection must be created beforehand *outside* the transaction! 35 | MongoCollection collection = db.getCollection("foo"); 36 | 37 | try (ClientSession clientSession = client.startSession()) { 38 | clientSession.startTransaction(); 39 | collection.insertOne(clientSession, new Document("i", 1)); 40 | collection.insertOne(clientSession, new Document("i", 2)); 41 | clientSession.commitTransaction(); 42 | println("Transaction committed sucessfully."); 43 | } 44 | } 45 | } 46 | 47 | private static final void println(Object o) { 48 | System.out.println(o); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /springdata-neo4j/src/main/java/neo4j/domain/User.java: -------------------------------------------------------------------------------- 1 | package neo4j.domain; 2 | 3 | import java.util.Date; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | 7 | import org.neo4j.ogm.annotation.GraphId; 8 | import org.neo4j.ogm.annotation.Index; 9 | import org.neo4j.ogm.annotation.NodeEntity; 10 | import org.neo4j.ogm.annotation.Relationship; 11 | 12 | /** Simple user class. */ 13 | @NodeEntity public class User { 14 | 15 | @GraphId Long id; 16 | 17 | @Index private String login; 18 | 19 | private String fullName; 20 | 21 | private Date lastLogin; 22 | 23 | @Relationship(type = "knows") 24 | Set friends; 25 | 26 | public User() {} 27 | 28 | public User(String login, String fullName) { 29 | this.login = login; 30 | this.fullName = fullName; 31 | this.lastLogin = new Date(); 32 | this.friends = new HashSet(); 33 | } 34 | 35 | public void knows(User user) { 36 | friends.add(user); 37 | } 38 | 39 | public String getLogin() { 40 | return login; 41 | } 42 | 43 | public void setLogin(String login) { 44 | this.login = login; 45 | } 46 | 47 | public String getFullName() { 48 | return fullName; 49 | } 50 | 51 | public void setFullName(String fullName) { 52 | this.fullName = fullName; 53 | } 54 | 55 | public Date getLastLogin() { 56 | return lastLogin; 57 | } 58 | 59 | public void setLastLogin(Date lastLogin) { 60 | this.lastLogin = lastLogin; 61 | } 62 | 63 | public long getId() { 64 | return id; 65 | } 66 | 67 | public void setId(long id) { 68 | this.id = id; 69 | } 70 | 71 | public Set getFriends() { 72 | return friends; 73 | } 74 | 75 | public void setFriends(Set friends) { 76 | this.friends = friends; 77 | } 78 | 79 | public void setId(Long id) { 80 | this.id = id; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /springdata-mongodb/src/main/java/mongodb/user/User.java: -------------------------------------------------------------------------------- 1 | package mongodb.user; 2 | 3 | import java.util.Date; 4 | 5 | import org.springframework.data.annotation.Id; 6 | import org.springframework.data.mongodb.core.index.Indexed; 7 | import org.springframework.data.mongodb.core.mapping.Document; 8 | 9 | @Document 10 | public class User { 11 | 12 | @Id 13 | private String id; 14 | 15 | @Indexed(unique=true) 16 | private String fullName; 17 | 18 | private Date lastLogin; 19 | 20 | public User() {} 21 | 22 | public User(String id, String fullName) { 23 | this.id = id; 24 | this.fullName = fullName; 25 | this.lastLogin = new Date(); 26 | } 27 | 28 | 29 | public String getId() { 30 | return id; 31 | } 32 | 33 | public void setId(String id) { 34 | this.id = id; 35 | } 36 | 37 | public String getFullName() { 38 | return fullName; 39 | } 40 | 41 | public void setFullName(String fullName) { 42 | this.fullName = fullName; 43 | } 44 | 45 | public Date getLastLogin() { 46 | return lastLogin; 47 | } 48 | 49 | public void setLastLogin(Date lastLogin) { 50 | this.lastLogin = lastLogin; 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | final int prime = 31; 56 | int result = 1; 57 | result = prime * result + ((id == null) ? 0 : id.hashCode()); 58 | return result; 59 | } 60 | 61 | @Override 62 | public boolean equals(Object obj) { 63 | if (this == obj) 64 | return true; 65 | if (obj == null) 66 | return false; 67 | if (getClass() != obj.getClass()) 68 | return false; 69 | User other = (User) obj; 70 | if (id == null) { 71 | if (other.id != null) 72 | return false; 73 | } else if (!id.equals(other.id)) 74 | return false; 75 | return true; 76 | } 77 | 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /springdata-mongodb/src/test/java/mongodb/user/IndexViolationTest.java: -------------------------------------------------------------------------------- 1 | package mongodb.user; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.data.domain.Sort.Direction; 8 | import org.springframework.data.mongodb.core.MongoOperations; 9 | import org.springframework.data.mongodb.core.index.Index; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | import mongodb.config.LocalhostMongoConfiguration; 14 | import mongodb.user.User; 15 | import mongodb.user.UserRepository; 16 | 17 | /** 18 | * This test shows that violation on indexes are ignored. 19 | *

20 | * To run this test, a local mongod instance is required using the standard port. 21 | * 22 | * @author Tobias Trelle 23 | */ 24 | @RunWith(SpringJUnit4ClassRunner.class) 25 | @ContextConfiguration(classes=LocalhostMongoConfiguration.class) 26 | public class IndexViolationTest { 27 | 28 | private static final String COLLECTION_NAME = "user"; 29 | 30 | @Autowired UserRepository repo; 31 | 32 | @Autowired MongoOperations template; 33 | 34 | /** 35 | * Create a target collection. 36 | */ 37 | @Before 38 | public void setUp() { 39 | template.dropCollection(COLLECTION_NAME); 40 | template.createCollection(COLLECTION_NAME); 41 | template.indexOps(COLLECTION_NAME).ensureIndex(new Index().on("fullName", Direction.ASC).unique()); 42 | } 43 | 44 | @Test 45 | public void does_not_detect_index_violation_on_id() { 46 | // given 47 | repo.save( new User("0", "User 0") ); // 1st param = _id field, 2nd = unique secondary index 48 | 49 | // when 50 | repo.save( new User("0", "User 1") ); 51 | 52 | // then 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /springdata-jpa/src/main/java/jpa/domain/User.java: -------------------------------------------------------------------------------- 1 | package jpa.domain; 2 | 3 | import java.util.Date; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.Id; 7 | import javax.persistence.NamedQueries; 8 | import javax.persistence.NamedQuery; 9 | 10 | @Entity 11 | @NamedQueries( { 12 | @NamedQuery( name="User.findByUser5", query = "SELECT u FROM User u where u.fullName = 'User 5'" ), 13 | @NamedQuery( name="User.classicQuery", query = "SELECT u FROM User u where u.fullName = :fullName" ) 14 | }) 15 | public class User { 16 | 17 | @Id private String id; 18 | 19 | private String fullName; 20 | 21 | private Date lastLogin; 22 | 23 | public User() {} 24 | 25 | public User(String id, String fullName) { 26 | this.id = id; 27 | this.fullName = fullName; 28 | this.lastLogin = new Date(); 29 | } 30 | 31 | 32 | public String getId() { 33 | return id; 34 | } 35 | 36 | public void setId(String id) { 37 | this.id = id; 38 | } 39 | 40 | public String getFullName() { 41 | return fullName; 42 | } 43 | 44 | public void setFullName(String fullName) { 45 | this.fullName = fullName; 46 | } 47 | 48 | public Date getLastLogin() { 49 | return lastLogin; 50 | } 51 | 52 | public void setLastLogin(Date lastLogin) { 53 | this.lastLogin = lastLogin; 54 | } 55 | 56 | @Override 57 | public int hashCode() { 58 | final int prime = 31; 59 | int result = 1; 60 | result = prime * result + ((id == null) ? 0 : id.hashCode()); 61 | return result; 62 | } 63 | 64 | @Override 65 | public boolean equals(Object obj) { 66 | if (this == obj) 67 | return true; 68 | if (obj == null) 69 | return false; 70 | if (getClass() != obj.getClass()) 71 | return false; 72 | User other = (User) obj; 73 | if (id == null) { 74 | if (other.id != null) 75 | return false; 76 | } else if (!id.equals(other.id)) 77 | return false; 78 | return true; 79 | } 80 | 81 | 82 | 83 | } 84 | -------------------------------------------------------------------------------- /springdata-jpa/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | de.codecentric 7 | springdata-examples 8 | 1.0.0 9 | 10 | 11 | springdata-jpa-examples 12 | jar 13 | Spring Data JPA Examples 14 | https://github.com/ttrelle/spring-data-examples/tree/master/springdata-jpa 15 | 16 | 17 | 18 | 19 | org.springframework.data 20 | spring-data-jpa 21 | 1.9.4.RELEASE 22 | 23 | 24 | org.hibernate 25 | hibernate-entitymanager 26 | 4.2.21.Final 27 | 28 | 29 | joda-time 30 | joda-time 31 | 2.0 32 | 33 | 34 | 36 | 37 | 38 | cglib 39 | cglib-nodep 40 | 2.2 41 | 42 | 43 | 44 | junit 45 | junit 46 | ${junit.version} 47 | test 48 | 49 | 50 | org.springframework 51 | spring-test 52 | 4.1.9.RELEASE 53 | test 54 | 55 | 56 | org.hsqldb 57 | hsqldb 58 | 1.8.0.10 59 | test 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /springdata-jpa/src/test/resources/jpa/repo/JpaRepoTest-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /springdata-mongodb/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | de.codecentric 7 | springdata-examples 8 | 1.0.0 9 | 10 | 11 | springdata-mongodb-examples 12 | jar 13 | Spring Data MongoDB Examples 14 | https://github.com/ttrelle/spring-data-examples/tree/master/springdata-mongodb 15 | 16 | 17 | 2.1.8.RELEASE 18 | 3.4.2 19 | 4.2.9.RELEASE 20 | 21 | 22 | 23 | 24 | 31 | 32 | org.springframework.data 33 | spring-data-mongodb 34 | ${spring.data.mongodb.version} 35 | 36 | 37 | 38 | junit 39 | junit 40 | ${junit.version} 41 | test 42 | 43 | 44 | org.springframework 45 | spring-test 46 | ${spring.version} 47 | test 48 | 49 | 50 | 51 | 52 | 53 | 54 | src/misc/java 55 | test-classes 56 | 57 | 58 | src/misc/resources 59 | test-classes 60 | 61 | 62 | 63 | 64 | 65 | 66 | spring-snapshot 67 | Spring Maven SNAPSHOT Repository 68 | http://repo.springsource.org/libs-snapshot 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /springdata-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | de.codecentric 7 | springdata-examples 8 | 1.0.0 9 | 10 | 11 | springdata-redis-examples 12 | jar 13 | Spring Data Redis Examples 14 | https://github.com/ttrelle/spring-data-examples/tree/master/springdata-redis 15 | 16 | 17 | 18 | org.springframework.data 19 | spring-data-redis 20 | 1.6.4.RELEASE 21 | 22 | 23 | org.slf4j 24 | slf4j-simple 25 | 1.7.13 26 | 27 | 28 | 29 | 30 | junit 31 | junit 32 | ${junit.version} 33 | test 34 | 35 | 36 | org.springframework 37 | spring-test 38 | 4.1.9.RELEASE 39 | test 40 | 41 | 42 | redis.clients 43 | jedis 44 | 2.7.3 45 | 46 | 47 | org.apache.commons 48 | commons-pool2 49 | 2.2 50 | test 51 | 52 | 53 | org.codehaus.jackson 54 | jackson-mapper-asl 55 | 1.8.8 56 | test 57 | 58 | 59 | com.fasterxml.jackson.core 60 | jackson-core 61 | 2.6.4 62 | test 63 | 64 | 65 | com.fasterxml.jackson.core 66 | jackson-databind 67 | 2.6.4 68 | test 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /springdata-mongodb/src/misc/java/mongodb/CappedCollectionManipulationTest.java: -------------------------------------------------------------------------------- 1 | package mongodb; 2 | 3 | import static org.hamcrest.core.Is.*; 4 | import static org.junit.Assert.*; 5 | 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.data.mongodb.core.CollectionOptions; 11 | import org.springframework.data.mongodb.core.MongoOperations; 12 | import org.springframework.test.context.ContextConfiguration; 13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 14 | 15 | /** 16 | * This test shows that deletes and updates to capped collections fail silently. 17 | *

18 | * To run this test, a local mongod instance is required using the standard port. 19 | * 20 | * @author Tobias Trelle 21 | */ 22 | @RunWith(SpringJUnit4ClassRunner.class) 23 | @ContextConfiguration 24 | public class CappedCollectionManipulationTest { 25 | 26 | private static final String COLLECTION_NAME = "user"; 27 | 28 | @Autowired UserRepository repo; 29 | 30 | @Autowired MongoOperations template; 31 | 32 | /** 33 | * Create a capped collection. 34 | * If you create an uncapped collections the tests go green. 35 | */ 36 | @Before 37 | public void setUp() { 38 | template.dropCollection(COLLECTION_NAME); 39 | 40 | // capped 41 | template.createCollection(COLLECTION_NAME, new CollectionOptions(1000,5,true)); 42 | 43 | // uncapped 44 | //template.createCollection(COLLECTION_NAME); 45 | } 46 | 47 | @Test 48 | public void does_not_delete_from_capped_collection() { 49 | // given 50 | for ( int i = 0; i< 5; i++ ) { 51 | repo.save(new User("user"+i, "Herr User Nr. " +i)); 52 | } 53 | 54 | // when 55 | repo.delete("user0"); 56 | 57 | // then 58 | assertNull("User not deleted", repo.findOne("user0")); 59 | } 60 | 61 | @Test 62 | public void does_not_update_capped_collection() { 63 | // given 64 | for ( int i = 0; i< 5; i++ ) { 65 | repo.save(new User("user"+i, "Herr User Nr. " +i)); 66 | } 67 | 68 | // when 69 | User user; 70 | user = repo.findOne("user0"); 71 | user.setFullName( "Something completely different" ); 72 | repo.save(user); 73 | user = repo.findOne("user0"); 74 | 75 | // then 76 | assertThat("Fullname mismatch", 77 | user.getFullName(), is("Something completely different") ); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /mongodriver/src/main/java/de/codecentric/driverclient/OrderExample.java: -------------------------------------------------------------------------------- 1 | package de.codecentric.driverclient; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | import java.net.UnknownHostException; 5 | import java.util.ArrayList; 6 | import java.util.Date; 7 | import java.util.List; 8 | 9 | import org.bson.Document; 10 | import org.bson.conversions.Bson; 11 | 12 | import com.mongodb.BasicDBObject; 13 | import com.mongodb.MongoClient; 14 | import com.mongodb.MongoException; 15 | import com.mongodb.client.FindIterable; 16 | import com.mongodb.client.MongoCollection; 17 | import com.mongodb.client.MongoDatabase; 18 | 19 | /** 20 | * Simple MongoDB client based on the MongoDB Java driver API. 21 | */ 22 | public class OrderExample { 23 | 24 | /** 25 | * CLI call. 26 | * @param argv command line arguments 27 | * @throws MongoException 28 | * @throws UnknownHostException 29 | * @throws UnsupportedEncodingException 30 | */ 31 | public static void main(String[] argv) throws UnknownHostException, MongoException, UnsupportedEncodingException { 32 | MongoClient mongo; 33 | 34 | // Default: localhost:27017 35 | mongo = new MongoClient(); 36 | 37 | MongoDatabase db = mongo.getDatabase("test"); 38 | MongoCollection collection = db.getCollection("order"); 39 | 40 | insert(collection); 41 | find(mongo); 42 | } 43 | 44 | private static void insert(MongoCollection collection) { 45 | Document order; 46 | List items = new ArrayList(); 47 | Document item; 48 | 49 | // order 50 | order = new Document(); 51 | order.put("date", new Date()); 52 | order.put("custInfo" , "Tobias Trelle"); 53 | order.put("items", items); 54 | // items 55 | item = new Document(); 56 | item.put("quantity", 1); 57 | item.put("price", 47.11); 58 | item.put("desc", "Item #1"); 59 | items.add(item); 60 | item = new Document(); 61 | item.put("quantity", 2); 62 | item.put("price", 42.0); 63 | item.put("desc", "Item #2"); 64 | items.add(item); 65 | 66 | collection.insertOne(order); 67 | } 68 | 69 | private static void find(MongoClient mongo) { 70 | MongoDatabase db = mongo.getDatabase("test"); 71 | MongoCollection collection = db.getCollection("order"); 72 | Bson query; 73 | FindIterable cursor; 74 | 75 | query = new BasicDBObject("items.quantity", 2); 76 | cursor = collection.find(query); 77 | 78 | for(Document document: cursor ) { 79 | println(document); 80 | } 81 | } 82 | 83 | private static final void println(Object o) { 84 | System.out.println(o); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /mongodriver/src/main/java/de/codecentric/driverclient/ReplicaSetTimeout.java: -------------------------------------------------------------------------------- 1 | package de.codecentric.driverclient; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.Date; 6 | import java.util.List; 7 | import java.util.concurrent.TimeUnit; 8 | 9 | import org.bson.Document; 10 | 11 | import com.mongodb.MongoClient; 12 | import com.mongodb.MongoException; 13 | import com.mongodb.ServerAddress; 14 | import com.mongodb.WriteConcern; 15 | import com.mongodb.client.MongoCollection; 16 | import com.mongodb.client.MongoDatabase; 17 | import com.mongodb.client.model.BulkWriteOptions; 18 | import com.mongodb.client.model.InsertOneModel; 19 | import com.mongodb.client.model.WriteModel; 20 | 21 | public class ReplicaSetTimeout { 22 | 23 | private static final WriteConcern WRITE_CONCERN = WriteConcern.ACKNOWLEDGED 24 | .withW("majority") 25 | .withWTimeout(200, TimeUnit.MILLISECONDS) 26 | .withJournal(true) 27 | ; 28 | 29 | private static final int BULK_SIZE = 10000; 30 | 31 | public static void main(String[] argv) { 32 | new ReplicaSetTimeout().execute(); 33 | } 34 | 35 | public void execute() { 36 | MongoClient mongo = null; 37 | 38 | mongo = new MongoClient(Arrays.asList( // 39 | new ServerAddress("localhost", 27001), // 40 | new ServerAddress("localhost", 27002), // 41 | new ServerAddress("localhost", 27003))); 42 | 43 | try { 44 | MongoDatabase db = mongo.getDatabase("test"); 45 | MongoCollection collection = db.getCollection("foo").withWriteConcern(WRITE_CONCERN); 46 | System.out.println("WriteConcern: " + collection.getWriteConcern()); 47 | 48 | // remove 49 | collection.deleteMany(new Document()); 50 | 51 | // bulk insert 52 | BulkWriteOptions opts = new BulkWriteOptions(); 53 | opts.ordered(true); 54 | 55 | long time = System.currentTimeMillis(); 56 | collection.bulkWrite( createBulk(), opts ); 57 | time = System.currentTimeMillis() - time; 58 | System.out.println("*** runtime [ms]: " + time); 59 | 60 | 61 | } catch (MongoException e) { 62 | e.printStackTrace(); 63 | } finally { 64 | if (mongo != null) { 65 | mongo.close(); 66 | } 67 | } 68 | 69 | } 70 | 71 | private List> createBulk() { 72 | final List> ops = new ArrayList>(); 73 | Document payload; 74 | 75 | for(int i=0; i(payload) ); 81 | } 82 | 83 | return ops; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /springdata-neo4j/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | de.codecentric 7 | springdata-examples 8 | 1.0.0 9 | 10 | 11 | springdata-neo4j-examples 12 | jar 13 | Spring Data Neo4j Examples 14 | http://maven.apache.org 15 | 16 | 17 | 2.2.5 18 | 19 | 20 | 21 | 22 | 23 | org.springframework.data 24 | spring-data-neo4j 25 | 4.0.0.RELEASE 26 | 27 | 28 | 29 | 30 | junit 31 | junit 32 | ${junit.version} 33 | test 34 | 35 | 36 | org.springframework.data 37 | spring-data-neo4j 38 | 4.0.0.RELEASE 39 | test-jar 40 | test 41 | 42 | 43 | org.neo4j 44 | neo4j-kernel 45 | ${neo4j.version} 46 | test-jar 47 | test 48 | 49 | 50 | org.neo4j.test 51 | neo4j-harness 52 | ${neo4j.version} 53 | test 54 | 55 | 56 | org.neo4j.app 57 | neo4j-server 58 | ${neo4j.version} 59 | test-jar 60 | test 61 | 62 | 63 | org.neo4j 64 | neo4j-ogm 65 | 1.1.2 66 | test-jar 67 | test 68 | 69 | 70 | cglib 71 | cglib 72 | 2.2.2 73 | 74 | 75 | 76 | javax.validation 77 | validation-api 78 | 1.1.0.Final 79 | 80 | 81 | 82 | org.springframework 83 | spring-test 84 | 4.1.9.RELEASE 85 | test 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /springdata-mongodb/src/test/java/mongodb/user/UserRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package mongodb.user; 2 | 3 | import static org.hamcrest.CoreMatchers.is; 4 | import static org.hamcrest.CoreMatchers.notNullValue; 5 | import static org.junit.Assert.assertThat; 6 | 7 | import java.util.List; 8 | 9 | import org.junit.After; 10 | import org.junit.Before; 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.data.domain.Page; 15 | import org.springframework.data.domain.PageRequest; 16 | import org.springframework.data.mongodb.core.MongoTemplate; 17 | import org.springframework.test.context.ContextConfiguration; 18 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 19 | 20 | import mongodb.config.LocalhostMongoConfiguration; 21 | import mongodb.user.User; 22 | import mongodb.user.UserRepository; 23 | 24 | /** 25 | * Tests for Spring Data MongoDB. 26 | * 27 | * @author Tobias Trelle 28 | */ 29 | @RunWith(SpringJUnit4ClassRunner.class) 30 | @ContextConfiguration(classes=LocalhostMongoConfiguration.class) 31 | public class UserRepositoryTest { 32 | 33 | @Autowired UserRepository repo; 34 | 35 | @Autowired MongoTemplate template; 36 | 37 | @Before public void setUp() { 38 | 39 | template.dropCollection("user"); 40 | template.createCollection("user"); 41 | 42 | repo.save(new User("root", "Superuser")); 43 | for ( int i = 0; i < 6; i++ ) { 44 | repo.save( new User( String.format("user%02d", i), "User " + i ) ); 45 | } 46 | } 47 | 48 | @Test public void shouldFindByCustomerQuery() { 49 | List users; 50 | 51 | // when 52 | users = repo.findByTheUsersFullName("User 0"); 53 | 54 | // then 55 | assertUserByFullName(users, "User 0"); 56 | } 57 | 58 | 59 | @Test public void shouldFindByFullNameLike() { 60 | List users; 61 | 62 | // when 63 | users = repo.findByFullNameLike("User", null); 64 | 65 | // then 66 | assertUserCount(users, 6); 67 | } 68 | 69 | @Test public void shouldPageUsers() { 70 | List users; 71 | 72 | // when 73 | Page page = repo.findAll( new PageRequest(2, 2 ) ); 74 | users = page.getContent(); 75 | 76 | // then 77 | assertUserCount(users, 2); 78 | } 79 | 80 | @After public void tearDown() { 81 | repo.deleteAll(); 82 | } 83 | 84 | private static void assertUserByFullName(List users, String fullName) { 85 | assertUserCount(users, 1); 86 | assertThat( "Mismatch full name", users.get(0).getFullName(), is(fullName)); 87 | } 88 | 89 | private static void assertUserCount(List users, int expected) { 90 | assertThat( users, notNullValue() ); 91 | assertThat( "Mismatch user count", users.size(), is(expected)); 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /springdata-neo4j/src/test/java/neo4j/repo/Neo4jRepoTest.java: -------------------------------------------------------------------------------- 1 | package neo4j.repo; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | 6 | import java.util.HashMap; 7 | import java.util.List; 8 | 9 | import neo4j.domain.User; 10 | import neo4j.repo.UserRepository; 11 | 12 | import org.junit.After; 13 | import org.junit.Before; 14 | import org.junit.Ignore; 15 | import org.junit.Test; 16 | import org.junit.runner.RunWith; 17 | import org.neo4j.ogm.session.result.Result; 18 | import org.springframework.beans.factory.annotation.Autowired; 19 | import org.springframework.data.neo4j.template.Neo4jOperations; 20 | import org.springframework.test.context.ContextConfiguration; 21 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 22 | 23 | @RunWith(SpringJUnit4ClassRunner.class) 24 | @ContextConfiguration(classes = Neo4jTestConfig.class) 25 | public class Neo4jRepoTest { 26 | 27 | private static final int USER_COUNT = 4; 28 | 29 | @Autowired 30 | Neo4jOperations template; 31 | 32 | @Autowired 33 | UserRepository repo; 34 | 35 | private long rootId; 36 | 37 | @Before 38 | public void setUp() { 39 | // create index User(login) 40 | Result result = template.query("CREATE INDEX ON :User(login)", new HashMap() ); 41 | 42 | User root = new User("root", "Superuser"); 43 | User[] user = new User[USER_COUNT]; 44 | 45 | for (int i = 0; i < user.length; i++) { 46 | user[i] = new User(String.format("user%02d", i), "User" + i); 47 | } 48 | 49 | // build graph 50 | for (int i = 0; i < user.length; i++) { 51 | root.knows(user[i]); 52 | for (int j = i; j < user.length; j++) { 53 | user[i].knows(user[j]); 54 | } 55 | } 56 | 57 | // save nodes 58 | for (int i = 0; i < user.length; i++) { 59 | repo.save(user[i]); 60 | } 61 | repo.save(root); 62 | rootId = root.getId(); 63 | out("Root id: " + rootId); 64 | 65 | } 66 | 67 | @Test 68 | public void shouldFindAll() { 69 | // when 70 | long n = repo.count(); 71 | 72 | // then 73 | assertEquals("User count mismatch", USER_COUNT + 1, n); 74 | } 75 | 76 | @Test 77 | public void shouldFindRootUserById() { 78 | // when 79 | User root = repo.findOne(rootId); 80 | 81 | // then 82 | assertNotNull("Root user not found", root); 83 | } 84 | 85 | @Test 86 | public void shouldFindRootUserByLogin() { 87 | // when 88 | User root = repo.findByLogin("root"); 89 | 90 | // then 91 | assertNotNull("Root user not found", root); 92 | } 93 | 94 | // TODO fixme 95 | // Caused by: org.neo4j.ogm.session.result.ResultProcessingException: "errors":[{"code":"Neo.ClientError.Schema.NoSuchIndex 96 | // ","message":"Index `User` does not exist"}]} 97 | @Test 98 | @Ignore 99 | public void shouldFindFriendsOfRoot() { 100 | // when 101 | List users = repo.findFriendsOfRoot(); 102 | 103 | // then 104 | assertNotNull("result is ", users); 105 | assertEquals("mismatch @ friend count", USER_COUNT, users.size()); 106 | } 107 | 108 | @After 109 | public void tearDown() { 110 | repo.deleteAll(); 111 | } 112 | 113 | private static void out(Object o) { 114 | System.out.println(o); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /springdata-mongodb/src/test/java/mongodb/order/OrderSubListTest.java: -------------------------------------------------------------------------------- 1 | package mongodb.order; 2 | 3 | import static org.hamcrest.CoreMatchers.is; 4 | import static org.hamcrest.CoreMatchers.notNullValue; 5 | import static org.junit.Assert.assertThat; 6 | import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation; 7 | import static org.springframework.data.mongodb.core.aggregation.Aggregation.project; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | import org.junit.Before; 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | import org.springframework.beans.factory.annotation.Autowired; 16 | import org.springframework.data.mongodb.core.MongoOperations; 17 | import org.springframework.data.mongodb.core.aggregation.Aggregation; 18 | import org.springframework.data.mongodb.core.aggregation.AggregationResults; 19 | import org.springframework.test.context.ContextConfiguration; 20 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 21 | 22 | import mongodb.config.LocalhostMongoConfiguration; 23 | import mongodb.order.Item; 24 | import mongodb.order.Order; 25 | import mongodb.order.OrderRepository; 26 | 27 | /** 28 | * Test cases for aggregations. 29 | * 30 | * @author Tobias Trelle 31 | */ 32 | @RunWith(SpringJUnit4ClassRunner.class) 33 | @ContextConfiguration(classes=LocalhostMongoConfiguration.class) 34 | public class OrderSubListTest { 35 | 36 | @Autowired OrderRepository repo; 37 | 38 | @Autowired MongoOperations template; 39 | 40 | @Before public void setUp() { 41 | repo.deleteAll(); 42 | } 43 | 44 | @Test public void projection_by_aggregation() { 45 | // given 46 | Order order = new Order("Tobias Trelle, gold customer"); 47 | List items = new ArrayList(); 48 | items.add( new Item(1, 47.11, "Item #1") ); 49 | items.add( new Item(2, 42.0, "Item #2") ); 50 | order.setItems(items); 51 | repo.save(order); 52 | 53 | // when 54 | Aggregation agg = newAggregation( 55 | project("items").andExclude("_id") 56 | ); 57 | AggregationResults result = template.aggregate(agg, Order.class, Order.class); 58 | 59 | // then 60 | assertThat(result, notNullValue()); 61 | List orders = result.getMappedResults(); 62 | assertThat(orders, notNullValue()); 63 | assertThat(orders.size(), is(1)); 64 | items = orders.get(0).getItems(); 65 | assertThat(items, notNullValue()); 66 | assertThat(items.size(), is(2)); 67 | } 68 | 69 | @Test public void projection_by_query() { 70 | // given 71 | Order order = new Order("Tobias Trelle, gold customer"); 72 | List items = new ArrayList(); 73 | items.add( new Item(1, 47.11, "Item #1") ); 74 | items.add( new Item(2, 42.0, "Item #2") ); 75 | order.setItems(items); 76 | repo.save(order); 77 | 78 | // when 79 | Aggregation agg = newAggregation( 80 | project("items").andExclude("_id") 81 | ); 82 | AggregationResults result = template.aggregate(agg, Order.class, Order.class); 83 | 84 | // then 85 | assertThat(result, notNullValue()); 86 | List orders = result.getMappedResults(); 87 | assertThat(orders, notNullValue()); 88 | assertThat(orders.size(), is(1)); 89 | items = orders.get(0).getItems(); 90 | assertThat(items, notNullValue()); 91 | assertThat(items.size(), is(2)); 92 | } 93 | 94 | } -------------------------------------------------------------------------------- /springdata-mongodb/src/test/java/mongodb/order/OrderRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package mongodb.order; 2 | 3 | import static org.hamcrest.CoreMatchers.is; 4 | import static org.hamcrest.CoreMatchers.notNullValue; 5 | import static org.hamcrest.CoreMatchers.nullValue; 6 | import static org.junit.Assert.assertThat; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.test.context.ContextConfiguration; 16 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 17 | 18 | import mongodb.config.LocalhostMongoConfiguration; 19 | import mongodb.order.Item; 20 | import mongodb.order.Order; 21 | import mongodb.order.OrderRepository; 22 | 23 | /** 24 | * Test cases for the {@link OrderRepository}. 25 | * 26 | * @author Tobias Trelle 27 | */ 28 | @RunWith(SpringJUnit4ClassRunner.class) 29 | @ContextConfiguration(classes=LocalhostMongoConfiguration.class) 30 | public class OrderRepositoryTest { 31 | 32 | @Autowired OrderRepository repo; 33 | 34 | @Before public void setUp() { 35 | repo.deleteAll(); 36 | } 37 | 38 | @Test public void shouldFindByItemsQuantity() { 39 | // given 40 | Order order = new Order("Tobias Trelle, gold customer"); 41 | List items = new ArrayList(); 42 | items.add( new Item(1, 47.11, "Item #1") ); 43 | items.add( new Item(2, 42.0, "Item #2") ); 44 | order.setItems(items); 45 | repo.save(order); 46 | 47 | // when 48 | List orders = repo.findByItemsQuantity(2); 49 | 50 | // then 51 | assertThat(orders, notNullValue()); 52 | assertThat(orders.size(), is(1)); 53 | } 54 | 55 | @Test public void shouldFindByAnnotatedQuery() { 56 | // given 57 | Order order = new Order("Tobias Trelle, gold customer"); 58 | List items = new ArrayList(); 59 | items.add( new Item(1, 47.11, "Item #1") ); 60 | items.add( new Item(2, 42.0, "Item #2") ); 61 | order.setItems(items); 62 | repo.save(order); 63 | 64 | // when 65 | List orders = repo.findWithQuery(2); 66 | 67 | // then 68 | assertThat(orders, notNullValue()); 69 | assertThat(orders.size(), is(1)); 70 | } 71 | 72 | @Test public void use_field_projection() { 73 | // given 74 | Order order = new Order("Tobias Trelle, gold customer"); 75 | List items = new ArrayList(); 76 | items.add( new Item(1, 47.11, "Item #1") ); 77 | items.add( new Item(2, 42.0, "Item #2") ); 78 | order.setItems(items); 79 | repo.save(order); 80 | 81 | // when 82 | List orders = repo.findOnlyItems("Tobias Trelle, gold customer"); 83 | 84 | // then 85 | assertThat(orders, notNullValue()); 86 | assertThat(orders.size(), is(1)); 87 | order = orders.get(0); 88 | assertThat(order.getId(), nullValue()); 89 | assertThat(order.getCustomerInfo(), nullValue()); 90 | assertThat(order.getDate(), nullValue()); 91 | 92 | items = order.getItems(); 93 | assertThat(items, notNullValue()); 94 | assertThat(items.size(), is(2)); 95 | Item item = items.get(0); 96 | assertThat( item.getDescription(), notNullValue()); 97 | assertThat( item.getPrice(), notNullValue()); 98 | assertThat( item.getQuantity(), notNullValue()); 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /spring-boot-mongo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.springframework.boot 8 | spring-boot-starter-parent 9 | 1.5.8.RELEASE 10 | 11 | 12 | 13 | de.codecentric 14 | spring-boot-mongo 15 | 0.0.1-SNAPSHOT 16 | jar 17 | SpringBoot MongoDB REST 18 | Demo project for Spring Boot MongoDB 19 | 20 | 21 | UTF-8 22 | UTF-8 23 | 1.8 24 | 0.4.11 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-mongodb 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-data-rest 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-web 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-actuator 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | 59 | 60 | 61 | 62 | 63 | docker 64 | 65 | 66 | 67 | com.spotify 68 | docker-maven-plugin 69 | ${docker-maven-plugin.version} 70 | 71 | mongoboot/${project.artifactId} 72 | ${project.basedir}/src/main/docker 73 | 74 | 75 | / 76 | ${project.build.directory} 77 | ${project.build.finalName}.jar 78 | 79 | 80 | 81 | 82 | 83 | build-image 84 | package 85 | 86 | build 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /springdata-mongodb/src/misc/java/mongodb/MongoDBMassTest.java: -------------------------------------------------------------------------------- 1 | package mongodb; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.data.mongodb.core.MongoTemplate; 13 | import org.springframework.data.mongodb.core.geo.Box; 14 | import org.springframework.data.mongodb.core.geo.Circle; 15 | import org.springframework.data.mongodb.core.geo.Point; 16 | import org.springframework.data.mongodb.core.index.GeospatialIndex; 17 | import org.springframework.test.context.ContextConfiguration; 18 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 19 | 20 | /** 21 | * Tests for Spring Data MongoDB - Geospatial queries. 22 | * 23 | * @author Tobias 24 | * Trelle 25 | */ 26 | @RunWith(SpringJUnit4ClassRunner.class) 27 | @ContextConfiguration 28 | public class MongoDBMassTest { 29 | 30 | private static final int N_POINTS = 100000; 31 | 32 | private static final int N_QUERIES = 10000; 33 | 34 | /** Radius for circle search. */ 35 | private static final double RADIUS = 0.1; 36 | 37 | /** Half the width of a box with the same area as a circle with r = {@link #RADIUS}. */ 38 | private static final double HALF_BOX_WIDTH = RADIUS * Math.sqrt(Math.PI) / 2; 39 | 40 | @Autowired 41 | LocationRepository repo; 42 | 43 | @Autowired 44 | MongoTemplate template; 45 | 46 | @Before 47 | public void setUp() { 48 | final List locations = new ArrayList(); 49 | 50 | // ensure geospatial index 51 | template.indexOps(Location.class).ensureIndex( new GeospatialIndex("position") ); 52 | 53 | // prepare data 54 | for ( int i = 0; i < N_POINTS; i++ ) { 55 | locations.add( new Location(String.valueOf(i), rnd(), rnd()) ); 56 | } 57 | out("Preparing data (" + N_POINTS + " locations) ..."); 58 | repo.save(locations); 59 | out("Done."); 60 | } 61 | 62 | @Test 63 | public void shouldPerformRandomQueries() { 64 | long time_circle = 0; 65 | long time_box = 0; 66 | long singleRun = 0; 67 | Circle c; 68 | Box b; 69 | long n_circle = 0; 70 | long n_box = 0; 71 | List result; 72 | 73 | out( "Performing " + N_QUERIES + " queries ..." ); 74 | for (int i = 0; i < N_QUERIES; i++) { 75 | 76 | double x = rnd(), y = rnd(); 77 | 78 | // circle search 79 | c = new Circle( x, y, RADIUS); 80 | // box search 81 | b = new Box( new Point(x - HALF_BOX_WIDTH, y - HALF_BOX_WIDTH), new Point(x + HALF_BOX_WIDTH, y + HALF_BOX_WIDTH) ); 82 | 83 | // perform circle query 84 | singleRun = System.currentTimeMillis(); 85 | result = repo.findByPositionWithin(c); 86 | singleRun = System.currentTimeMillis() - singleRun; 87 | n_circle += result.size(); 88 | time_circle += singleRun; 89 | 90 | // peform box search 91 | singleRun = System.currentTimeMillis(); 92 | result = repo.findByPositionWithin(b); 93 | singleRun = System.currentTimeMillis() - singleRun; 94 | n_box += result.size(); 95 | time_box += singleRun; 96 | 97 | } 98 | out( "Done.\n" ); 99 | 100 | out( "Search by circle:" ); 101 | out( "Overall time for " + N_QUERIES + " queries [ms]: " + time_circle ); 102 | out( "Average time per query [ms]: " + (double)time_circle / N_QUERIES ); 103 | out( "Average hits per query: " + n_circle / N_QUERIES ); 104 | out( "" ); 105 | out( "Search by box:" ); 106 | out( "Overall time for " + N_QUERIES + " queries [ms]: " + time_box ); 107 | out( "Average time per query [ms]: " + (double)time_box / N_QUERIES ); 108 | out( "Average hits per query: " + n_box / N_QUERIES ); 109 | } 110 | 111 | @After 112 | public void tearDown() { 113 | repo.deleteAll(); 114 | } 115 | 116 | private static double rnd() { 117 | return 2 * Math.random() -1; 118 | } 119 | 120 | private static void out(Object o) { 121 | System.out.println(o); 122 | } 123 | 124 | } 125 | -------------------------------------------------------------------------------- /springdata-jpa/src/test/java/jpa/repo/JpaRepoTest.java: -------------------------------------------------------------------------------- 1 | package jpa.repo; 2 | 3 | import static org.hamcrest.CoreMatchers.is; 4 | import static org.hamcrest.CoreMatchers.notNullValue; 5 | import static org.junit.Assert.assertThat; 6 | 7 | import java.util.List; 8 | 9 | import jpa.domain.User; 10 | 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.data.domain.Page; 16 | import org.springframework.data.domain.PageRequest; 17 | import org.springframework.data.domain.Sort; 18 | import org.springframework.test.context.ContextConfiguration; 19 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 20 | 21 | /** 22 | * Tests for Spring Data JPA. 23 | * 24 | * @author Tobias Trelle 25 | */ 26 | @RunWith(SpringJUnit4ClassRunner.class) 27 | @ContextConfiguration 28 | public class JpaRepoTest { 29 | 30 | @Autowired UserRepository repo; 31 | 32 | @Autowired ClassicUserRepository classicRepo; 33 | 34 | @Before public void setUp() { 35 | for ( int i = 0; i < 6; i++ ) { 36 | repo.save( new User( String.format("user%02d", i), "User " + i ) ); 37 | } 38 | } 39 | 40 | @Test public void shouldUseClassicRepository() { 41 | List users; 42 | 43 | // when 44 | users = classicRepo.findByFullName("User 1"); 45 | 46 | // then 47 | assertUserByFullName(users, "User 1"); 48 | } 49 | 50 | @Test public void shouldPageUsers() { 51 | List users; 52 | 53 | // when 54 | Page page = repo.findAll( new PageRequest(2, 2 ) ); 55 | users = page.getContent(); 56 | 57 | // then 58 | assertUserCount(users, 2); 59 | } 60 | 61 | @Test public void shouldFindByFullnameQuery() { 62 | List users; 63 | 64 | // when 65 | users = repo.findByFullName("User 5"); 66 | 67 | // then 68 | assertUserByFullName(users, "User 5"); 69 | } 70 | 71 | @Test public void shouldFindByFullnameQueryWithSort() { 72 | List users; 73 | 74 | // when 75 | users = repo.findByFullName("User 5", new Sort( new Sort.Order(Sort.Direction.DESC,"fullName"))); 76 | 77 | // then 78 | assertUserByFullName(users, "User 5"); 79 | } 80 | 81 | @Test public void shouldUseExistingNamedQuery() { 82 | List users; 83 | 84 | // when 85 | users = repo.findByUser5(); 86 | 87 | // then 88 | assertUserByFullName(users, "User 5"); 89 | } 90 | 91 | @Test public void shouldUseXmlNamedQuery() { 92 | List users; 93 | 94 | // when 95 | users = repo.findByOrm(); 96 | 97 | // then 98 | assertUserByFullName(users, "User 2"); 99 | } 100 | 101 | @Test public void shouldUseSpringDataQuery() { 102 | List users; 103 | 104 | // when 105 | users = repo.findByGivenQuery(); 106 | 107 | // then 108 | assertUserByFullName(users, "User 3"); 109 | } 110 | 111 | @Test public void shouldIgnoreNullQueryParameters() { 112 | List usersById, usersByFullName; 113 | 114 | // when 115 | usersById = repo.findByIdAndFullName("user01", null); 116 | usersByFullName = repo.findByIdAndFullName(null, "User 01"); 117 | 118 | // then 119 | assertUserCount(usersById, 0); 120 | assertUserCount(usersByFullName, 0); 121 | } 122 | 123 | @Test public void shouldSortByTwoCriteria() { 124 | List users; 125 | 126 | // when 127 | users = repo.findAll( new Sort( 128 | new Sort.Order(Sort.Direction.ASC, "id"), 129 | new Sort.Order(Sort.Direction.DESC, "fullName") 130 | ) 131 | ); 132 | 133 | // then 134 | assertUserCount(users, 6); 135 | } 136 | 137 | private static void assertUserByFullName(List users, String fullName) { 138 | assertUserCount(users, 1); 139 | assertThat( "Mismatch full name", users.get(0).getFullName(), is(fullName)); 140 | } 141 | 142 | private static void assertUserCount(List users, int expected) { 143 | assertThat( users, notNullValue() ); 144 | assertThat( "Mismatch user count", users.size(), is(expected)); 145 | } 146 | 147 | } 148 | -------------------------------------------------------------------------------- /mongodriver/src/main/java/de/codecentric/driverclient/SimpleMongoClient.java: -------------------------------------------------------------------------------- 1 | package de.codecentric.driverclient; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | import java.net.UnknownHostException; 5 | import java.util.Date; 6 | 7 | import org.bson.BSON; 8 | import org.bson.Document; 9 | import org.bson.conversions.Bson; 10 | 11 | import com.mongodb.BasicDBObject; 12 | import com.mongodb.DBObject; 13 | import com.mongodb.MongoClient; 14 | import com.mongodb.MongoException; 15 | import com.mongodb.client.FindIterable; 16 | import com.mongodb.client.MongoCollection; 17 | import com.mongodb.client.MongoDatabase; 18 | import com.mongodb.client.MongoIterable; 19 | 20 | /** 21 | * Simple MongoDB client based on the MongoDB Java driver API. 22 | */ 23 | public class SimpleMongoClient { 24 | 25 | /** 26 | * CLI call. 27 | * @param argv command line arguments 28 | * @throws MongoException 29 | * @throws UnknownHostException 30 | * @throws UnsupportedEncodingException 31 | */ 32 | public static void main(String[] argv) throws UnknownHostException, MongoException, UnsupportedEncodingException { 33 | MongoClient mongo = null; 34 | 35 | try { 36 | // Default: localhost:27017 37 | mongo = new MongoClient(); 38 | 39 | // Sharding: mongos server 40 | // mongo = new MongoClient("mongos-1", 4711); 41 | 42 | // Replica set 43 | // mongo = new MongoClient( 44 | // s new MongoClientURI("mongodb://localhost:27001,localhost:27002,localhost:27003/replicaSet=demo-dev") 45 | // ); 46 | 47 | // use database "test" 48 | MongoDatabase db = mongo.getDatabase("test"); 49 | 50 | // get collection names 51 | MongoIterable colls = db.listCollectionNames(); 52 | for (String s : colls) { 53 | println(s); 54 | } 55 | 56 | // use collection "foo" 57 | MongoCollection collection = db.getCollection("foo"); 58 | 59 | insert(collection); 60 | find(collection); 61 | 62 | // remove(collection); 63 | // bsonize(); 64 | } finally { 65 | if (mongo != null) { 66 | mongo.close(); 67 | } 68 | } 69 | } 70 | 71 | private static void remove(MongoCollection collection) { 72 | // alle Dokuemente mit {i: 42} 73 | Bson criteria = new BasicDBObject("i", 42); 74 | collection.deleteOne(criteria); 75 | 76 | // alle Dokumente 77 | collection.deleteMany( new BasicDBObject() ); 78 | 79 | // schneller: 80 | collection.drop(); 81 | } 82 | 83 | 84 | private static void insert(MongoCollection collection) { 85 | // Document speichern 86 | Document doc = new Document(); 87 | doc.put("date", new Date()); 88 | doc.put("i", 42); 89 | 90 | collection.insertOne(doc); 91 | } 92 | 93 | private static void find(MongoCollection collection) { 94 | FindIterable cursor; 95 | 96 | // alle Dokumente 97 | cursor = collection.find(); 98 | 99 | for ( Document document: cursor ) { 100 | println(document); 101 | } 102 | } 103 | 104 | private static void find(MongoCollection collection, Bson query) { 105 | FindIterable cursor; 106 | 107 | // alle Dokumente 108 | cursor = collection.find(); 109 | 110 | // Dokumente mit {i: 42} 111 | cursor = collection.find( query ); 112 | 113 | for ( Document document: cursor ) { 114 | println(document); 115 | } 116 | } 117 | 118 | 119 | private static void bsonize() throws UnsupportedEncodingException { 120 | final String key = "hello"; 121 | final String value = "MongoDB"; 122 | 123 | bsonize( new BasicDBObject(key, value) ); 124 | println( toString(key.getBytes("UTF-8")) ); 125 | println( toString(value.getBytes("UTF-8")) ); 126 | } 127 | 128 | 129 | private static void bsonize(DBObject doc) { 130 | final byte[] buff = BSON.encode(doc); 131 | 132 | println( toString(buff) ); 133 | } 134 | 135 | private static String toString(byte[] buff) { 136 | final StringBuilder sb = new StringBuilder(); 137 | 138 | for (int i = 0; i < buff.length; i++) { 139 | sb.append("\\x"); 140 | 141 | String hex = Integer.toHexString(buff[i]); 142 | if ( hex.length() < 2 ) { 143 | sb.append("0"); 144 | } 145 | 146 | sb.append(hex); 147 | ; 148 | } 149 | 150 | return sb.toString(); 151 | } 152 | 153 | private static final void println(Object o) { 154 | System.out.println(o); 155 | } 156 | 157 | } 158 | -------------------------------------------------------------------------------- /springdata-mongodb/src/test/java/mongodb/geo/LocationRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package mongodb.geo; 2 | 3 | import static org.hamcrest.CoreMatchers.is; 4 | import static org.hamcrest.CoreMatchers.notNullValue; 5 | import static org.junit.Assert.assertThat; 6 | 7 | import java.util.List; 8 | 9 | import org.junit.After; 10 | import org.junit.Before; 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.data.geo.Box; 15 | import org.springframework.data.geo.Circle; 16 | import org.springframework.data.geo.Distance; 17 | import org.springframework.data.geo.Metrics; 18 | import org.springframework.data.geo.Point; 19 | import org.springframework.data.mongodb.core.MongoTemplate; 20 | import org.springframework.data.mongodb.core.index.GeoSpatialIndexType; 21 | import org.springframework.data.mongodb.core.index.GeospatialIndex; 22 | import org.springframework.test.context.ContextConfiguration; 23 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 24 | 25 | import mongodb.config.LocalhostMongoConfiguration; 26 | 27 | /** 28 | * Tests for Spring Data MongoDB - Geospatial queries. 29 | * 30 | * @author Tobias 31 | * Trelle 32 | */ 33 | @RunWith(SpringJUnit4ClassRunner.class) 34 | @ContextConfiguration(classes=LocalhostMongoConfiguration.class) 35 | public class LocationRepositoryTest { 36 | 37 | private static final Point DUS = new Point( 6.810036, 51.224088 ); 38 | 39 | @Autowired 40 | private MongoTemplate template; 41 | 42 | @Autowired 43 | LocationRepository repo; 44 | 45 | @Before public void setUp() { 46 | template 47 | .indexOps(Location.class) 48 | .ensureIndex(new GeospatialIndex("position").typed(GeoSpatialIndexType.GEO_2DSPHERE)); 49 | 50 | // prepare data 51 | repo.save( new Location("A", 0.001, -0.002) ); 52 | repo.save( new Location("B", 1, 1) ); 53 | repo.save( new Location("C", 0.5, 0.5) ); 54 | repo.save( new Location("D", -0.5, -0.5) ); 55 | 56 | repo.save( new Location("Berlin", 13.405838, 52.531261 )); 57 | repo.save( new Location("Cologne", 6.921272, 50.960157 )); 58 | repo.save( new Location("Düsseldorf", 6.810036, 51.224088 ) ); 59 | } 60 | 61 | @Test public void shouldFindSelf() { 62 | // when 63 | List locations = repo.findByPositionNear(DUS , new Distance(1, Metrics.KILOMETERS) ); 64 | 65 | // then 66 | assertLocations(locations, "Düsseldorf"); 67 | } 68 | 69 | @Test public void shouldFindCologne() { 70 | // when 71 | List locations = repo.findByPositionNear(DUS , new Distance(70, Metrics.KILOMETERS) ); 72 | 73 | // then 74 | assertLocations(locations, "Düsseldorf", "Cologne"); 75 | } 76 | 77 | @Test public void shouldFindCologneAndBerlin() { 78 | // when 79 | List locations = repo.findByPositionNear(DUS , new Distance(350, Metrics.MILES) ); 80 | 81 | // then 82 | assertLocations(locations, "Düsseldorf", "Cologne", "Berlin"); 83 | } 84 | 85 | @Test public void shouldFindAll() { 86 | // when 87 | List locations = repo.findAll(); 88 | 89 | // then 90 | assertLocations(locations, "A", "B", "C", "D", "Berlin", "Cologne", "Düsseldorf"); 91 | } 92 | 93 | @Test public void shouldFindAroundOrigin() { 94 | // when 95 | List locations = repo.findByPositionWithin(new Circle(0, 0, 0.75)); 96 | 97 | // then 98 | assertLocations(locations, "A", "C", "D"); 99 | } 100 | 101 | @Test public void shouldFindWithinBox() { 102 | // when 103 | List locations = repo.findByPositionWithin(new Box(new Point( 104 | 0.25, 0.25), new Point(1, 1))); 105 | 106 | // then 107 | assertLocations(locations, "B", "C"); 108 | } 109 | 110 | @After 111 | public void tearDown() { 112 | //repo.deleteAll(); 113 | } 114 | 115 | private static void assertLocations(List locations, String... ids) { 116 | assertThat( locations, notNullValue() ); 117 | out("-----------------------------"); 118 | for (Location l : locations) { 119 | out(l); 120 | } 121 | assertThat("Mismatch location count", ids.length, is(locations.size())); 122 | for (String id : ids) { 123 | assertThat("Location " + id + " not found", 124 | locations.contains(new Location(id, 0, 0)), is(true)); 125 | } 126 | } 127 | 128 | private static void out(Object o) { 129 | System.out.println(o); 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /spring-boot-mongo/mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | set MAVEN_CMD_LINE_ARGS=%* 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | 121 | set WRAPPER_JAR="".\.mvn\wrapper\maven-wrapper.jar"" 122 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 123 | 124 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CMD_LINE_ARGS% 125 | if ERRORLEVEL 1 goto error 126 | goto end 127 | 128 | :error 129 | set ERROR_CODE=1 130 | 131 | :end 132 | @endlocal & set ERROR_CODE=%ERROR_CODE% 133 | 134 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 135 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 136 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 137 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 138 | :skipRcPost 139 | 140 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 141 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 142 | 143 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 144 | 145 | exit /B %ERROR_CODE% -------------------------------------------------------------------------------- /spring-boot-mongo/mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # 58 | # Look for the Apple JDKs first to preserve the existing behaviour, and then look 59 | # for the new JDKs provided by Oracle. 60 | # 61 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then 62 | # 63 | # Apple JDKs 64 | # 65 | export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home 66 | fi 67 | 68 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then 69 | # 70 | # Apple JDKs 71 | # 72 | export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home 73 | fi 74 | 75 | if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then 76 | # 77 | # Oracle JDKs 78 | # 79 | export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home 80 | fi 81 | 82 | if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then 83 | # 84 | # Apple JDKs 85 | # 86 | export JAVA_HOME=`/usr/libexec/java_home` 87 | fi 88 | ;; 89 | esac 90 | 91 | if [ -z "$JAVA_HOME" ] ; then 92 | if [ -r /etc/gentoo-release ] ; then 93 | JAVA_HOME=`java-config --jre-home` 94 | fi 95 | fi 96 | 97 | if [ -z "$M2_HOME" ] ; then 98 | ## resolve links - $0 may be a link to maven's home 99 | PRG="$0" 100 | 101 | # need this for relative symlinks 102 | while [ -h "$PRG" ] ; do 103 | ls=`ls -ld "$PRG"` 104 | link=`expr "$ls" : '.*-> \(.*\)$'` 105 | if expr "$link" : '/.*' > /dev/null; then 106 | PRG="$link" 107 | else 108 | PRG="`dirname "$PRG"`/$link" 109 | fi 110 | done 111 | 112 | saveddir=`pwd` 113 | 114 | M2_HOME=`dirname "$PRG"`/.. 115 | 116 | # make it fully qualified 117 | M2_HOME=`cd "$M2_HOME" && pwd` 118 | 119 | cd "$saveddir" 120 | # echo Using m2 at $M2_HOME 121 | fi 122 | 123 | # For Cygwin, ensure paths are in UNIX format before anything is touched 124 | if $cygwin ; then 125 | [ -n "$M2_HOME" ] && 126 | M2_HOME=`cygpath --unix "$M2_HOME"` 127 | [ -n "$JAVA_HOME" ] && 128 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 129 | [ -n "$CLASSPATH" ] && 130 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 131 | fi 132 | 133 | # For Migwn, ensure paths are in UNIX format before anything is touched 134 | if $mingw ; then 135 | [ -n "$M2_HOME" ] && 136 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 137 | [ -n "$JAVA_HOME" ] && 138 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 139 | # TODO classpath? 140 | fi 141 | 142 | if [ -z "$JAVA_HOME" ]; then 143 | javaExecutable="`which javac`" 144 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 145 | # readlink(1) is not available as standard on Solaris 10. 146 | readLink=`which readlink` 147 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 148 | if $darwin ; then 149 | javaHome="`dirname \"$javaExecutable\"`" 150 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 151 | else 152 | javaExecutable="`readlink -f \"$javaExecutable\"`" 153 | fi 154 | javaHome="`dirname \"$javaExecutable\"`" 155 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 156 | JAVA_HOME="$javaHome" 157 | export JAVA_HOME 158 | fi 159 | fi 160 | fi 161 | 162 | if [ -z "$JAVACMD" ] ; then 163 | if [ -n "$JAVA_HOME" ] ; then 164 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 165 | # IBM's JDK on AIX uses strange locations for the executables 166 | JAVACMD="$JAVA_HOME/jre/sh/java" 167 | else 168 | JAVACMD="$JAVA_HOME/bin/java" 169 | fi 170 | else 171 | JAVACMD="`which java`" 172 | fi 173 | fi 174 | 175 | if [ ! -x "$JAVACMD" ] ; then 176 | echo "Error: JAVA_HOME is not defined correctly." >&2 177 | echo " We cannot execute $JAVACMD" >&2 178 | exit 1 179 | fi 180 | 181 | if [ -z "$JAVA_HOME" ] ; then 182 | echo "Warning: JAVA_HOME environment variable is not set." 183 | fi 184 | 185 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 186 | 187 | # For Cygwin, switch paths to Windows format before running java 188 | if $cygwin; then 189 | [ -n "$M2_HOME" ] && 190 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 191 | [ -n "$JAVA_HOME" ] && 192 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 193 | [ -n "$CLASSPATH" ] && 194 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 195 | fi 196 | 197 | # traverses directory structure from process work directory to filesystem root 198 | # first directory with .mvn subdirectory is considered project base directory 199 | find_maven_basedir() { 200 | local basedir=$(pwd) 201 | local wdir=$(pwd) 202 | while [ "$wdir" != '/' ] ; do 203 | if [ -d "$wdir"/.mvn ] ; then 204 | basedir=$wdir 205 | break 206 | fi 207 | wdir=$(cd "$wdir/.."; pwd) 208 | done 209 | echo "${basedir}" 210 | } 211 | 212 | # concatenates all lines of a file 213 | concat_lines() { 214 | if [ -f "$1" ]; then 215 | echo "$(tr -s '\n' ' ' < "$1")" 216 | fi 217 | } 218 | 219 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)} 220 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 221 | 222 | # Provide a "standardized" way to retrieve the CLI args that will 223 | # work with both Windows and non-Windows executions. 224 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 225 | export MAVEN_CMD_LINE_ARGS 226 | 227 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 228 | 229 | exec "$JAVACMD" \ 230 | $MAVEN_OPTS \ 231 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 232 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 233 | ${WRAPPER_LAUNCHER} "$@" 234 | --------------------------------------------------------------------------------