有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java MongoDB>DataNucleus>JPA查询使用“成员”工作

Query query = manager.createQuery("SELECT chat FROM Chat chat WHERE :user MEMBER OF chat.members");

CriteriaBuilder criteriaBuilder = manager.getCriteriaBuilder();
CriteriaQuery<Chat> criteriaQuery = criteriaBuilder.createQuery(Chat.class);
Root<Chat> chatRoot = criteriaQuery.from(Chat.class);

Expression<Collection<User>> membersExpression = chatRoot.get("members");
ParameterExpression<User> userParameter = criteriaBuilder.parameter(User.class);
Predicate containsGroup = criteriaBuilder.isMember(userParameter, membersExpression);
criteriaQuery.where(containsGroup);
TypedQuery query = manager.createQuery(criteriaQuery);
query.setParameter(userParameter, this);

我只是得到了我想要的

DEBUG Cache - Query Cache of type "org.datanucleus.query.cache.SoftQueryCompilationCache" initialised
DEBUG Cache - Query Cache of type "org.datanucleus.store.query.cache.SoftQueryDatastoreCompilationCache" initialised
DEBUG Cache - Query Cache of type "org.datanucleus.store.query.cache.SoftQueryResultsCache" initialised
DEBUG Query - JPQL Single-String with "SELECT DN_THIS FROM com.resocial.beans.Chat DN_THIS WHERE :DN_PARAM_0 MEMBER OF DN_THIS.members"
DEBUG Query - QueryCompilation:
  [from:ClassExpression(alias=DN_THIS)]
  [filter:InvokeExpression{[PrimaryExpression{DN_THIS.members}].contains(ParameterExpression{DN_PARAM_0})}]
  [symbols: DN_THIS type=com.resocial.beans.Chat, DN_PARAM_0 type=unknown]
DEBUG Connection - Created DB from MongoClient
DEBUG Connection - ManagedConnection org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@1119d93 [conn=com.mongodb.DB@ad7b11, commitOnRelease=true, closeOnRelease=true, closeOnTxnEnd=false] is starting
DEBUG Persistence - Managing Persistence of Class : com.resocial.beans.Chat [Table : (none), InheritanceStrategy : complete-table]
DEBUG Schema - Ensuring presence of index "Chat_PK" for collection "Chat" as "{ "id" : 1}"
DEBUG Connection - ManagedConnection org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@1119d93 [conn=com.mongodb.DB@ad7b11, commitOnRelease=true, closeOnRelease=true, closeOnTxnEnd=false] is committing
DEBUG Connection - ManagedConnection org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@1119d93 [conn=com.mongodb.DB@ad7b11, commitOnRelease=true, closeOnRelease=true, closeOnTxnEnd=false] committed connection
DEBUG Query - JPQL Query : Compiling "SELECT DN_THIS FROM com.resocial.beans.Chat DN_THIS WHERE :DN_PARAM_0 MEMBER OF DN_THIS.members" 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 : 
Dont currently support querying of multi-valued fields at com.resocial.beans.Chat.members
org.datanucleus.exceptions.NucleusUserException: Dont currently support querying of multi-valued fields at com.resocial.beans.Chat.members
    at org.datanucleus.store.mongodb.query.QueryToMongoDBMapper.getFieldNameForPrimary(QueryToMongoDBMapper.java:860)
    at org.datanucleus.store.mongodb.query.QueryToMongoDBMapper.processPrimaryExpression(QueryToMongoDBMapper.java:453)
    at org.datanucleus.store.mongodb.query.QueryToMongoDBMapper.processInvokeExpression(QueryToMongoDBMapper.java:630)
    at org.datanucleus.query.evaluator.AbstractExpressionEvaluator.compilePrimaryExpression(AbstractExpressionEvaluator.java:213)
    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.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)
    ...
