English 中文(简体)
我的法典为什么没有更新我的实体的反对? Spring Hibernate MVC app
原标题:Why doesn t my code update my entity object? Spring Hibernate MVC app

I have a Spring Hibernate MVC and trying to implement CRUD operations. I have gotten create, get, delete in a "RESTful" kinda way but I am having difficulties with update.

在试图更新物体时,参数是/id? 表格是控制器法:

@RequestMapping(value = "/{id}", params = "form", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Long id, Model uiModel) {
        uiModel.addAttribute("invoice", invoiceServiceHibernate.getInvoice(id));
        return "invoices/update";
    }

因此,更新的JSP表格使用该表格填充了该物体。 The JSP Update form of the following:

<c:if  test="${!empty invoice}">
<form:form method="PUT" commandName="invoice">
    <table>
        <tr>
            <td>Amount</td>
            <td><form:input value="${invoice.amount}" path="amount" /></td>
        </tr>
        ... more labels and fields ...
        <tr>
            <td colspan="3">
                <input type="submit" value="Update Invoice" />
            </td>
        </tr>
    </table>
</form:form>

提交时,要求采用的控制器方法应为:

@RequestMapping(method = RequestMethod.PUT)
    public String update(Invoice invoice, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            uiModel.addAttribute("invoice", invoice);
            return "invoices/update";
        }
        uiModel.asMap().clear();
        invoiceServiceHibernate.updateInvoice(invoice);
        return "redirect:/invoices/";
    }

我已尝试了<代码>update、saveOrUpdate,>merinvoiceserviceHibernate.updateInvoice(invoice),但并不奏效。

控制员最后开立了新的发票,而不是更新旧的发票。 为什么如此? 我读到。 连接并会谈使用@ModelAttribute。 我对它进行了审判,但它仍然不可行。 使用@ModelAttribute或@Valid没有。 这笔钱并不产生新的发票,但它没有更新目前的发票。

任何建议?

感谢!

最佳回答

Where s the ID in your JSP? It looks like you re not sending back invoice.id as part of your form.

如果在即将提出的申请中包含一些how,那么自由国就没有任何办法说明它本应更新的发票。 Spring s WebDataBinder的基本功能确实像现在这样是魔鬼。 所有这些都成为新的目标(或如果在《示范Map》中存在,则使用现有物体),然后针对星号提出细数要求参数,并称号。 (当然会发生某种转变类型,等等) 如果申请参数中没有身份证,那么当网站数据交换机提出新申请时,贵实体的国际发展法领域就会出现空白。 你们需要一种方式,向数据管理员提供这一身份证明。

你能够做到这一点有两个基本途径。

首先,你可以以你的形式约束实体中的每一领域。 您的发票、发票、转帐等藏匿处。

第二,你可以在服务器上保存一个实体的复印件。 当然,这引进了服务器的侧面状态,也是一种完全是可再生的。 各位可以利用你的控制员的春斯·阿蒂丁特来告诉它,让实体保持记忆,并以新的价值观更新数据集,而不是创造新的价值。

问题回答

你们需要一笔交易。 Annotate updateInvoice with @transactional.

If you don t have it set-up, check here. In short, you need <tx:annotation-driven /> in your spring config file, and to declare a transaction manager with a reference to your session/entity manager





相关问题
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 ...