|
16 | 16 |
|
17 | 17 | package org.springframework.data.document.mongodb;
|
18 | 18 |
|
| 19 | +import static org.springframework.data.document.mongodb.query.Criteria.whereId; |
| 20 | +import static org.springframework.data.document.mongodb.query.Query.query; |
| 21 | + |
19 | 22 | import java.beans.IntrospectionException;
|
20 | 23 | import java.beans.PropertyDescriptor;
|
21 | 24 | import java.lang.reflect.InvocationTargetException;
|
@@ -165,7 +168,14 @@ public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionN
|
165 | 168 | if (writeResultChecking != null) {
|
166 | 169 | this.writeResultChecking = writeResultChecking;
|
167 | 170 | }
|
168 |
| - setMongoConverter(mongoConverter == null ? new SimpleMongoConverter() : mongoConverter); |
| 171 | + if (mongoConverter == null) { |
| 172 | + SimpleMongoConverter smc = new SimpleMongoConverter(); |
| 173 | + smc.afterPropertiesSet(); |
| 174 | + setMongoConverter(smc); |
| 175 | + } else { |
| 176 | + setMongoConverter(mongoConverter); |
| 177 | + } |
| 178 | + //setMongoConverter(mongoConverter == null ? new SimpleMongoConverter() : mongoConverter); |
169 | 179 | }
|
170 | 180 |
|
171 | 181 | public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
@@ -808,33 +818,50 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
|
808 | 818 | * @see org.springframework.data.document.mongodb.MongoOperations#remove(com.mongodb.DBObject)
|
809 | 819 | */
|
810 | 820 | public void remove(Query query) {
|
811 |
| - remove(getRequiredDefaultCollectionName(), query); |
812 |
| - } |
813 |
| - |
| 821 | + remove(query, null); |
| 822 | + } |
| 823 | + |
| 824 | + public void remove(Object object) { |
| 825 | + Object idValue = this.getIdValue(object); |
| 826 | + remove(new Query(whereId().is(idValue)), object.getClass()); |
| 827 | + } |
| 828 | + |
| 829 | + public <T> void remove(Query query, Class<T> targetClass) { |
| 830 | + remove(getEntityCollection(targetClass), query, targetClass); |
| 831 | + } |
| 832 | + |
| 833 | + public <T> void remove(String collectionName, final Query query, Class<T> targetClass) { |
| 834 | + if (query == null) { |
| 835 | + throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null"); |
| 836 | + } |
| 837 | + final DBObject queryObject = query.getQueryObject(); |
| 838 | + if (targetClass == null) { |
| 839 | + substituteMappedIdIfNecessary(queryObject); |
| 840 | + } else { |
| 841 | + substituteMappedIdIfNecessary(queryObject, targetClass, this.mongoConverter); |
| 842 | + } |
| 843 | + if (LOGGER.isDebugEnabled()) { |
| 844 | + LOGGER.debug("remove using query: " + queryObject); |
| 845 | + } |
| 846 | + execute(collectionName, new CollectionCallback<Void>() { |
| 847 | + public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
| 848 | + WriteResult wr = null; |
| 849 | + if (writeConcern == null) { |
| 850 | + wr = collection.remove(queryObject); |
| 851 | + } else { |
| 852 | + wr = collection.remove(queryObject, writeConcern); |
| 853 | + } |
| 854 | + handleAnyWriteResultErrors(wr, queryObject, "remove"); |
| 855 | + return null; |
| 856 | + } |
| 857 | + }); |
| 858 | + } |
| 859 | + |
814 | 860 | /* (non-Javadoc)
|
815 | 861 | * @see org.springframework.data.document.mongodb.MongoOperations#remove(java.lang.String, com.mongodb.DBObject)
|
816 | 862 | */
|
817 | 863 | public void remove(String collectionName, final Query query) {
|
818 |
| - if (query == null) { |
819 |
| - throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null"); |
820 |
| - } |
821 |
| - final DBObject queryObject = query.getQueryObject(); |
822 |
| - substituteMappedIdIfNecessary(queryObject); |
823 |
| - if (LOGGER.isDebugEnabled()) { |
824 |
| - LOGGER.debug("remove using query: " + queryObject); |
825 |
| - } |
826 |
| - execute(collectionName, new CollectionCallback<Void>() { |
827 |
| - public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
828 |
| - WriteResult wr = null; |
829 |
| - if (writeConcern == null) { |
830 |
| - wr = collection.remove(queryObject); |
831 |
| - } else { |
832 |
| - wr = collection.remove(queryObject, writeConcern); |
833 |
| - } |
834 |
| - handleAnyWriteResultErrors(wr, queryObject, "remove"); |
835 |
| - return null; |
836 |
| - } |
837 |
| - }); |
| 864 | + remove(collectionName, query, null); |
838 | 865 | }
|
839 | 866 |
|
840 | 867 |
|
@@ -909,9 +936,6 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
|
909 | 936 | readerToUse = this.mongoConverter;
|
910 | 937 | }
|
911 | 938 | substituteMappedIdIfNecessary(query, targetClass, readerToUse);
|
912 |
| - if (LOGGER.isDebugEnabled()) { |
913 |
| - LOGGER.debug("findOne using query: " + query + " fields: " + fields + " for class: " + targetClass); |
914 |
| - } |
915 | 939 | return execute(new FindOneCallback(query, fields), new ReadDbObjectCallback<T>(readerToUse, targetClass),
|
916 | 940 | collectionName);
|
917 | 941 | }
|
@@ -1006,6 +1030,32 @@ protected <T> T doFindAndRemove(String collectionName, DBObject query, DBObject
|
1006 | 1030 | collectionName);
|
1007 | 1031 | }
|
1008 | 1032 |
|
| 1033 | + protected Object getIdValue(Object object) { |
| 1034 | + if (null != mappingContext) { |
| 1035 | + PersistentEntity<?> entity = mappingContext.getPersistentEntity(object.getClass()); |
| 1036 | + if (null != entity) { |
| 1037 | + PersistentProperty idProp = entity.getIdProperty(); |
| 1038 | + if (null != idProp) { |
| 1039 | + try { |
| 1040 | + return MappingBeanHelper.getProperty(object, idProp, Object.class, true); |
| 1041 | + } catch (IllegalAccessException e) { |
| 1042 | + throw new MappingException(e.getMessage(), e); |
| 1043 | + } catch (InvocationTargetException e) { |
| 1044 | + throw new MappingException(e.getMessage(), e); |
| 1045 | + } |
| 1046 | + } |
| 1047 | + } |
| 1048 | + } |
| 1049 | + |
| 1050 | + ConfigurablePropertyAccessor bw = PropertyAccessorFactory.forDirectFieldAccess(object); |
| 1051 | + MongoPropertyDescriptor idDescriptor = new MongoPropertyDescriptors(object.getClass()).getIdDescriptor(); |
| 1052 | + |
| 1053 | + if (idDescriptor == null) { |
| 1054 | + return null; |
| 1055 | + } |
| 1056 | + return bw.getPropertyValue(idDescriptor.getName()); |
| 1057 | + |
| 1058 | + } |
1009 | 1059 | /**
|
1010 | 1060 | * Populates the id property of the saved object, if it's not set already.
|
1011 | 1061 | *
|
@@ -1249,8 +1299,14 @@ public FindOneCallback(DBObject query, DBObject fields) {
|
1249 | 1299 |
|
1250 | 1300 | public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException {
|
1251 | 1301 | if (fields == null) {
|
| 1302 | + if (LOGGER.isDebugEnabled()) { |
| 1303 | + LOGGER.debug("findOne using query: " + query + " in db.collection: " + collection.getFullName()); |
| 1304 | + } |
1252 | 1305 | return collection.findOne(query);
|
1253 | 1306 | } else {
|
| 1307 | + if (LOGGER.isDebugEnabled()) { |
| 1308 | + LOGGER.debug("findOne using query: " + query + " fields: " + fields + " in db.collection: " + collection.getFullName()); |
| 1309 | + } |
1254 | 1310 | return collection.findOne(query, fields);
|
1255 | 1311 | }
|
1256 | 1312 | }
|
|
0 commit comments