简体   繁体   中英

Hibernate get saved object by id before persisted to database

I have a hibernate object that I'm persisting using session.saveOrUpdate without committing to the database which gives me the object id. I'm passing the object id to my interface which will later be used to rebuild the object on submit. When I submit the form, I'm no longer able to do a session query to retrieve object because the object technically doesn't exist in the database.

Does anybody know how to retrieve that object? If I just create a new object, the database id's will increment out of control. Any thoughts?

Question Edits and clarification.

When the form is submitted and contains errors, I need to catch the errors and reload the page with the errors. The problem I've been having is I'm losing the non persisted data in my collections. My solution was to temporarily persist the current object with all the child objects to the users session and then copy the persisted object back into the main object once the page reloaded which would return all the users previous data. However I've run into the failed to lazily initialize a collection of role: which is why I've resorted to a transaction such as session.update. This solved this issue, but gave id's to the newly created objects prior to the validation error before actually saving to the database.

Code Sample

@Property
private PurchaseRequest pr;

@Persist
private PurchaseRequest prPersist;   

Class<?> onActivate(Long prId) {
    if(request.isXHR()) {
        return null;
    }

    PurchaseRequest purchaseRequest = null;

    if(prPersist != null) {           
        session.update(prPersist);
        purchaseRequest = prPersist;
        prPersist = null;
    } else {
        purchaseRequest = (PurchaseRequest) session.createCriteria(PurchaseRequest.class)
            .add(Restrictions.idEq(prId))
            .uniqueResult();
    }

    this.pr = purchaseRequest;
}

Validation Method

void onValidateFromPR() throws Exception {
    if (form.getHasErrors()) {
        prPersist = this.pr;
        return Page.Index;
    }
}

There's no reason to saveOrUpdate an object and assign it an ID if you don't want it to be saved in the database yet. Only save it when the form is submitted and the data contained in the object are valid.

The thing is that the object is saved in the current DB transaction. That's what it is not yet visible to another transactions (or sessions).

That object does not exists for another transactions until the current one is commited. You can play along with Isolation level, but I would not recommend it.

Either you persisted it at the end of the transaction, and use a detach reattach pattern. Or you do partial saves, where you save an object that is not fully done. You can flag it someway.

Another approach is to use an in-memory storage of such objects.

It is legit that the ID becomes generated since every time you call saveOrUpdate or whatever method that makes an object persistent you are telling Hibernate that you are willing to save it eventually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM