Google App Engine JPA persist() 方法未填充我的对象的 ID

发布于 2024-10-10 01:35:53 字数 524 浏览 8 评论 0原文

我正在尝试保留一个新的 Java 对象,该对象具有自动生成的 Id,但在调用 EntityManager 的 persist() 方法时并未填充该对象。我在我的 Object 类中有..

...
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
...

并且我本质上使用...来持久化它

emf = Persistence.createEntityManagerFactory("transactions-optional");
em = emf.createEntityManager();
em.persist(myObj);

。持久化它之后, id 字段不会被填充。我尝试过调用 em.flush() 但它抱怨它必须在事务内。我不需要或不想要事务,但在持久化对象后我确实需要这个 Id。

这是 GAE 的 JPA 实现中的错误吗?

任何建议将不胜感激!

I'm trying to persist a new Java object which has an auto-generated Id but it is not being populated upon calling EntityManager's persist() method. I have in my Object class..

...
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
...

and I'm persisting it essentially using...

emf = Persistence.createEntityManagerFactory("transactions-optional");
em = emf.createEntityManager();
em.persist(myObj);

After persisting it, the id field is not populated. I've tried calling em.flush() but it complains that it must be inside a transaction. I don't need or want transactions but I do need this Id immediately after persisting the object.

Is this a bug in GAE's implementation of JPA?

Any suggestions would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅窗月明清似水 2024-10-17 01:35:53

我可以确认调用 em.refresh(object) 将填充持久对象的 id。

I can confirm that calling em.refresh(object) will populate the id of the persisted object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文