论坛首页 Java企业应用论坛

关于多重映射问题,很奇怪

浏览 3450 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2003-12-15  
代码:
   int testerId = 1;
    int developerId = 2;
   //查出一条BUG
    Bug bug = hjdo.selectBugByName("bug1");

   //向项目添加一条BUG
    Project project = hjdo.selectProjectById(projectId);
    Set bugs = project.getBugs();
    bugs.add(bug);
    project.setBugs(bugs);
    hjdo.updateProject(project, projectId);//更新项目
   
    //向测试人员添加一条BUG
    User tester = hjdo.selectUserById(userId1);
    Set testerBugs = tester.getBugs();
    testerBugs.add(bug);
    tester.setBugs(user1Bugs);
    hjdo.updateUser(tester, testerId);// 更新用户(测试员)
    //向开发人员添加一条BUG
    User developer = hjdo.selectUserById(developerId);
    Set developerBugs = developer.getBugs();
    developerBugs .add(bug);
    developer.setBugs(developerBugs );
    hjdo.updateUser(developer, developerId);//更新用户(开发员)
我的Bug,User,Project之间都是多对多的关系映射,当扫行更新操作时,却出现问题:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 229377, of class: org.arden.bugok.jdo.beans.Bug

at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1605)

at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1377)

at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1403)

at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1327)

at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)

at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)

at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)

at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)

at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)

at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)

at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1408)

at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1327)

at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)

at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)

at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)

at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)

at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)

at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)

at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1408)

at net.sf.hibernate.impl.SessionImpl.update(SessionImpl.java:1362)

at org.arden.bugok.jdo.MsSQLServerHjdo.updateUser(MsSQLServerHjdo.java:452)

at org.arden.bugok.test.TestHjdo.testUpdateProject(TestHjdo.java:82)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:324)

at junit.framework.TestCase.runTest(TestCase.java:154)

at junit.framework.TestCase.runBare(TestCase.java:127)

at junit.framework.TestResult$1.protect(TestResult.java:106)

at junit.framework.TestResult.runProtected(TestResult.java:124)

at junit.framework.TestResult.run(TestResult.java:109)

at junit.framework.TestCase.run(TestCase.java:118)

at junit.framework.TestSuite.runTest(TestSuite.java:208)

at junit.framework.TestSuite.run(TestSuite.java:203)

at com.borland.jbuilder.unittest.JBTestRunner.run(JBTestRunner.java:210)

at com.borland.jbuilder.unittest.JBTestRunner.initiateTest(JBTestRunner.java:252)

at com.borland.jbuilder.unittest.JBTestRunner.main(JBTestRunner.java:558)
   发表时间:2004-02-11  
1.当Project project = hjdo.selectProjectById(projectId)时,可能会同时load属于该project的所有
bugs.这时候,这些bugs与当前session建立联系.
2. 当User tester = hjdo.selectUserById(userId1)时,也可能会同时load属于该project的所有
bugs.这时候,这些bugs与当前session建立联系.
   在当前session下,如果2取出的bug有与1取出的bug相同的话,就会报 different object with the same identifier value was already associated with the session的错误.因此建议完成project操作后关闭session. 新开一个session在处理user或developer操作.
0 请登录后投票
   发表时间:2004-03-29  
我也碰到同样的问题,不知道如何处理!现在的心情很郁闷!真想放弃了它!可又不甘心!
0 请登录后投票
   发表时间:2004-03-29  
jimlaren 写道
我也碰到同样的问题,不知道如何处理!现在的心情很郁闷!真想放弃了它!可又不甘心!


搂主的问题完全是因为他程序的逻辑不对而产生的,就如民工叔叔所说的,把已经发生关联的bug和project对象重新建立关联,出错是可以遇见的。我不知道这段程序的目的是什么,如果要插入新的关联则需要创建一个新的bug实例。
0 请登录后投票
   发表时间:2004-03-29  
我说说我的情况吧!
我的表是多对多关联!
schema如下
drop table if exists course_employee_link
drop table if exists employee
drop table if exists course
create table course_employee_link (
   employee_id BIGINT not null,
   course_id BIGINT not null,
   primary key (course_id, employee_id)
)
create table employee (
   employee_id BIGINT not null,
   email VARCHAR(30),
   password VARCHAR(30),
   user_name VARCHAR(30),
   primary key (employee_id)
)
create table course (
   course_id BIGINT not null,
   course_description VARCHAR(255),
   course_name VARCHAR(30),
   quantity INTEGER,
   sign_up_quantity INTEGER,
   primary key (course_id)
)
alter table course_employee_link add index (course_id), add constraint FKA34265C7575ED7BF foreign key (course_id) references course (course_id)
alter table course_employee_link add index (employee_id), add constraint FKA34265C7367BE44C foreign key (employee_id) references employee (employee_id)

具体的处理函数:
public ActionForward performSignIn(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
CourseForm courseForm = (CourseForm) form;
//get session
HttpSession httpSession = request.getSession();
//get employee
Employee employee =
(Employee) httpSession.getAttribute(IntelConstants.EMPLOYEE_KEY);
if (null == employee) {
return mapping.findForward(IntelConstants.GLOBAL_FORWARD_LOGON);
} else {
//get course
Course course =
getControl().findByPrimaryKey(courseForm.getCourseId());//getControl() return a dao object
//get employee set
Set employees = course.getEmployees();
//add employee to set
employees.add(employee);
course.setSignUpQuantity(employees.size());
//update course
if (getControl().updateCourse(course)) {
transferProperties(courseForm, course, true);
return this.performDetail(
mapping,
courseForm,
request,
response);
} else {
return mapping.findForward(IntelConstants.GLOBAL_ERROR);
}

}
}
具体的错误信息:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 2, of class: com.edynasty.intel.employee.jdo.Employee
at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1642)
at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1414)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1440)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1364)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1445)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1364)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1445)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1364)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1445)
at net.sf.hibernate.impl.SessionImpl.update(SessionImpl.java:1326)
at com.edynasty.intel.course.dao.impl.CourseDAOImpl.updateCourse(CourseDAOImpl.java:50)
at com.edynasty.intel.course.control.CourseControl.updateCourse(CourseControl.java:62)
at com.edynasty.intel.course.action.CourseAction.performSignIn(CourseAction.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at com.edynasty.filter.CompressionFilter.doFilter(CompressionFilter.java:80)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:688)
at java.lang.Thread.run(Thread.java:534)
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics