gpt4 book ai didi

java - Eclipselink mongoDB native 查询 find() 不起作用

转载 作者:行者123 更新时间:2023-12-01 05:06:29 27 4
gpt4 key购买 nike

我最近正在开发一个带有 mongoDB 数据库的 JAVA 项目。因此我决定使用 eclipselink 2.4。关于查询,它允许通常的“类似 SQL”查询,但也(据说)允许 native mongoDB 查询。我关注了this tutorial并使一切正常运转。我能够运行 findOne() native mongoDB 查询 (queryOne),但我无法运行 find() 查询(queryAll 不起作用)...

  1. 查询 queryOne = em.createNativeQuery("db.ORDER.findOne()",订单.class);

  2. 查询 queryAll = em.createNativeQuery("db.ORDER.find()", 订单.class);

执行查询时出现错误:

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-6044] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.QueryException
Exception Description: The primary key read from the row [EISMappedRecord(
value => DBQuery: myTest.ORDER -> undefined)] during the execution of the query was detected to be null. Primary keys must not contain null.
Query: ReadAllQuery(referenceClass=Order )
at org.eclipse.persistence.exceptions.QueryException.nullPrimaryKeyInBuildingObject(QueryException.java:912)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:586)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:562)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:776)
at org.eclipse.persistence.queries.ReadAllQuery.registerResultInUnitOfWork(ReadAllQuery.java:781)
at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:433)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1149)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:852)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1108)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:392)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1196)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2875)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1602)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1584)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1549)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:231)
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:411)
at example.Test.testQuery(Test.java:171)
at example.Test.main(Test.java:54)

借助 MongoDB shell 执行查询:

{ "_id" : "5061DC17E4B0881F592675A5", "TOTALCOST" : 3187, "SHIPPINGADDRESS" : [     {   "POSTALCODE" : "L5J1H7",    "COUNTRY" : "Canada",   "PROVINCE" : "ON",  "CITY" : "Ottawa",  "STREET" : "17 Jane St." } ], "ORDERLINES" : [  {   "DESCRIPTION" : "machine",  "COST" : 2999,  "LINENUMBER" : 1 },     {   "DESCRIPTION" : "shipping",     "COST" : 129,   "LINENUMBER" : 2 },     {   "DESCRIPTION" : "installation",     "COST" : 59,    "LINENUMBER" : 3 } ], "VERSION" : NumberLong(1), "DESCRIPTION" : "Pinball machine", "CUSTOMER__id" : "5061DC17E4B0881F592675A4", "BILLINGADDRESS" : [   {   "POSTALCODE" : "L5J1H7",    "COUNTRY" : "Canada",   "PROVINCE" : "ON",  "CITY" : "Ottawa",  "STREET" : "17 Jane St." } ] }
{ "_id" : "5061DC17E4B0881F592675A6", "TOTALCOST" : 565, "SHIPPINGADDRESS" : [ { "POSTALCODE" : "L5J1H7", "COUNTRY" : "Canada", "PROVINCE" : "ON", "CITY" : "Ottawa", "STREET" : "17 Jane St." } ], "ORDERLINES" : [ { "DESCRIPTION" : "machine", "COST" : 500, "LINENUMBER" : 1 }, { "DESCRIPTION" : "balls", "COST" : 5, "LINENUMBER" : 2 }, { "DESCRIPTION" : "shipping", "COST" : 60, "LINENUMBER" : 3 } ], "VERSION" : NumberLong(1), "DESCRIPTION" : "Foosball", "CUSTOMER__id" : "5061DC17E4B0881F592675A4", "BILLINGADDRESS" : [ { "POSTALCODE" : "L5J1H8", "COUNTRY" : "Canada", "PROVINCE" : "ON", "CITY" : "Ottawa", "STREET" : "7 Bank St." } ] }
{ "_id" : "5061DC17E4B0881F592675A8", "TOTALCOST" : 402, "SHIPPINGADDRESS" : [ { "POSTALCODE" : "L5J1H7", "COUNTRY" : "Canada", "PROVINCE" : "ON", "CITY" : "Ottawa", "STREET" : "17 Jane St." } ], "ORDERLINES" : [ { "DESCRIPTION" : "table", "COST" : 300, "LINENUMBER" : 1 }, { "DESCRIPTION" : "balls", "COST" : 5, "LINENUMBER" : 2 }, { "DESCRIPTION" : "rackets", "COST" : 15, "LINENUMBER" : 3 }, { "DESCRIPTION" : "net", "COST" : 2, "LINENUMBER" : 4 }, { "DESCRIPTION" : "shipping", "COST" : 80, "LINENUMBER" : 5 } ], "VERSION" : NumberLong(1), "DESCRIPTION" : "Pingpong table", "CUSTOMER__id" : "5061DC17E4B0881F592675A7", "BILLINGADDRESS" : [ { "POSTALCODE" : "L5J1H8", "COUNTRY" : "Canada", "PROVINCE" : "ON", "CITY" : "Ottawa", "STREET" : "7 Bank St." } ] }

如果您能告诉我如何解决这个问题或一些解决方法,我将不胜感激。

编辑:JPQL 查询可能很有用,我正在使用一些查询作为解决方法,但是,我当前无法查询引用 id。例如,如果我有一个带有客户引用的订单(订单仅保存客户的 ID)和 mongoDB 查询,我会像 taht 一样搜索:

db.ORDER.find({"CUSTOMER__id": "ID"})

我相信 JPQL 类似的查询会是这样的:

query = em.createQuery("Select o from Order o where o.customer.id = \"ID\"");

但是,再一次...不起作用!错误:

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-6168] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.QueryException
Exception Description: Query failed to prepare, unexpected error occurred: [java.lang.ClassCastException: org.eclipse.persistence.eis.mappings.EISOneToOneMapping cannot be cast to org.eclipse.persistence.mappings.OneToOneMapping].
Internal Exception: java.lang.ClassCastException: org.eclipse.persistence.eis.mappings.EISOneToOneMapping cannot be cast to org.eclipse.persistence.mappings.OneToOneMapping
Query: ReadAllQuery(referenceClass=Order jpql="Select o from Order o where o.customer.id = "ID"")
at org.eclipse.persistence.exceptions.QueryException.prepareFailed(QueryException.java:1572)
at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:633)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:882)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:838)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1108)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:392)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1196)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2875)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1602)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1584)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1549)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:231)
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:411)
at example.Test.testQuery(Test.java:175)
at example.Test.main(Test.java:55)
Caused by: java.lang.ClassCastException: org.eclipse.persistence.eis.mappings.EISOneToOneMapping cannot be cast to org.eclipse.persistence.mappings.OneToOneMapping
at org.eclipse.persistence.internal.expressions.RelationExpression.checkForeignKeyJoinOptimization(RelationExpression.java:512)
at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:535)
at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1365)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildNormalSelectStatement(ExpressionQueryMechanism.java:543)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareSelectAllRows(ExpressionQueryMechanism.java:1675)
at org.eclipse.persistence.queries.ReadAllQuery.prepareSelectAllRows(ReadAllQuery.java:719)
at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:656)
at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:614)
... 13 more

谢谢

最佳答案

它似乎试图获取主键为空的对象。而且记录是A5、A6和A8。如果按顺序创建的话,A7好像搞乱了?

是否可以删除 ORDER 文档并再次运行测试(插入和获取)?

关于java - Eclipselink mongoDB native 查询 find() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12586953/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com