gpt4 book ai didi

Java - 为什么 hibernate 会检测到脏对象,尽管它已经保存了?

转载 作者:太空宇宙 更新时间:2023-11-04 06:41:15 28 4
gpt4 key购买 nike


我在我们的 Web 应用程序中使用 spring 和 hibernate,并且我们遇到了在一次执行中插入多行的问题。我们的代码中没有循环在一个用户请求中插入 3 行。我只是想知道这( hibernate 检测到脏对象)是否可能是原因。
请帮忙...
谢谢,

最佳答案

When the session is opened and the object is just saved in or retrieved from the database. This state is called persistent. During this state Hibernate manages the object and saves your changes, if you commit them. Below you can see an example. A car is saved and the name is changed afterwards. As the car is in persistent state, the new name will be saved.

Session session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.save(car);
car.setName(“Peugeot”);
tx.commit();

The following code loads a car by id and changes the name. There is no session.update involved. Every object which is loaded by session.get, session.load or a query is in persistent state. It is stored in the persistence context of the session. When tx.commit() is called, Hibernate will flush the persistence context and all not yet written insert, update and delete statements are executed.

检查您在保存操作之后和事务提交之前是否没有修改对象(关系)。

关于Java - 为什么 hibernate 会检测到脏对象,尽管它已经保存了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24649073/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com