Java GAE 数据存储上的子查询
我正在尝试创建一个具有用户之间连接的用户数据库(朋友列表)。 有 2 个主表:UserEntity(主字段 id)和 FriendEntity,其字段为: - initiatorId - 发起友谊的用户的id -friendId - 已被邀请的用户的id。
现在我正在尝试获取某个特定用户的所有朋友,并在此处使用 JDO 中的子查询时遇到了一些问题。
从逻辑上讲,查询应该是这样的: SQL: SELECT * FROM UserEntity WHERE EXISTS (SELECT * FORM FriendEntity WHERE (initiatorId == UserEntity.id &&friendId == userId) || (friendId == UserEntity.id && initiatorId == userId))
或SELECT * FROM UserEntity WHERE userId IN (SELECT * FROM FriendEntity WHERE initiatorId == UserEntity.id) OR userId IN (SELECT * FROM FriendEntity WHEREfriendId == UserEntity.id)
因此,为了复制 JDOQL 中的最后一个查询,我尝试执行以下操作以下:
Query friendQuery = pm.newQuery(FriendEntity.class);
friendQuery.setFilter("initiatorId == uidParam");
friendQuery.setResult("friendId");
Query initiatorQuery = pm.newQuery(FriendEntity.class);
initiatorQuery.setFilter("friendId == uidParam");
initiatorQuery.setResult("initiatorId");
Query query = pm.newQuery(UserEntity.class);
query.setFilter("initiatorQuery.contains(id) || friendQuery.contains(id)");
query.addSubquery(initiatorQuery, "List initiatorQuery", null, "String uidParam");
query.addSubquery(friendQuery, "List friendQuery", null, "String uidParam");
query.declareParameters("String uidParam");
List<UserEntity> friends = (List<UserEntity>) query.execute(userId);
结果我收到以下错误: 解析表达式时不支持的方法。
有人可以帮忙解决这个问题吗?
I am trying to create a database of users with connection between users (friends list).
There are 2 main tables: UserEntity (main field id) and FriendEntity with fields:
- initiatorId - id of user who initiated the friendship
- friendId - id of user who has been invited.
Now I am trying to fetch all friends of one particular user and encountered some problems with using subqueries in JDO here.
Logically the query should be something like this:
SQL: SELECT * FROM UserEntity WHERE EXISTS (SELECT * FORM FriendEntity WHERE (initiatorId == UserEntity.id && friendId == userId) || (friendId == UserEntity.id && initiatorId == userId))
or SELECT * FROM UserEntity WHERE userId IN (SELECT * FROM FriendEntity WHERE initiatorId == UserEntity.id) OR userId IN (SELECT * FROM FriendEntity WHERE friendId == UserEntity.id)
So to replicate the last query in JDOQL, I tried to do the following:
Query friendQuery = pm.newQuery(FriendEntity.class);
friendQuery.setFilter("initiatorId == uidParam");
friendQuery.setResult("friendId");
Query initiatorQuery = pm.newQuery(FriendEntity.class);
initiatorQuery.setFilter("friendId == uidParam");
initiatorQuery.setResult("initiatorId");
Query query = pm.newQuery(UserEntity.class);
query.setFilter("initiatorQuery.contains(id) || friendQuery.contains(id)");
query.addSubquery(initiatorQuery, "List initiatorQuery", null, "String uidParam");
query.addSubquery(friendQuery, "List friendQuery", null, "String uidParam");
query.declareParameters("String uidParam");
List<UserEntity> friends = (List<UserEntity>) query.execute(userId);
In result I get the following error:
Unsupported method while parsing expression.
Could anyone help with this query please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试进行联接 - 您无法跨实体进行联接。
这是一个在存在拥有关系的情况下执行简单联接的示例
http://gae-java- persistence.blogspot.com/2010/03/executing-simple-joins-across-owned.html
另请查看该网站,它有很多关于如何使持久性工作发挥作用的非常好的信息。以及一些连接的解决方法。
您可以在实体之间设置关系,请参阅 GAE 文档。
You are trying to do a join - You cannot do joins across Entities.
Here is an example of doing a simple join if there is an owned relationship
http://gae-java-persistence.blogspot.com/2010/03/executing-simple-joins-across-owned.html
Also look at that web site it has a lot of really good information on getting the persistence stuff to work. as well as a couple of work-arounds for joins.
You can set up relationships among Entities, for that see GAE docs.