|
|
|
@ -230,6 +230,8 @@ public class GeoSpatialAppConfig extends AbstractMongoConfiguration { |
|
|
|
<!-- Default bean name is 'mongo' --> |
|
|
|
<!-- Default bean name is 'mongo' --> |
|
|
|
<mongo:mongo host="localhost" port="27017"/> |
|
|
|
<mongo:mongo host="localhost" port="27017"/> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<mongo:db-factory dbname="database" mongo-ref="mongo"/> |
|
|
|
|
|
|
|
|
|
|
|
<!-- by default look for a Mongo object named 'mongo' - default name used for the converter is 'mappingConverter' --> |
|
|
|
<!-- by default look for a Mongo object named 'mongo' - default name used for the converter is 'mappingConverter' --> |
|
|
|
<mongo:mapping-converter base-package="com.bigbank.domain"> |
|
|
|
<mongo:mapping-converter base-package="com.bigbank.domain"> |
|
|
|
<mongo:custom-converters> |
|
|
|
<mongo:custom-converters> |
|
|
|
@ -244,9 +246,7 @@ public class GeoSpatialAppConfig extends AbstractMongoConfiguration { |
|
|
|
|
|
|
|
|
|
|
|
<!-- set the mapping converter to be used by the MongoTemplate --> |
|
|
|
<!-- set the mapping converter to be used by the MongoTemplate --> |
|
|
|
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> |
|
|
|
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> |
|
|
|
<constructor-arg name="mongo" ref="mongo" /> |
|
|
|
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> |
|
|
|
<constructor-arg name="databaseName" value="test" /> |
|
|
|
|
|
|
|
<constructor-arg name="defaultCollectionName" value="myCollection" /> |
|
|
|
|
|
|
|
<constructor-arg name="mongoConverter" ref="mappingConverter"/> |
|
|
|
<constructor-arg name="mongoConverter" ref="mappingConverter"/> |
|
|
|
</bean> |
|
|
|
</bean> |
|
|
|
|
|
|
|
|
|
|
|
@ -382,52 +382,51 @@ public class Person { |
|
|
|
|
|
|
|
|
|
|
|
<programlisting language="java">@Document |
|
|
|
<programlisting language="java">@Document |
|
|
|
@CompoundIndexes({ |
|
|
|
@CompoundIndexes({ |
|
|
|
@CompoundIndex(name = "age_idx", def = "{'lastName': 1, 'age': -1}") |
|
|
|
@CompoundIndex(name = "age_idx", def = "{'lastName': 1, 'age': -1}") |
|
|
|
}) |
|
|
|
}) |
|
|
|
public class Person<T extends Address> { |
|
|
|
public class Person<T extends Address> { |
|
|
|
|
|
|
|
|
|
|
|
@Id |
|
|
|
@Id |
|
|
|
private String id; |
|
|
|
private String id; |
|
|
|
@Indexed(unique = true) |
|
|
|
@Indexed(unique = true) |
|
|
|
private Integer ssn; |
|
|
|
private Integer ssn; |
|
|
|
private String firstName; |
|
|
|
private String firstName; |
|
|
|
@Indexed |
|
|
|
@Indexed |
|
|
|
private String lastName; |
|
|
|
private String lastName; |
|
|
|
private Integer age; |
|
|
|
private Integer age; |
|
|
|
@Transient |
|
|
|
@Transient |
|
|
|
private Integer accountTotal; |
|
|
|
private Integer accountTotal; |
|
|
|
@DBRef |
|
|
|
@DBRef |
|
|
|
private List<Account> accounts; |
|
|
|
private List<Account> accounts; |
|
|
|
private T address; |
|
|
|
private T address; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Person(Integer ssn) { |
|
|
|
public Person(Integer ssn) { |
|
|
|
this.ssn = ssn; |
|
|
|
this.ssn = ssn; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@PersistenceConstructor |
|
|
|
@PersistenceConstructor |
|
|
|
public Person(Integer ssn, String firstName, String lastName, Integer age, T address) { |
|
|
|
public Person(Integer ssn, String firstName, String lastName, Integer age, T address) { |
|
|
|
this.ssn = ssn; |
|
|
|
this.ssn = ssn; |
|
|
|
this.firstName = firstName; |
|
|
|
this.firstName = firstName; |
|
|
|
this.lastName = lastName; |
|
|
|
this.lastName = lastName; |
|
|
|
this.age = age; |
|
|
|
this.age = age; |
|
|
|
this.address = address; |
|
|
|
this.address = address; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getId() { |
|
|
|
public String getId() { |
|
|
|
return id; |
|
|
|
return id; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// no setter for Id. (getter is only exposed for some unit testing) |
|
|
|
// no setter for Id. (getter is only exposed for some unit testing) |
|
|
|
|
|
|
|
|
|
|
|
public Integer getSsn() { |
|
|
|
public Integer getSsn() { |
|
|
|
return ssn; |
|
|
|
return ssn; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// other getters/setters ommitted |
|
|
|
// other getters/setters ommitted |
|
|
|
|
|
|
|
</programlisting> |
|
|
|
}</programlisting> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<para></para> |
|
|
|
<para></para> |
|
|
|
</section> |
|
|
|
</section> |
|
|
|
@ -436,7 +435,7 @@ public class Person<T extends Address> { |
|
|
|
<title>Compound Indexes</title> |
|
|
|
<title>Compound Indexes</title> |
|
|
|
|
|
|
|
|
|
|
|
<para>Compound indexes are also supported. They are defined at the class |
|
|
|
<para>Compound indexes are also supported. They are defined at the class |
|
|
|
level, rather than on indidvidual properties. </para> |
|
|
|
level, rather than on indidvidual properties.</para> |
|
|
|
|
|
|
|
|
|
|
|
<note> |
|
|
|
<note> |
|
|
|
<para>Compound indexes are very important to improve the performance |
|
|
|
<para>Compound indexes are very important to improve the performance |
|
|
|
@ -540,7 +539,7 @@ public class Person { |
|
|
|
mapping of all Java types to DBObjects. However, sometimes you may want |
|
|
|
mapping of all Java types to DBObjects. However, sometimes you may want |
|
|
|
the <interfacename>MongoConverter</interfacename>'s do most of the work |
|
|
|
the <interfacename>MongoConverter</interfacename>'s do most of the work |
|
|
|
but allow you to selectivly handle the conversion for a particular type |
|
|
|
but allow you to selectivly handle the conversion for a particular type |
|
|
|
or to optimize performance. </para> |
|
|
|
or to optimize performance.</para> |
|
|
|
|
|
|
|
|
|
|
|
<para>To selectivly handle the conversion yourself, register one or more |
|
|
|
<para>To selectivly handle the conversion yourself, register one or more |
|
|
|
one or more |
|
|
|
one or more |
|
|
|
@ -563,18 +562,18 @@ public class Person { |
|
|
|
<classname>AbstractMongoConfiguration</classname> can be overriden to |
|
|
|
<classname>AbstractMongoConfiguration</classname> can be overriden to |
|
|
|
configure a MappingMongoConverter. The examples <link |
|
|
|
configure a MappingMongoConverter. The examples <link |
|
|
|
linkend="???">here</link> at the begining of this chapter show how to |
|
|
|
linkend="???">here</link> at the begining of this chapter show how to |
|
|
|
perform the configuration using Java and XML. </para> |
|
|
|
perform the configuration using Java and XML.</para> |
|
|
|
|
|
|
|
|
|
|
|
<para>Below is an example of a Spring Converter implementation that |
|
|
|
<para>Below is an example of a Spring Converter implementation that |
|
|
|
converts from a DBObject to a Person POJO.</para> |
|
|
|
converts from a DBObject to a Person POJO.</para> |
|
|
|
|
|
|
|
|
|
|
|
<programlisting language="java">public class PersonReadConverter implements Converter<DBObject, Person> { |
|
|
|
<programlisting language="java">public class PersonReadConverter implements Converter<DBObject, Person> { |
|
|
|
|
|
|
|
|
|
|
|
public Person convert(DBObject source) { |
|
|
|
public Person convert(DBObject source) { |
|
|
|
Person p = new Person((ObjectId) source.get("_id"), (String) source.get("name")); |
|
|
|
Person p = new Person((ObjectId) source.get("_id"), (String) source.get("name")); |
|
|
|
p.setAge((Integer) source.get("age")); |
|
|
|
p.setAge((Integer) source.get("age")); |
|
|
|
return p; |
|
|
|
return p; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}</programlisting> |
|
|
|
}</programlisting> |
|
|
|
|
|
|
|
|
|
|
|
@ -583,13 +582,13 @@ public class Person { |
|
|
|
|
|
|
|
|
|
|
|
<programlisting language="java">public class PersonWriteConverter implements Converter<Person, DBObject> { |
|
|
|
<programlisting language="java">public class PersonWriteConverter implements Converter<Person, DBObject> { |
|
|
|
|
|
|
|
|
|
|
|
public DBObject convert(Person source) { |
|
|
|
public DBObject convert(Person source) { |
|
|
|
DBObject dbo = new BasicDBObject(); |
|
|
|
DBObject dbo = new BasicDBObject(); |
|
|
|
dbo.put("_id", source.getId()); |
|
|
|
dbo.put("_id", source.getId()); |
|
|
|
dbo.put("name", source.getFirstName()); |
|
|
|
dbo.put("name", source.getFirstName()); |
|
|
|
dbo.put("age", source.getAge()); |
|
|
|
dbo.put("age", source.getAge()); |
|
|
|
return dbo; |
|
|
|
return dbo; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}</programlisting> |
|
|
|
}</programlisting> |
|
|
|
|
|
|
|
|
|
|
|
|