2011-03-27 4 views
0

ORM 프레임 워크로 hibernate를 사용합니다.이 작업을 쓰기 작업에 처음 사용합니다.get NonUniqueObjectException struts2 및 hibernate를 사용하여 엔티티를 업데이트 할 때

Befor this appliction, 난 그냥 최대 절전 모드를 사용하여 데이터베이스에서 데이터를 읽습니다.

<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="@[email protected]()" 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> 

이 페이지의 전체 코드

here 찾을 수 있습니다 :

내 스트럿 활동에

, 나는 "작업"라는 이름의 개체를 업데이트하려고,이 업데이트 페이지에서 양식입니다

그런 다음 struts2 작업에서 struts2 (다음 예제의 "작업"개체)에 의해 생성 된 작업 개체를 얻고 db (다음 예제의 "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로 설정했습니다.

무엇이 문제입니까? 전체 프로젝트는 여기에서 찾을 수 있습니다 enter image description here

그리고 : 다음

내 DB에서 tabls의 realation 배이다 기본적으로 https://github.com/hguser/TaskManager

답변

0

, 무엇을 최대 절전 말하는 것은 당신이 두 개체를 가지고있는 동일한 식별자 (동일한 기본 키)를 가졌지 만 동일한 객체가 아닙니다.

이 게시물을 따라 가면 도움이 될 것입니다. NonUniqueObjectException

이 예외는 동일한 식별자로 세션 컨텍스트에서 두 개의 개체를 찾을 때만 발생합니다.

+0

나는 최대 절전 모드로 말하는 것을 알고 있으며, 디버깅을 시도하고 오류를 찾으려고 시도하지만 나는 할 수 없다. 내 애플 리케이션에서 업데이트 할 개체가 중첩 된 개체 ... ( – hguser

관련 문제