/
QuickTour.java
143 lines (112 loc) · 4.43 KB
/
QuickTour.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package org.mongodb.morphia.example;
import com.mongodb.MongoClient;
import org.bson.types.ObjectId;
import org.junit.Assert;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Field;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Index;
import org.mongodb.morphia.annotations.Indexes;
import org.mongodb.morphia.annotations.Property;
import org.mongodb.morphia.annotations.Reference;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.mongodb.morphia.query.UpdateResults;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
/**
* This class is used in the Quick Tour documentation and is used to demonstrate various Morphia features.
*/
public final class QuickTour {
private QuickTour() {
}
public static void main(final String[] args) throws UnknownHostException {
final Morphia morphia = new Morphia();
// tell morphia where to find your classes
// can be called multiple times with different packages or classes
morphia.mapPackage("org.mongodb.morphia.example");
// create the Datastore connecting to the database running on the default port on the local host
final Datastore datastore = morphia.createDatastore(new MongoClient(), "morphia_example");
datastore.getDB().dropDatabase();
datastore.ensureIndexes();
final Employee elmer = new Employee("Elmer Fudd", 50000.0);
datastore.save(elmer);
final Employee daffy = new Employee("Daffy Duck", 40000.0);
datastore.save(daffy);
final Employee pepe = new Employee("Pepé Le Pew", 25000.0);
datastore.save(pepe);
elmer.getDirectReports().add(daffy);
elmer.getDirectReports().add(pepe);
datastore.save(elmer);
Query<Employee> query = datastore.find(Employee.class);
final List<Employee> employees = query.asList();
Assert.assertEquals(3, employees.size());
List<Employee> underpaid = datastore.find(Employee.class)
.filter("salary <=", 30000)
.asList();
Assert.assertEquals(1, underpaid.size());
underpaid = datastore.find(Employee.class)
.field("salary").lessThanOrEq(30000)
.asList();
Assert.assertEquals(1, underpaid.size());
final Query<Employee> underPaidQuery = datastore.find(Employee.class)
.filter("salary <=", 30000);
final UpdateOperations<Employee> updateOperations = datastore.createUpdateOperations(Employee.class)
.inc("salary", 10000);
final UpdateResults results = datastore.update(underPaidQuery, updateOperations);
Assert.assertEquals(1, results.getUpdatedCount());
final Query<Employee> overPaidQuery = datastore.find(Employee.class)
.filter("salary >", 100000);
datastore.delete(overPaidQuery);
}
}
@Entity("employees")
@Indexes(@Index(value = "salary", fields = @Field("salary")))
class Employee {
@Id
private ObjectId id;
private String name;
private Integer age;
@Reference
private Employee manager;
@Reference
private List<Employee> directReports = new ArrayList<Employee>();
@Property("wage")
private Double salary;
public Employee() {
}
public Employee(final String name, final Double salary) {
this.name = name;
this.salary = salary;
}
public List<Employee> getDirectReports() {
return directReports;
}
public void setDirectReports(final List<Employee> directReports) {
this.directReports = directReports;
}
public ObjectId getId() {
return id;
}
public Employee getManager() {
return manager;
}
public void setManager(final Employee manager) {
this.manager = manager;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(final Double salary) {
this.salary = salary;
}
}