I have an entity class:
@Document(collection = "messages")
public class Message {
@Id
private String id;
}
And I'm using QueryDsl for generating Mongo queries. Querydsl generates necessary QMessage class:
@Generated("com.querydsl.codegen.EntitySerializer")
public class QMessage extends EntityPathBase<Message> {
private static final PathInits INITS = PathInits.DIRECT2;
public static final QMessage message = new QMessage("message");
public final StringPath id = createString("id");
}
with id field as a StringPath. Then I'm trying to query messages by id using predicate with:
QMessage message = QMessage.message;
BooleanExpression predicate = message.id.goe("someId").and(message.id.loe("someId"));
and use findAll(predicate) method in QuerydslPredicateExecutor. And it does create a correct query:
find "messages", filter { $and: [ { _id: { $gte: "someId" } }, { _id: { $lte: "someId" } } ] }
But the problem is that mongo cannot find anything if you query by id as a String. Because when inserting this document, mongo creates an ObjectId on this field, so the correct query would be:
find "messages", filter { $and: [ { _id: { $gte: ObjectId('someId') } }, { _id: { $lte: ObjectId('someId') } } ] }
So I've come up with manual creation of necessary path like this:
Path<Message> message = Expressions.path(Message.class, "message");
Path<ObjectId> idField = Expressions.path(ObjectId.class, message, "id");
Constant<ObjectId> objectIdGT = (Constant<ObjectId>) Expressions.constant(new ObjectId("someId"));
Constant<ObjectId> objectIdLT = (Constant<ObjectId>) Expressions.constant(new ObjectId("someId"));
BooleanExpression predicate = Expressions.predicate(Ops.GOE, idField, objectIdGT).and(Expressions.predicate(Ops.LOE, idField, objectIdLT));
And it works, but I would like to have this path generated automatically by QueryDsl with all other paths. Is there any annotation\configuration to customize code generation?