我使用 Hibernate 和 JPAContainer。这里是实体和意外行为部分的代码:
// MyEntity
@Entity
class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String value;
// getters and setters...
}
// set up JPAContainer
JPAContainer c = JPAContainerFactory.make(MyEntity.class, "lazyhibernate");
c.getEntityProvider().setEntityManager(null);
c.getEntityProvider().setEntityManagerProvider(entityManagerProvider);
c.getEntityProvider().setLazyLoadingDelegate(new HibernateLazyLoadingDelegate());
final BeanItem<MyEntity> item = new BeanItem<MyEntity>(new MyEntity());
fill(item); // fill item fields...
MyEntity e = item.getBean();
c.addEntity(e);
c.commit();
System.out.println(e.getId()); // return null
如何获取新创建实体的 id?