1

我有一个场景,Struts2 操作从后端获取模型对象并将其放入 OGNL 堆栈,并使用该数据更新前端视图。

然后我更改视图中与模型对象对应的值并更新。getSession().update(model)然后使用 Hibernate 的方法 保存该值 。

问题:当模型对象被读入 OGNL 堆栈时,该事务上下文关闭并且模型对象被分离。为什么允许我使用 保存更改getSession().update(model)?我认为getSession().merge(model)应该使用。

请帮助我理解歧义。

4

1 回答 1

1

Both methods can pass detached object as parameter, but if there's an object in the context with the given identifier, the first method throws an exception. Read

Update the persistent instance with the identifier of the given detached instance. If there is a persistent instance with the same identifier, an exception is thrown.

public void update(Object object);

The second method doesn't throw an exception because it loads not existing object by its identifier to the context, update it, and returns to the caller.

Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session.

public Object merge(Object object);
于 2015-07-16T10:46:43.640 回答