使用Key.Id查询Google App Engine数据存储区(Query Google App Engine Datastore with Key.Id)

编程入门 行业动态 更新时间:2024-10-17 20:18:51
使用Key.Id查询Google App Engine数据存储区(Query Google App Engine Datastore with Key.Id)

我正在将一个简单的Java应用程序部署到Google App Engine。

我有一个包含Key的简单JPA实体作为我生成的ID。

import javax.persistence.*; @Entity public class MyEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private com.google.appengine.api.datastore.Key key; ...

一旦我坚持这个对象。 我可以像这样查看钥匙的ID ...

long id = entity.getKey().getId();

你知道我怎样才能使用相同的ID来获得我的实体吗? 像这样的东西...

Query query = em.createQuery("SELECT e FROM MyEntity e WHERE e.key.id = :myId"); query.setParameter("myId", id);

上述不起作用。 我知道我可以通过将Key作为参数传递回来,但我想知道是否可以使用long id。

I'm deploying a simple Java app to Google App Engine.

I have a simple JPA Entity containing a Key as my generated ID.

import javax.persistence.*; @Entity public class MyEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private com.google.appengine.api.datastore.Key key; ...

Once I persisted this object. I can view the key's ID like so...

long id = entity.getKey().getId();

Do you know how I can I use the same Id to get my entity back? Something like this...

Query query = em.createQuery("SELECT e FROM MyEntity e WHERE e.key.id = :myId"); query.setParameter("myId", id);

The above doesn't work. I know I can get it back by passing in the Key as the parameter, but I'd like to know if I could use the long id instead.

最满意答案

找到解决方案,使用KeyFactory创建一个Key并传入ID。 然后在密钥上查询。

Key key = KeyFactory.createKey(MyEntity.class.getSimpleName(), id);

Found the solution, create a Key using the KeyFactory and pass in the ID. Then Query on Key.

Key key = KeyFactory.createKey(MyEntity.class.getSimpleName(), id);

更多推荐

本文发布于:2023-07-15 05:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110750.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据存储   Google   Key   Id   App

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!