DB4O查询问题

时间:2015-01-03 12:09:08

标签: db4o

我有下一个实体继续使用DB4O,它完全保存在db中。

public class Message {

    private String userFrom;
    private String userTo;
    private String message;
    private Date received;
    private Boolean readed;

    ...

}

我在尝试通过一个查询恢复持久化对象时遇到了问题。

使用SQL可以看到它。

SELECT * FROM Message WHERE readed = 0 
AND ((userFrom = 'asd' AND userTo = 'qwe') OR (userFrom = 'qwe' AND userTo = 'asd'))
ORDER BY received DESC

我当前错误的DB4O查询可能如下所示。

Query query =  super.getQuery();
query.constrain(Message.class);

Query readedQuery = query.descend("readed");
Query receivedQuery = query.descend("received");
Query userToQuery = query.descend("userTo");
Query userFromQuery = query.descend("userFrom");

receivedQuery.orderDescending()
    .constrain(readedQuery.constrain(Boolean.FALSE))
        .and(userToQuery.constrain("asd")
                .and(userFromQuery.constrain("qwe"))
            .or(userToQuery.constrain("qwe")
                .and(userFromQuery.constrain("asd"))));

return query.execute();

1 个答案:

答案 0 :(得分:0)

解决方案:

receivedQuery.sortBy(new Comparator<Message>() {

    @Override
    public int compare(Message message1, Message message2) {

        return message1.getReceived().after(message2.getReceived()) ? 1 : -1;
    }
}).readedQuery.constrain(Boolean.FALSE)
    .and(userToQuery.constrain("asd")
        .and(userFromQuery.constrain("qwe"))
    .or(userToQuery.constrain("qwe")
        .and(userFromQuery.constrain("asd"))));