-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Multiple updates in a session?
PostPosted: Fri Sep 26, 2008 2:02 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
I'm using Spring 2.0.8 and Hibernate 3.3+, an AnnotatedSessionFactoryBean, MySQL 5, Firefox, Vista, and a keyboard.

I'm using the HibernateDaoSupport class as a base for my DAO objects. What I need to know is how to use more than one operation in a method. I need to fetch an object, perform some modification on it, then update it to the database. When I try doing this, I get this error:

Code:
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
   at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:410)
   at org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:43)
   at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
   at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
   at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
   at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:123)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:293)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
   at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:33)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:564)
   at org.hibernate.impl.SessionImpl.update(SessionImpl.java:552)
   at org.hibernate.impl.SessionImpl.update(SessionImpl.java:544)
   at com.tkassembled.database.UserDAOImpl.registerUser(UserDAOImpl.java:171)
   at com.tkassembled.service.HibernateServiceImpl.registerUser(HibernateServiceImpl.java:99)
   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:597)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
   at $Proxy28.registerUser(Unknown Source)
   at com.tkassembled.RemotingHandler.registerUser(RemotingHandler.java:129)
   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:597)
   at org.red5.server.service.ServiceInvoker.invoke(ServiceInvoker.java:217)
   at org.red5.server.service.ServiceInvoker.invoke(ServiceInvoker.java:123)
   at org.red5.server.net.rtmp.RTMPHandler.invokeCall(RTMPHandler.java:157)
   at org.red5.server.net.rtmp.RTMPHandler.onInvoke(RTMPHandler.java:409)
   at org.red5.server.net.rtmp.BaseRTMPHandler.messageReceived(BaseRTMPHandler.java:143)
   at org.red5.server.net.rtmp.RTMPMinaIoHandler.messageReceived(RTMPMinaIoHandler.java:119)
   at org.apache.mina.common.support.AbstractIoFilterChain$TailFilter.messageReceived(AbstractIoFilterChain.java:570)
   at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageReceived(AbstractIoFilterChain.java:299)
   at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(AbstractIoFilterChain.java:53)
   at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageReceived(AbstractIoFilterChain.java:648)
   at org.apache.mina.filter.executor.ExecutorFilter.processEvent(ExecutorFilter.java:220)
   at org.apache.mina.filter.executor.ExecutorFilter$ProcessEventsRunnable.run(ExecutorFilter.java:264)
   at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
   at java.lang.Thread.run(Thread.java:619)


My code kinda looks like this:

Code:
Criteria criteria = this.getSession.createCriteria(User.class);
criteria.add(Restrictions.eq("id", id));
User user = (User) criteria.uniqueResult();
if (!user.equals(null)) {
  user.setActive(true);
  this.getHibernateTemplate().update(user);
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 2:15 pm 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
Quote:
I'm using Spring 2.0.8 and Hibernate 3.3+, an AnnotatedSessionFactoryBean, MySQL 5, Firefox, Vista, and a keyboard.


I think your problem is that you're using Vista. And without a mouse!

But seriously. I'm not sure what getHibernateTemplate() does. Is it possible that it opens a new session? Does getSession.update(user); not work?

Also, if you're retrieving using a primary key you can use .get() or .load() instead of .uniqueResult()


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 2:27 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
I just found my mouse. It helps sometimes. I prefer to code in command prompt though, so it's not too much help all of the time... but Vista isn't too bad. It's an operating system, it has its ups and downs like any other. I'm just too lazy to fire up an Ubuntu machine even though I really want to check out linux.

getHibernateTemplate() returns a HibernateTemplate instance auto-loaded by Spring. It abstracts the use of sessions away from developers so all you gotta do is call this.getHibernateTemplate().persist(value) rather than create the session, start a transaction, click your heels three times...
Anyway, whenever I call getHibernateTemplate().* more than once in a method block, I get a weird error. I'm thinking that it only allows you to do one transaction with the hibernateTemplate object. I'm not sure. In my service layer, I can't start an update in any way, it's really weird.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 2:42 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
Even if I explicitly call getSession().disconnect() and getSession().close() I'm still getting this error? Weird. Has anyone else dealt with this directly?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 3:47 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
Everything I know is wrong.

getHibernateTemplate() seems to be opening up a new Session every time I call it, so I had to workaround by using the actual Sessions and Transactions.

phen0m, thanks for helping me process my insanity.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.