使用 struts2 和 hibernate 更新实体时出现 NonUniqueObjectException
我使用hibernate作为ORM框架,这是我第一次使用它进行写入操作。
在这个应用程序之前,我只是使用 hibernate 从数据库读取数据。
在我的struts操作中,我尝试更新一个名为“Task”的实体,这是更新页面中的形式:
<s:form action="task_update" namespace="/common" cssStyle="width:95%">
<s:textfield value="%{task.id}" cssStyle="display:none" name="task.id"></s:textfield>
<s:textfield name="task.name" value="%{task.name}" label="TaskName"/>
<s:select list="task.managers"
listKey="id" listValue="name" label="Manager" value="%{task.manage}" name="task.department.id">
</s:select>
<s:select list="#session.current_user.departments"
listKey="id" listValue="name" label="Departmentn of this task" value="%{task.department.{id}}" name="task.department.id">
</s:select>
<table>
<caption align="left">Steps</caption>
<tr>
<th>Name</th>
<th>End Time</th>
<th>Operators</th>
<th>Status</th>
<th>Set the order</th>
<th><span id="addStep" style="cursor:pointer" >Add Steps</span></th>
</tr>
<s:iterator value="task.steps">
<tr class="step">
<td>
<s:textfield name="task.steps[0].name" value="%{#this.name}" theme="simple"/>
<s:textfield name="task.steps[0].id" value="%{#this.id}" theme="simple" cssStyle="display:none"/>
<s:textfield name="task.steps[0].position" value="%{#this.position}" theme="simple" cssStyle="display:none" class="position"/>
</td>
<td><s:textfield name="task.steps[0].end" value="%{#this.end}" theme="simple"/></td>
<td>
<s:select list="allOpIndb" listKey="id" listValue="name" value="%{#this.operator.{id}}"
name="task.steps[0].operator.id" multiple="true" theme="simple" id="a">
</s:select>
</td>
<td>
<s:select list="@com.infomanager.entity.TaskStepStatus@values()" theme="simple"
name="task.steps[0].status" listValue="cnValue" value="%{#this.status}" id="b"/>
</td>
<td>
<span class="up">up</span>
<span class="down">down</span>
<span class="del">del</span>
</td>
<td></td>
</tr>
</s:iterator>
<tr>
<td colspan="6">
<s:submit value="Submit"></s:submit>
</td>
</tr>
</table>
</s:form>
该页面的全部代码可以找到 这里:
然后在struts2操作中,我得到了struts2创建的任务对象(“任务”对象在以下示例),并在数据库中找到正在更新的对象(以下示例中的“task_db”对象):
public String task_update{
DozerBeanMapper dbm = new DozerBeanMapper();
// the 'task' object is created by struts2
taskid = task.getId();
String name_st = task.getName();
int dep_id_st = task.getDepartment().getId();
List<TaskStep> steps_st = task.getSteps();
TaskDaoImpl tkDao = new TaskDaoImpl();
TaskStepDaoImpl tsDao = new TaskStepDaoImpl();
OperatorDaoImpl opDao = new OperatorDaoImpl();
DepartmentDaoImpl depDao = new DepartmentDaoImpl();
List<TaskStep> step_db = new ArrayList<TaskStep>();
for (TaskStep step_st : steps_st) {
int tsid = step_st.getId();
TaskStep ts_db = tsDao.queryStepById(tsid);
if (ts_db == null) {
ts_db = step_st;
} else
dbm.map(step_st, ts_db);
// sest the operators
List<Operator> ops_to_db = new ArrayList<Operator>();
for (Operator op_st : step_st.getOperator()) {
ops_to_db.add(opDao.queryOperatorById(op_st.getId()));
}
ts_db.setOperator(ops_to_db);
step_db.add(ts_db);
}
//set the id of the task have the same id with task_db,so set its id to a unimpossible value
task.setId(-100);
//set it to null!
task=null;
Task task_db = tkDao.queryTaskById(taskid);
task_db.setName(name_st);
task_db.setDepartment(depDao.queryDepartById(dep_id_st));
task_db.setSteps(step_db);
tkDao.updateTask(task_db);
}
当我提交表单时,我收到“org.hibernate.NonUniqueObjectException:”: 具有相同标识符值的不同对象已与会话关联:[com.infomanager.entity.Task#4]。
我想知道为什么?我已将任务的 id 设置为 -100,并将其设置为 null。
问题是什么?
以下是我的数据库中表格的关系:
整个项目可以在这里找到:https://github.com/hguser/TaskManager
I use hibernate as the ORM framework,and this is my first time use it for writing operation.
Befor this appliction,I just use hibernate to read data from the db.
In my struts action,I try to update a entity named "Task",this is the form in the update page:
<s:form action="task_update" namespace="/common" cssStyle="width:95%">
<s:textfield value="%{task.id}" cssStyle="display:none" name="task.id"></s:textfield>
<s:textfield name="task.name" value="%{task.name}" label="TaskName"/>
<s:select list="task.managers"
listKey="id" listValue="name" label="Manager" value="%{task.manage}" name="task.department.id">
</s:select>
<s:select list="#session.current_user.departments"
listKey="id" listValue="name" label="Departmentn of this task" value="%{task.department.{id}}" name="task.department.id">
</s:select>
<table>
<caption align="left">Steps</caption>
<tr>
<th>Name</th>
<th>End Time</th>
<th>Operators</th>
<th>Status</th>
<th>Set the order</th>
<th><span id="addStep" style="cursor:pointer" >Add Steps</span></th>
</tr>
<s:iterator value="task.steps">
<tr class="step">
<td>
<s:textfield name="task.steps[0].name" value="%{#this.name}" theme="simple"/>
<s:textfield name="task.steps[0].id" value="%{#this.id}" theme="simple" cssStyle="display:none"/>
<s:textfield name="task.steps[0].position" value="%{#this.position}" theme="simple" cssStyle="display:none" class="position"/>
</td>
<td><s:textfield name="task.steps[0].end" value="%{#this.end}" theme="simple"/></td>
<td>
<s:select list="allOpIndb" listKey="id" listValue="name" value="%{#this.operator.{id}}"
name="task.steps[0].operator.id" multiple="true" theme="simple" id="a">
</s:select>
</td>
<td>
<s:select list="@com.infomanager.entity.TaskStepStatus@values()" theme="simple"
name="task.steps[0].status" listValue="cnValue" value="%{#this.status}" id="b"/>
</td>
<td>
<span class="up">up</span>
<span class="down">down</span>
<span class="del">del</span>
</td>
<td></td>
</tr>
</s:iterator>
<tr>
<td colspan="6">
<s:submit value="Submit"></s:submit>
</td>
</tr>
</table>
</s:form>
The whole codes of this page can be found here:
Then in the struts2 action,I get the task object created by struts2(The "task" object in the following example),and find the being updated object in the db(the "task_db" object in the following example):
public String task_update{
DozerBeanMapper dbm = new DozerBeanMapper();
// the 'task' object is created by struts2
taskid = task.getId();
String name_st = task.getName();
int dep_id_st = task.getDepartment().getId();
List<TaskStep> steps_st = task.getSteps();
TaskDaoImpl tkDao = new TaskDaoImpl();
TaskStepDaoImpl tsDao = new TaskStepDaoImpl();
OperatorDaoImpl opDao = new OperatorDaoImpl();
DepartmentDaoImpl depDao = new DepartmentDaoImpl();
List<TaskStep> step_db = new ArrayList<TaskStep>();
for (TaskStep step_st : steps_st) {
int tsid = step_st.getId();
TaskStep ts_db = tsDao.queryStepById(tsid);
if (ts_db == null) {
ts_db = step_st;
} else
dbm.map(step_st, ts_db);
// sest the operators
List<Operator> ops_to_db = new ArrayList<Operator>();
for (Operator op_st : step_st.getOperator()) {
ops_to_db.add(opDao.queryOperatorById(op_st.getId()));
}
ts_db.setOperator(ops_to_db);
step_db.add(ts_db);
}
//set the id of the task have the same id with task_db,so set its id to a unimpossible value
task.setId(-100);
//set it to null!
task=null;
Task task_db = tkDao.queryTaskById(taskid);
task_db.setName(name_st);
task_db.setDepartment(depDao.queryDepartById(dep_id_st));
task_db.setSteps(step_db);
tkDao.updateTask(task_db);
}
When I submit the form,I got the "org.hibernate.NonUniqueObjectException:":
a different object with the same identifier value was already associated with the session: [com.infomanager.entity.Task#4].
I wonder why? I have set the task's id to -100,and set it to null.
What is the problem?
THe following is the realation ship of the tabls in my db:
And the whole project can be found here:https://github.com/hguser/TaskManager
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,hibernate 的意思是,您有两个具有相同标识符(相同主键)的对象,但它们不是同一个对象。
关注这篇文章也许会对你有所帮助。
NonUniqueObjectException
仅当 hibernate 找到两个时才会出现此异常会话上下文中具有相同标识符的对象。
Basically, what hibernate is saying is that you have two objects which have the same identifier (same primary key) but they are not the same object.
Follow this post may be it will help you.
NonUniqueObjectException
This exception is only there when hibernate find two objects in the session context with the same identifier.