-->
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.  [ 2 posts ] 
Author Message
 Post subject: Found two representations of same collection
PostPosted: Wed Feb 25, 2004 3:26 am 
Beginner
Beginner

Joined: Sat Jan 31, 2004 10:09 am
Posts: 26
Hi all, could anybody tell me why this occured?

I want to save an entity, but that entity was loaded in previous request cycle. I kept that entity in the Visit. And that entity has a one-to-many collection.

I will attach my session controller in the last.
----------------------------------------------------------------------
Code:
Caused by: net.sf.hibernate.HibernateException: Found two representations of same collection
net.sf.hibernate.impl.SessionImpl.updateReachableCollection(SessionImpl.java:2836)
net.sf.hibernate.impl.FlushVisitor.processCollection(FlushVisitor.java:32)
net.sf.hibernate.impl.AbstractVisitor.processValue(AbstractVisitor.java:69)
net.sf.hibernate.impl.AbstractVisitor.processValues(AbstractVisitor.java:36)
net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2556)
net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2422)
net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2224)
net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2203)
net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
hibernate.SessionUtil.executeTxn(SessionUtil.java:130)

-----------------------------------------------------------------------
Below is the source I manage session in a Servlet Filter:
I don't want to use lock() to reattach session to enttiy. I think it is hard to implement. But is it correct to just disconnect() and clear() session after I complete an request-response



Code:
public class SessionController implements Filter {

   private static ThreadLocal hSessionHolder = new ThreadLocal();
   private static SessionFactory sessionFactory;
   
   static {
      
      Configuration cfg;
      try {
         cfg = new Configuration().configure();
         sessionFactory = cfg.buildSessionFactory();
      }
      catch (HibernateException e) {
         throw new RuntimeException("Fail to configure hibernate", e);
      }
   }
   
   public static Session getSession() {
      
      Session s = (Session) hSessionHolder.get();
      if (s == null || !s.isOpen()) {
         s = initSession();
         hSessionHolder.set(s);
      }
      return s;
   }
   
   public void init(FilterConfig arg0) throws ServletException {
      
      // empty
   }

   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
      
     final String HSESSION = "hibernate.session";
    
     HttpSession httpSession = ((HttpServletRequest)request).getSession();
     Session session = (Session) httpSession.getAttribute(HSESSION);
      try {
         try {
           if (session == null) {
             session = initSession();
             httpSession.setAttribute(HSESSION, session);
           }
           else
             session.reconnect();
          
           hSessionHolder.set(session);

            chain.doFilter(request, response); //business is inside of filter chain
         }
         finally {
           session = (Session) hSessionHolder.get();
           hSessionHolder.set(null);
           if (session != null) {
             session.clear();
               session.disconnect();
           }
         }
      }
      catch (HibernateException e) {
        e.printStackTrace();
         throw new RuntimeException("Fail to close hibernate session", e);
      }
   }

   public void destroy() {
      
//      bird.destory();
   }
   
   private static Session initSession() {
      
      Session session;
      try {
         session = sessionFactory.openSession();
         return session;
      }
      catch (HibernateException e) {
        e.printStackTrace();
         throw new RuntimeException("Fail to open hibernate session.");
      }
      
   }
}
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 8:38 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You should probably close it.

But your pb is that you somehow load an entity and it session and in the same session reattache this collection.
Or you use the same collection instance on 2 differents collections
coll = new HashSet();
parent.setCollection(coll);
parent2.setCollection(coll); //this is forbidden

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.