DEBUG Query - JPQL Query : Compile Time for datastore = 72 ms
DEBUG Connection - ManagedConnection found in the pool : "org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@e96dbd [conn=com.mongodb.DB@ad7b11, commitOnRelease=true, closeOnRelease=false, closeOnTxnEnd=false]" for key="org.datanucleus.ExecutionContextImpl@a0497b" in factory="ConnectionFactory:tx[org.datanucleus.store.mongodb.ConnectionFactoryImpl@c9b10a]"
DEBUG Connection - ManagedConnection org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@e96dbd [conn=com.mongodb.DB@ad7b11, commitOnRelease=true, closeOnRelease=false, closeOnTxnEnd=false] is starting
DEBUG Query - JPQL Query : Executing "SELECT DN_THIS FROM com.resocial.beans.Chat DN_THIS WHERE :DN_PARAM_0 MEMBER OF DN_THIS.members" ...
DEBUG Native - Performing find() using query on collection Chat for fields={ "id" : 1} with filter={ } and ordering=null
INFO  Query - Reading in results for query "SELECT DN_THIS FROM com.resocial.beans.Chat DN_THIS WHERE :DN_PARAM_0 MEMBER OF DN_THIS.members" since the connection used is closing/committing
DEBUG Cache - Object with id "com.resocial.beans.Chat:51" not found in Level 1 cache [cache size = 2]
DEBUG Cache - Object with id "com.resocial.beans.Chat:51" not found in Level 2 cache
DEBUG Cache - Object "com.resocial.beans.Chat@1f0347a" (id="com.resocial.beans.Chat:51") added to Level 1 cache (loadedFlags="[YN]")
DEBUG Cache - Object "com.resocial.beans.Chat@1f0347a" (id="51") added to Level 2 cache (fields="[0]", version="")
DEBUG Query - JPQL Query : Processing the "filter" clause using in-memory evaluation (clause = "InvokeExpression{[PrimaryExpression{DN_THIS.members}].contains(ParameterExpression{DN_PARAM_0})}")
DEBUG Query - Evaluating filter for 1 candidates
DEBUG Lifecycle - Object "com.resocial.beans.Chat@1f0347a" (id="com.resocial.beans.Chat:51") has a lifecycle change : "HOLLOW"->"P_NONTRANS"
DEBUG Connection - ManagedConnection found in the pool : "org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@e96dbd [conn=com.mongodb.DB@ad7b11, commitOnRelease=true, closeOnRelease=false, closeOnTxnEnd=false]" for key="org.datanucleus.ExecutionContextImpl@a0497b" in factory="ConnectionFactory:tx[org.datanucleus.store.mongodb.ConnectionFactoryImpl@c9b10a]"
DEBUG Retrieve - Fetching object "com.resocial.beans.Chat@1f0347a" (id=51) fields [members]
DEBUG Retrieve - Object "com.resocial.beans.Chat@1f0347a" (id="51") being retrieved from MongoDB
DEBUG Native - Retrieving object for { "id" : 51}
 [...]
DEBUG Persistence - Object "com.resocial.beans.Chat@1f0347a" (id="com.resocial.beans.Chat:51") is having the value in field "members" replaced by a SCO wrapper
DEBUG Persistence - Created SCO wrapper of type org.datanucleus.store.types.wrappers.ArrayList for object "com.resocial.beans.Chat@1f0347a" field "members" with 2 entries, using options="cached,allowNulls"
DEBUG Retrieve - Execution Time = 111 ms
DEBUG Connection - ManagedConnection org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@e96dbd [conn=com.mongodb.DB@ad7b11, commitOnRelease=true, closeOnRelease=false, closeOnTxnEnd=false] is committing
DEBUG Connection - ManagedConnection org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@e96dbd [conn=com.mongodb.DB@ad7b11, commitOnRelease=true, closeOnRelease=false, closeOnTxnEnd=false] committed connection
DEBUG Cache - Object "com.resocial.beans.Chat@1f0347a" (id="51") is having the following fields in Level 2 cache object updated : [1]
DEBUG Query - Evaluation of filter for candidate="com.resocial.beans.Chat@1f0347a" gives a match
DEBUG Query - JPQL Query : Execution Time = 298 ms
DEBUG Connection - ManagedConnection org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@e96dbd [conn=com.mongodb.DB@ad7b11, commitOnRelease=true, closeOnRelease=false, closeOnTxnEnd=false] is committing
DEBUG Connection - ManagedConnection org.datanucleus.store.mongodb.ConnectionFactoryImpl$ManagedConnectionImpl@e96dbd [conn=com.mongodb.DB@ad7b11, commitOnRelease=true, closeOnRelease=false, closeOnTxnEnd=false] committed connection

在我的日志文件中

我想得到的是,你已经猜到了,一个包含所有聊天的列表,其中我的用户被列为会员。是否有一种persistenceapi方法(仅限于jpa,而不是特定于datanucleus或mongodb)来实现我的目标

是的,我坚持使用MongoDB 2.4


共 (1) 个答案

  1. # 1 楼答案

    在更改持久类@Entit[ies]之后,出于一个很好的理由,您需要对它们进行两次增强(我不理解,但它必须是好的)。升级到MongoDB 3.0或更高版本或DataNucleus 5.0或更高版本时,您不再需要这样做