Reputation: 3128
I have a domain class called User. It uses an object of MyUserId
as the Id
@Document(collection = "users")
public class User implements Serializable {
@Property
@Id
private MyUserId id;
@Version
private Integer version;
private String firstName;
// Setters, getters
}
The MyUserId
class:
public class MyUserId implements Serializable{
@Property
private String userId;
@Property
private String sampleId;
// setters, getters
}
Inside my Mongo, documents are getting stored as {_id:{userId:....., sampleId:....}, <more fields here>}
My userRepository is like this:
public interface UserRepository extends MongoRepository<User, MyUserId> {
@Query("{'_id': {$in: ?0}}")
List<User> findByUserIds(Collection<MyUserId> userIds);
}
When I'm querying my userRepository, The query is being fired as:
{_id: {$in: [ {_class:"com.sampleuser.MyUserId", userId:"....", sampleId:"...."}, {_class:"com.sampleuser.MyUserId", userId:"....", sampleId:"...."}]}}
It's obvious that it's adding the _class field while querying, but not while storing. Can someone throw some light at how to fix this? It's causing all my queries to fail. Thank you!
Upvotes: 0
Views: 1838
Reputation: 6736
There actually exists an issue when using @Query
whith complex id types. I'd suggest to use a custom repository implementation until DATAMONGO-1078 is resolved.
Within the custom implementation you could use MongoTemplate
to execute the query somehow like this
@Override
public List<User> findByUserIds(Collection<MyUserId> userIds) {
return template.find(query(where("id").in(userIds)), User.class);
}
Upvotes: 1