Skip to content

Instantly share code, notes, and snippets.

@Promichel
Created November 16, 2011 19:36
Show Gist options
  • Save Promichel/1371092 to your computer and use it in GitHub Desktop.
Save Promichel/1371092 to your computer and use it in GitHub Desktop.
Mongo with Spring
public class Account {
public enum Type {
SAVINGS, CHECKING
}
private String id;
private String accountNumber;
private Account.Type accountType;
private Double balance;
public Account(){
}
public Account(String accountNumber, Type accountType, Double balance) {
super();
this.accountNumber = accountNumber;
this.accountType = accountType;
this.balance = balance;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public Account.Type getAccountType() {
return accountType;
}
public void setAccountType(Account.Type accountType) {
this.accountType = accountType;
}
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account [id=" + id + ", accountNumber=" + accountNumber
+ ", accountType=" + accountType + ", balance=" + balance + "]";
}
}
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.examples.hello.domain.Account;
import org.springframework.data.mongodb.examples.hello.domain.Person;
import org.springframework.stereotype.Repository;
@Repository
public class HelloMongo {
@Autowired
MongoOperations mongoOperations;
public void run() {
if (mongoOperations.collectionExists(Person.class)) {
mongoOperations.dropCollection(Person.class);
}
mongoOperations.createCollection(Person.class);
Person p = new Person("John", 39);
Account a = new Account("1234-59873-893-1", Account.Type.SAVINGS, 123.45D);
p.getAccounts().add(a);
mongoOperations.insert(p);
List<Person> results = mongoOperations.findAll(Person.class);
System.out.println("Results: " + results);
}
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Person {
@Id
private String id;
private String name;
private int age;
private List<Account> accounts = new ArrayList<Account>();
public Person() {
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Account> getAccounts() {
return accounts;
}
public void addAccount(Account account) {
this.accounts.add(account);
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age
+ ", accounts=" + accounts + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment