Browse Source

Merge branch 'master' of github.com:SpringSource/spring-data-document

* 'master' of github.com:SpringSource/spring-data-document:
  added a simple UpdateBuilder implementation
  README updates.
  README updates.
  README updates.
  README updates.
  README updates.
  README updates.
  README updates.
  README updates.
  README updates.
  Add initial README
  DATADOC-23 removed DocumentSource and its subclasses
pull/1/head
Oliver Gierke 15 years ago
parent
commit
69ca5e2a98
  1. 155
      README.md
  2. 8
      spring-data-couchdb/src/main/java/org/springframework/data/document/couchdb/CouchDocumentSource.java
  3. 6
      spring-data-couchdb/src/main/java/org/springframework/data/document/couchdb/CouchTemplate.java
  4. 29
      spring-data-document-core/src/main/java/org/springframework/data/document/DocumentSource.java
  5. 26
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/UpdateBuilder.java
  6. 47
      spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/UpdateBuilderTests.java

155
README.md

@ -0,0 +1,155 @@
Spring Data - Document
======================
The primary goal of the [Spring Data](http://www.springsource.org/spring-data) project is to make it easier to build Spring-powered applications that use new data access technologies such as non-relational databases, map-reduce frameworks, and cloud based data services.
As the name implies, the **Document** modules provides integration with document databases such as [MongoDB](http://www.mongodb.org/) and [CouchDB](http://couchdb.apache.org/).
Getting Help
------------
At this point your best bet is to look at the Look at the [JavaDocs](http://static.springsource.org/spring-data/data-document/docs/1.0.0.BUILD-SNAPSHOT/spring-data-mongodb/apidocs/) for MongoDB integration and corresponding and source code. For more detailed questions, use the [forum](http://forum.springsource.org/forumdisplay.php?f=80). If you are new to Spring as well as to Spring Data, look for information about [Spring projects](http://www.springsource.org/projects).
The [User Guide](http://static.springsource.org/spring-data/data-document/docs/1.0.0.BUILD-SNAPSHOT/reference/html/) (A work in progress).
Quick Start
-----------
## MongoDB
For those in a hurry:
* Download the jar through Maven:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-document</artifactId>
<version>1.0.0-BUILD-SNAPSHOT</version>
</dependency>
<repository>
<id>spring-maven-snapshot</id>
<snapshots><enabled>true</enabled></snapshots>
<name>Springframework Maven SNAPSHOT Repository</name>
<url>http://maven.springframework.org/snapshot</url>
</repository>
### MongoTemplate
MongoTemplate is the central support class for Mongo database operations. It provides
* Basic POJO mapping support to and from BSON
* Connection Affinity callback
* Exception translation into Spring's [technology agnostic DAO exception hierarchy](http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/dao.html#dao-exceptions).
Future plans are to support optional logging and/or exception throwing based on WriteResult return value, common map-reduce operations, GridFS operations. A simple API for partial document updates is also planned.
### Easy Data Repository generation
To simplify the creation of Data Repositories a generic Repository interface and default implementation is provided. Furthermore, Spring will automatically create a Repository implementation for you that adds implementations of finder methods you specify on an interface.
The Repository interface is
public interface Repository<T, ID extends Serializable> {
T save(T entity);
List<T> save(Iterable<? extends T> entities);
T findById(ID id);
boolean exists(ID id);
List<T> findAll();
Long count();
void delete(T entity);
void delete(Iterable<? extends T> entities);
void deleteAll();
}
The MongoRepository extends Repository and will in future add more Mongo specific methods.
public interface MongoRepository<T, ID extends Serializable> extends
Repository<T, ID> {
}
SimpleMongoRepository is the out of the box implementation of the MongoRepository you can use for basid CRUD operations.
To go beyond basic CRUD, extend the MongoRepository interface and supply your own finder methods that follow simple naming conventions such that they can be easily converted into queries.
For example, given a Person class with first and last name properties, a PersonRepository interface that can query for Person by last name and when the first name matches a regular expression is shown below
public interface PersonRepository extends MongoRepository<Person, Long> {
List<Person> findByLastname(String lastname);
List<Person> findByFirstnameLike(String firstname);
}
You can have Spring automatically generate the implemention as shown below
<bean id="template" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg>
<bean class="com.mongodb.Mongo">
<constructor-arg value="localhost" />
<constructor-arg value="27017" />
</bean>
</constructor-arg>
<constructor-arg value="database" />
<property name="defaultCollectionName" value="springdata" />
</bean>
<bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean">
<property name="template" ref="template" />
<property name="repositoryInterface" value="org.springframework.data.document.mongodb.repository.PersonRepository" />
</bean>
This will register an object in the container named PersonRepository. You can use it as shown below
@Service
public class MyService {
@Autowired
PersonRepository repository;
public void doWork() {
repository.deleteAll();
Person person = new Person();
person.setFirstname("Oliver");
person.setLastname("Gierke");
person = repository.save(person);
List<Person> lastNameResults = repository.findByLastname("Gierke");
List<Person> firstNameResults = repository.findByFirstnameLike("Oli*");
}
}
## CouchDB
TBD
Contributing to Spring Data
---------------------------
Here are some ways for you to get involved in the community:
* Get involved with the Spring community on the Spring Community Forums. Please help out on the [forum](http://forum.springsource.org/forumdisplay.php?f=80) by responding to questions and joining the debate.
* Create [JIRA](https://jira.springframework.org/browse/DATADOC) tickets for bugs and new features and comment and vote on the ones that you are interested in.
* Github is for social coding: if you want to write code, we encourage contributions through pull requests from [forks of this repository](http://help.github.com/forking/). If you want to contribute code this way, please reference a JIRA ticket as well covering the specific issue you are addressing.
* Watch for upcoming articles on Spring by [subscribing](http://www.springsource.org/node/feed) to springframework.org
Before we accept a non-trivial patch or pull request we will need you to sign the [contributor's agreement](https://support.springsource.com/spring_committer_signup). Signing the contributor's agreement does not grant anyone commit rights to the main repository, but it does mean that we can accept your contributions, and you will get an author credit if we do. Active contributors might be asked to join the core team, and given the ability to merge pull requests.

8
spring-data-couchdb/src/main/java/org/springframework/data/document/couchdb/CouchDocumentSource.java

@ -1,8 +0,0 @@
package org.springframework.data.document.couchdb;
import org.jcouchdb.document.BaseDocument;
import org.springframework.data.document.DocumentSource;
public interface CouchDocumentSource extends DocumentSource<BaseDocument> {
}

6
spring-data-couchdb/src/main/java/org/springframework/data/document/couchdb/CouchTemplate.java

@ -20,7 +20,6 @@ package org.springframework.data.document.couchdb;
import org.jcouchdb.db.Database; import org.jcouchdb.db.Database;
import org.jcouchdb.document.BaseDocument; import org.jcouchdb.document.BaseDocument;
import org.springframework.data.document.AbstractDocumentStoreTemplate; import org.springframework.data.document.AbstractDocumentStoreTemplate;
import org.springframework.data.document.DocumentSource;
public class CouchTemplate extends AbstractDocumentStoreTemplate<Database> { public class CouchTemplate extends AbstractDocumentStoreTemplate<Database> {
@ -40,9 +39,8 @@ import org.springframework.data.document.DocumentSource;
this.database = database; this.database = database;
} }
public void save(DocumentSource<BaseDocument> documentSource) { public void save(BaseDocument document) {
BaseDocument d = documentSource.getDocument(); getConnection().createDocument(document);
getConnection().createDocument(d);
} }
@Override @Override

29
spring-data-document-core/src/main/java/org/springframework/data/document/DocumentSource.java

@ -1,29 +0,0 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.document;
/**
* Class used to map a business object to an object providing the source data for a Document.
*
* @author Thomas Risberg
* @since 1.0
*/
public interface DocumentSource<D> {
D getDocument();
}

26
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoDocumentSource.java → spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/UpdateBuilder.java

@ -15,10 +15,32 @@
*/ */
package org.springframework.data.document.mongodb; package org.springframework.data.document.mongodb;
import org.springframework.data.document.DocumentSource; import java.util.Collections;
import java.util.LinkedHashMap;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; import com.mongodb.DBObject;
public interface MongoDocumentSource extends DocumentSource<DBObject> { public class UpdateBuilder {
private LinkedHashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
public UpdateBuilder set(String key, Object value) {
criteria.put("$set", Collections.singletonMap(key, value));
return this;
}
public UpdateBuilder inc(String key, long inc) {
criteria.put("$inc", Collections.singletonMap(key, inc));
return this;
}
public DBObject build() {
DBObject dbo = new BasicDBObject();
for (String k : criteria.keySet()) {
dbo.put(k, criteria.get(k));
}
return dbo;
}
} }

47
spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/UpdateBuilderTests.java

@ -0,0 +1,47 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.document.mongodb;
import org.junit.Assert;
import org.junit.Test;
public class UpdateBuilderTests {
@Test
public void testSetUpdate() {
UpdateBuilder ub = new UpdateBuilder()
.set("directory", "/Users/Test/Desktop");
Assert.assertEquals("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", ub.build().toString());
}
@Test
public void testIncUpdate() {
UpdateBuilder ub = new UpdateBuilder()
.inc("size", 1);
Assert.assertEquals("{ \"$inc\" : { \"size\" : 1}}", ub.build().toString());
}
@Test
public void testIncAndSetUpdate() {
UpdateBuilder ub = new UpdateBuilder()
.inc("size", 1)
.set("directory", "/Users/Test/Desktop");
Assert.assertEquals("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}",
ub.build().toString());
}
}
Loading…
Cancel
Save