jacekn
jacekn

Reputation: 1541

JPA: How to get updated entity object after insert?

Sorry, not clear how to ask this question. Here's my scenario:

Controller

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);
    }
}

As you can see, I am not modifying the entity object reference, anywhere. I was uder the impression that the merge operation would update the persisted object with data that has been initialized at the table level. In my case it's only the id that is auto generated. But there could be other things, such as creation timestamps, etc... Is there something that I need to do, to have this generated id set on the persisted object? There is no issue with the operation itself. The record is saved in the table with the generated id.

I'm using Hibernate 4 for JPA provider.

Upvotes: 1

Views: 2158

Answers (1)

Bozho
Bozho

Reputation: 597106

'merge() returns the new instance, so just make every method return your entity

Upvotes: 5

Related Questions