我使用此查询:
Query query = manager.createQuery("SELECT msg FROM Message msg WHERE msg.chat = :chat ORDER BY msg.timestamp DESC");
query.setParameter("chat", chat);
或此查询:
CriteriaBuilder criteriaBuilder = manager.getCriteriaBuilder();
CriteriaQuery<Message> criteriaQuery = criteriaBuilder.createQuery(Message.class);
Root<Message> msgRoot = criteriaQuery.from(Message.class);
Expression<Chat> chatExpression = msgRoot.get("chat");
ParameterExpression<Chat> chatParameter = criteriaBuilder.parameter(Chat.class);
Predicate belongsToChat = criteriaBuilder.equal(chatParameter, chatExpression);
criteriaQuery.where(belongsToChat);
TypedQuery query = manager.createQuery(criteriaQuery);
query.setParameter(chatParameter, this);
Message
得到了
@Getter
@Setter
private long timestamp;
所以我猜我会得到这个聊天的所有消息,但不,我没有。我只删除了ORDER BY msg.timestamp DESC
部分。 ORDER BY msg.timestamp DESC
,ORDER BY msg.timestamp ASC
和ORDER BY msg.timestamp
都不起作用。
我希望我的问题解释得很好,因为我唯一需要添加的是我的日志文件:
DEBUG Cache - Object "beans.Chat@f4ce6b" (id="beans.Chat:51") taken from Level 1 cache (loadedFlags="[YY]") [cache size = 5]
DEBUG Query - JPQL Single-String with "SELECT msg FROM Message msg WHERE msg.chat = :chat ORDER BY msg.timestamp DESC"
DEBUG Query - JPQL Query : Compiling "SELECT msg FROM Message msg WHERE msg.chat = :chat ORDER BY msg.timestamp DESC"
DEBUG Query - JPQL Query : Compile Time = 7 ms
DEBUG Query - QueryCompilation:
[from:ClassExpression(alias=msg)]
[filter:DyadicExpression{PrimaryExpression{msg.chat} = ParameterExpression{chat}}]
[ordering:OrderExpression{PrimaryExpression{msg.timestamp} descending}]
[symbols: msg type=beans.Message, chat type=beans.Chat]
DEBUG Connection - Created DB from MongoClient
DEBUG Connection - ManagedConnection org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@dfdf24 [conn=com.mongodb.DB@16e62fc, commitOnRelease=true, closeOnRelease=true, closeOnTxnEnd=false] is starting
DEBUG Connection - ManagedConnection org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@dfdf24 [conn=com.mongodb.DB@16e62fc, commitOnRelease=true, closeOnRelease=true, closeOnTxnEnd=false] is committing
DEBUG Connection - ManagedConnection org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@dfdf24 [conn=com.mongodb.DB@16e62fc, commitOnRelease=true, closeOnRelease=true, closeOnTxnEnd=false] committed connection
DEBUG Query - JPQL Query : Compiling "SELECT msg FROM Message msg WHERE msg.chat = :chat ORDER BY msg.timestamp DESC" for datastore
WARN Query - FROM clause will be ignored. Not supported for this datastore (MongoDB doesn't do 'joins')
DEBUG Query - Compilation of filter to be evaluated completely in-datastore was impossible :
java.lang.NullPointerException
at org.datanucleus.store.mongodb.query.QueryToMongoDBMapper.getFieldNameForPrimary(QueryToMongoDBMapper.java:756)
at org.datanucleus.store.mongodb.query.QueryToMongoDBMapper.processPrimaryExpression(QueryToMongoDBMapper.java:453)
at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.compilePrimaryExpression(AbstractExpressionEvaluator.java:193)
at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.compileUnaryExpression(AbstractExpressionEvaluator.java:182)
at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.compileAdditiveMultiplicativeExpression(AbstractExpressionEvaluator.java:161)
at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.compileRelationalExpression(AbstractExpressionEvaluator.java:136)
at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.compileOrAndExpression(AbstractExpressionEvaluator.java:78)
at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.evaluate(AbstractExpressionEvaluator.java:46)
at org.datanucleus.query.expression.Expression.evaluate(Expression.java:338)
at org.datanucleus.query.expression.DyadicExpression.evaluate(DyadicExpression.java:69)
at org.datanucleus.store.mongodb.query.QueryToMongoDBMapper.compileFilter(QueryToMongoDBMapper.java:176)
at org.datanucleus.store.mongodb.query.QueryToMongoDBMapper.compile(QueryToMongoDBMapper.java:152)
at org.datanucleus.store.mongodb.query.JPQLQuery.compileQueryFull(JPQLQuery.java:400)
at org.datanucleus.store.mongodb.query.JPQLQuery.compileInternal(JPQLQuery.java:213)
at org.datanucleus.store.query.Query.executeQuery(Query.java:1816)
at org.datanucleus.store.query.Query.executeWithMap(Query.java:1762)
at org.datanucleus.api.jpa.JPAQuery.getResultList(JPAQuery.java:202)
...
WARN ServletHandler
java.lang.NullPointerException
at org.datanucleus.store.mongodb.query.QueryToMongoDBMapper.getFieldNameForPrimary(QueryToMongoDBMapper.java:756)
at org.datanucleus.store.mongodb.query.QueryToMongoDBMapper.processPrimaryExpression(QueryToMongoDBMapper.java:453)
at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.compilePrimaryExpression(AbstractExpressionEvaluator.java:193)
at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.compileUnaryExpression(AbstractExpressionEvaluator.java:182)
at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.compileAdditiveMultiplicativeExpression(AbstractExpressionEvaluator.java:161)
at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.compileRelationalExpression(AbstractExpressionEvaluator.java:136)
at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.compileOrAndExpression(AbstractExpressionEvaluator.java:78)
at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.evaluate(AbstractExpressionEvaluator.java:46)
at org.datanucleus.query.expression.Expression.evaluate(Expression.java:338)
at org.datanucleus.store.mongodb.query.QueryToMongoDBMapper.compileOrdering(QueryToMongoDBMapper.java:250)
at org.datanucleus.store.mongodb.query.QueryToMongoDBMapper.compile(QueryToMongoDBMapper.java:162)
at org.datanucleus.store.mongodb.query.JPQLQuery.compileQueryFull(JPQLQuery.java:400)
at org.datanucleus.store.mongodb.query.JPQLQuery.compileInternal(JPQLQuery.java:213)
at org.datanucleus.store.query.Query.executeQuery(Query.java:1816)
at org.datanucleus.store.query.Query.executeWithMap(Query.java:1762)
at org.datanucleus.api.jpa.JPAQuery.getResultList(JPAQuery.java:202)
...