25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 15 ■ <strong>Spring</strong> Data Within Your <strong>Spring</strong> Application<br />

public Date getModified() {<br />

return modified;<br />

}<br />

public void setModified(Date modified) {<br />

this.modified = modified;<br />

}<br />

}<br />

public String toString(){<br />

StringBuilder builder = new StringBuilder("Document(");<br />

builder.append("id: ");<br />

builder.append(documentId);<br />

builder.append(", name: ");<br />

builder.append(name);<br />

builder.append(", type: ");<br />

builder.append(type);<br />

builder.append(", location: ");<br />

builder.append(location);<br />

builder.append(", created: ");<br />

builder.append(created);<br />

builder.append(", modified: ");<br />

builder.append(modified);<br />

builder.append(")");<br />

return builder.toString();<br />

}<br />

Listing 15-1 shows you the modified model, the Document class. It uses three annotations. The @Id that will help<br />

to identify the object saved, the @DBRef that will add a reference to another collection (remember that Mongo uses<br />

collections for saving objects), and finally the @Document annotation that identifies a domain object to be persisted<br />

into a collection.<br />

Listing 15-2 shows you another domain model, the Type class.<br />

Listing 15-2. Type.java<br />

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

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

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

public class Type {<br />

@Id<br />

private String typeId;<br />

private String name;<br />

private String desc;<br />

private String extension;<br />

//Constructors Omitted<br />

public String getTypeId() {<br />

return typeId;<br />

}<br />

205

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

Saved successfully!

Ooh no, something went wrong!