Solved

Illegal attempt to associate a collection with two open sessions

Posted on 2014-01-27
6
3,343 Views
Last Modified: 2014-01-29
Hello there,

I am trying to update a record in one of the frames in my application and I get this error. Before it was working fine. I mean I could add and update the records in the frame. then I added the delete operation and the del is working but when I try to update I get this error. please help. I have attached my classes.

cheers
Zolf

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
=== 2014-01-28 09:45:18,612 [l0-2] DEBUG RPCManager - Content type for RPC transaction: text/plain; charset=UTF-8
=== 2014-01-28 09:45:18,612 [l0-2] DEBUG RPCManager - DMI response, dropExtraFields: true
=== 2014-01-28 09:45:18,612 [l0-2] INFO  Compression - /kaizenerpweb/sc/IDACall: 144 -> 132 bytes
      at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:432)
      at org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:66)
      at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:124)
      at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:84)
      at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:78)
      at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:146)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:316)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:246)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:112)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
      at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535)
      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527)
      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:523)
      at com.kaizen.erp.server.bussinessLogic.supplier.SupplierOrderBussinessLogic.saveSupplierOrder(SupplierOrderBussinessLogic.java:134)
      at com.kaizen.erp.server.bussinessLogic.supplier.SupplierOrderBussinessLogic.add(SupplierOrderBussinessLogic.java:110)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
SupplierOrderBussinessLogic.java
SupplierOrder.hbm.xml
0
Comment
Question by:zolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 39814820
Can you provide the code for your HibernateUtils class please?
0
 

Author Comment

by:zolf
ID: 39816908
Hi mccarl,

I have attached the file you mentioned.

cheers
Zolf
HibernateUtills.java
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39816949
Ok, I believe this error is due to the "supplierOrder" object being retrieved in the "getSupplierOrderFromRequest" method (line 108) under one session and then attempting to saveOrUpdate the same object in the "saveSupplierOrder" method (line 110) under a new session. And since your session handling seems "a bit odd" and you are only closing sessions when "session.isConnected()" is false (and I'm guessing that generally it isn't false) that the first session is still open when you are trying to manipulate your object in the second session.

So some potential fixes are... properly closing the session regardless if it is still connected may work (although that may introduce other problems, it's hard to tell with your code a bit all over the place). Otherwise, try and only open the one session for the entire request, rather than having that "add" method where you call additional methods that each opens its own session.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:zolf
ID: 39816976
thanks for your comments. but It was working properly before when I added the one to many in the SupplierOrder.hbm.xml to be able to perform delete cascade, then this issue came up in the update process.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39816987
but It was working properly before
That may well be the case, but I don't know what the "before" code/configuration looked like, I only know what you have now. In fact, you have now given a little bit more information... In the original question, you just said that you "added the delete operation" which I took to meaning that you added the code for the delete method, but now you say that you had to actually add the mapping relationships as well. That may explain what has happened...

So, "before", you had incorrect session handling code but a simpler relationship mapping defined and so despite the incorrect session handling Hibernate was probably still able to carry on regardless. Now, you have added additional relationship mappings, it is just too much for Hibernate to work out what to do in the face of the incorrect session handling and so you get the Exception that you see.

I see you have two options... 1) Remove the delete operation and the changes you made to the mapping so that you are back to where you were and you can update records (probably not the best option because I'm guessing that you really want to be able to delete stuff) or 2) fix the session handling code (as described above) and be able to update and delete stuff (the ideal solution!!)
0
 

Author Comment

by:zolf
ID: 39816996
also when I try to run a simple code like this too I get the error. I guess it has to do with this one to many which I added to my hbm.xml file

public static void main(String []args){
        SupplierOrder supplierOrder = getInstance().getSupplierOrderById(1L);
        if(supplierOrder!=null)
                getInstance().saveSupplierOrder(supplierOrder);
        else
                System.out.println("null");
}
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Apache Camel - Unable to propagate JMS Header Properties between Request - Response 3 37
Setup GlassFish 4 53
junit as external jar or library 7 55
Regarding swagger API 1 57
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question