25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 15<br />

<strong>Spring</strong> Data Within Your <strong>Spring</strong><br />

Application<br />

So far your <strong>Spring</strong> application has some basic features, enough to demonstrate the capabilities of the <strong>Spring</strong><br />

<strong>Framework</strong>. But imagine for a moment that you need to add more attributes to the model, maybe something like<br />

tags or keywords to the documents. It should be easy enough to add the attributes and modify the DAOs and/or SQL<br />

statements, right? For easy attributes, maybe it is, but what happens if you require more complicated relationships or<br />

more attributes every week? It would be a tedious job to change it all the time.<br />

Fortunately, there are NoSQL databases, which provide features like adding more attributes to a persistence<br />

object without adding or modifying schemas. In this chapter, you are going to add a NoSQL database: MongoDB.<br />

Using NoSQL Databases<br />

MongoDB is a document-oriented database system that makes the integration of data very easy; it persists objects<br />

using a JSON-like style and creates collections of this data. Let’s add this new feature to your <strong>Spring</strong> application.<br />

The <strong>Spring</strong> <strong>Framework</strong> provides a module for NoSQL databases: <strong>Spring</strong> Data. Before you continue, you need<br />

to make sure you have installed the MongoDB server on your computer. (For more information about how to install<br />

MongoDB, see Appendix A.) See Listing 15-1 for the modification of the Document class.<br />

Listing 15-1. Document.java<br />

package com.apress.isf.java.model;<br />

import java.util.Date;<br />

import org.springframework.data.annotation.Id;<br />

import org.springframework.data.mongodb.core.mapping.DBRef;<br />

@org.springframework.data.mongodb.core.mapping.Document(collection="docs")<br />

public class Document {<br />

@Id<br />

private String documentId;<br />

private String name;<br />

@DBRef<br />

private Type type;<br />

private String location;<br />

private String description;<br />

203

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!