1
对不起,不清楚如何提出这个问题。这里是我的情况:JPA:插入后如何获取更新的实体对象?
控制器
HostSite hostSite=new HostSite();
hostSite.setUrl("www.somesite.com");
// hostSite.getId() is null, naturally
hostSiteService.saveHostSite(hostSite);
// hostSite.getId() is still null
HostSiteServiceImpl
public void saveHostSite(HostSite hostSite) {
hostSiteDAO.save(hostSite);
}
HostSiteDAOImpl
public void save(HostSite hostSite) {
if(hostSite!=null) {
getEntityManager().merge(hostSite);
}
}
正如你所看到的,我没有在任何地方修改实体对象引用。我更喜欢合并操作会更新持久化对象的数据已在表级初始化。在我的情况下,它只是自动生成的ID。但也可能有其他的东西,比如创建时间戳等等。是否有我需要做的事情,让这个生成的id在持久对象上设置?操作本身没有问题。记录与生成的ID一起保存在表格中。
我使用Hibernate 4作为JPA提供程序。