English 中文(简体)
在持续保存到数据库之前, Hidelate 会被 id 保存对象
原标题:Hibernate get saved object by id before persisted to database

我有一个休眠对象, 我使用会话. save orUddate 来持续, 而不承诺使用该对象的代号。 我将对象代号传送到我的接口, 然后再用来重建提交对象。 当我提交表格时, 我不能再做会话查询来检索对象, 因为从技术上讲, 该对象在数据库中不存在 。

有人知道怎么取回那个对象吗?如果我刚刚创建了一个新对象, 数据库的代号会越发失控。 有什么想法吗?

问题编辑和澄清。

当提交表格并包含错误时,我需要抓取错误并用错误重新装入页面。我一直面临的问题是,我在收藏中丢失了未持续的数据。我的解决方案是暂时将当前对象与所有子对象一同持续到用户会话中,然后在重新加载页面后将持续对象复制回主对象,该页面将返回所有用户先前的数据。然而,我遇到了一个错误,无法对角色集进行 Lazic 初始化:这就是为什么我一直使用诸如会话. 更新这样的交易。这解决了这个问题,但在实际保存到数据库之前,先给新创建的对象以ID s 。

代码样本

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

审定方法

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

没有理由 saveOr更新 a 对象,如果您不想在数据库中保存该对象,请指定该对象的代号。只有在提交窗体和对象所含数据有效时才保存该代号。

问题是,当前 DB 交易中保存了对象。 其它交易( 或会话) 尚未看到的东西 。

此对象在进行当前交易之前不存在。 您可以与孤立级别一起玩, 但我不推荐它 。

您要么在交易结尾处坚持, 并使用 < code> detach reatach 模式。 或者部分保存, 保存一个未完全完成的对象。 您可以标记它某种方式 。

另一种做法是使用这种物体的模拟储存。

自每次调用 saveOr更新 或任何使物体具有持久性的方法后,身份识别就会产生, 或任何使物体具有持久性的方法, 你告诉Hibernate, 你愿意最终保存身份识别码。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签