Google App Engine JPA persist() 方法未填充我的对象的 ID
我正在尝试保留一个新的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以确认调用 em.refresh(object) 将填充持久对象的 id。
I can confirm that calling em.refresh(object) will populate the id of the persisted object.