Hibernate changes data to insert

Source: Internet
Author: User

Another problem occurs when you use SSH. When you query data in the database and save the data, you can insert a record again. Check the log records and find that this object has been treated as transient when it is saved. So it is natural that hibernate uses insert instead of update, but it is okay to use the same logic in other experiments before, this is only because many functions are provided.

The logic is like this: Find an object by Id in the action, modify some values, and then call the business layer to save it. In this case, the first query and subsequent save operations are not in a transaction. It is possible that when the session is saved, the association with the object has been released, and the object becomes transient. I have been thinking for a long time, change the logic to a new object in the action, assign values to the id and the attributes to be modified, assign the object to the business layer, query the object in the business layer, modify the object, and save the object, in addition, the business layer method is configured as a transaction to solve the problem.

The configuration method is as follows:

[Html]
<Bean id = "transactionManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager">
<Property name = "sessionFactory" ref = "sessionFactory"> </property>
</Bean>
<Tx: advice id = "txAdvice" transaction-manager = "transactionManager">
<Tx: attributes>
<Tx: method name = "*"/>
</Tx: attributes>
</Tx: advice>
<Aop: config> www.2cto.com
<Aop: pointcut id = "myPointcut" expression = "bean (floorService) | bean (tableService)"/>
<Aop: advisor advice-ref = "txAdvice" pointcut-ref = "myPointcut"/>
</Aop: config>

FloorService and tableService are the objects at the business layer. <tx: method name = "*"/> configures each method in the object as a transaction.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.