Skip to content

MongoTemplate uses the name of the POJO object instead the DB field in findById [DATAMONGO-1426] #2337

@spring-projects-issues

Description

@spring-projects-issues

Jose Luis opened DATAMONGO-1426 and commented

Hi

In MongoTemplate findById it uses the POJO property name to build the query instead of the field name in the database.

public <T> T findById(Object id, Class<T> entityClass, String collectionName) {
		MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entityClass);
		MongoPersistentProperty idProperty = persistentEntity == null ? null : persistentEntity.getIdProperty();
		String idKey = idProperty == null ? ID_FIELD : idProperty.getName();
		return doFindOne(collectionName, new BasicDBObject(idKey, id), null, entityClass);
	}

if the @Id is pressent, then the query issues the field name in the POJO instead in the db side, for instance, if we'd have this class:

public class Customer
{
      @Id
      String customerId;
}

then the query of findOne would be { customerId : "12345" } instead of { _id : "12345"}

save is working correctly, and uses the _id field. Actually removing the @Id, makes it work correctly. (String idKey = idProperty == null ? ID_FIELD : idProperty.getName();).

so actually using idProperty.getFieldName() should fix it.

Also exists fail, a object recently created does not exists.


Affects: 1.9.1 (Hopper SR1)

0 votes, 5 watchers

Metadata

Metadata

Labels

in: mappingMapping and conversion infrastructuretype: bugA general bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions