Apache Wicket、OpenJPA 和 Spring/ROO

发布于 2024-09-27 06:33:28 字数 1179 浏览 5 评论 0原文

我已经设置了一个 Spring ROO 应用程序,将持久性设置为 OpenJPA 并创建了一些实体。 然后我用 Apache Wicket 替换了 Spring MVC。东西似乎工作正常,我已经成功显示了客户实体列表。

接下来是客户的编辑视图。现在,我已经制作了一个直接使用 OpenJPA 实体作为表单模型的 Wicket 表单,因此我必须使实体类实现 Serialized。

现在,我不确定如何正确实现 OpenJPA 持久性,我现在得到的是:

@Override
protected void onSubmit() {
    try {
        if (customer.getId()!=null) {
            customer.merge();
        }
        else {
            customer.persist();
        }
    }
    catch (Exception e) {
        throw new Error(e);
    }
    super.onSubmit();
}

这有效,但每个 Customer 对象只能执行一次。不知何故。 也就是说,我提交表单一次,它适用于新客户 (.persist()) 和现有客户 (.merge())。但是,我为同一个客户再次提交表单,但出现此错误(我在此处添加了一些换行符):

<openjpa-2.0.0-r422266:935683 nonfatal store error>
org.apache.openjpa.persistence.OptimisticLockException:
An optimistic lock violation was detected when flushing object instance "no.magge.iumb.domain.crm.PrivateCustomer-379" to the data store.
This indicates that the object was concurrently modified in another transaction.

我的问题是,坚持使用 OpenJPA 的正确方法是什么?为什么我会收到该错误?

Wicket-wise:我是否应该使用可拆卸的客户模型创建一个单独的 Wicket IModel,这可能是我遇到这些问题的原因吗?

非常感谢您的任何建议!

I've set up a Spring ROO application, set persistence to OpenJPA and created some entities.
Then I replaced Spring MVC with Apache Wicket. Stuff seems to be working fine and I've successfully displayed a list of Customer entities.

Next up was the editing view for the customer. For now I've made a Wicket form that uses the OpenJPA entity directly as the form model, and thus I had to make the entity class implement Serializable.

Now, I'm not sure how to correctly implement the OpenJPA persistence, what I've got now is this:

@Override
protected void onSubmit() {
    try {
        if (customer.getId()!=null) {
            customer.merge();
        }
        else {
            customer.persist();
        }
    }
    catch (Exception e) {
        throw new Error(e);
    }
    super.onSubmit();
}

That works, but only once per Customer object. Somehow.
That is, I submit my form once and it works both with a new customer (.persist()) and a existing customer (.merge()). However, I submit the form again for the same customer I get this error (I added some linebreaks here):

<openjpa-2.0.0-r422266:935683 nonfatal store error>
org.apache.openjpa.persistence.OptimisticLockException:
An optimistic lock violation was detected when flushing object instance "no.magge.iumb.domain.crm.PrivateCustomer-379" to the data store.
This indicates that the object was concurrently modified in another transaction.

My question is, what is the correct way to persist with OpenJPA and why am I getting that error ?

Wicket-wise: Should I have created a separate Wicket IModel with a detachable Customer model and could that be the reason that I have these problems?

Thanks a bunch for any advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑咖 2024-10-04 06:33:28

帮自己一个忙,分离你的应用程序层。视图中的代码不应访问数据库。

创建一个 Service 层和/或一个 Dao 层,对这些层的代码进行单元测试以查看它们是否正常工作,然后将 dao 或服务对象注入到 wicket 组件中。 (我建议你使用 spring 来实现这一点,但你也可以手动完成)

在你的场景中,有很多不同的事情可能会在一个地方失败,而且几乎不可能将它们分开。

以下是一些提示:

Do yourself a favor and separate your application layers. Code in a view should not ever access a Database.

Create a Service layer and / or a Dao layer, unit test the code of those layers to see that they are working and then inject a dao or service object into the wicket component. (I'd recommend you to use spring for that, but you can also do it manually)

With your scenario, there are so many different things that can fail in one spot, and it's nearly impossible to separate them.

Here are some pointers:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文