Apache Wicket、OpenJPA 和 Spring/ROO
我已经设置了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
帮自己一个忙,分离你的应用程序层。视图中的代码不应访问数据库。
创建一个 Service 层和/或一个 Dao 层,对这些层的代码进行单元测试以查看它们是否正常工作,然后将 dao 或服务对象注入到 wicket 组件中。 (我建议你使用 spring 来实现这一点,但你也可以手动完成)
在你的场景中,有很多不同的事情可能会在一个地方失败,而且几乎不可能将它们分开。
以下是一些提示:
配置(与JPA非常相似)
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:
Configuration (it's very similar for JPA)