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.  [ 1 post ] 
Author Message
 Post subject: Is there better way than this to find unreferenced objects?
PostPosted: Fri Sep 07, 2007 5:59 am 
Newbie

Joined: Fri Jun 08, 2007 5:21 am
Posts: 2
Is there better way to find that object isn't been referenced by any other object than this?

Right now i'm finding all object that have Attachment list and then doing subqueries to see if attachment is in their list or not.

Code:
public static void DeleteOrphanAttachments(ISession s)
      {
         IDictionary classes = s.SessionFactory.GetAllClassMetadata();

         List<Type> queryTypes = new List<Type>();
         List<string> propertyNames = new List<string>();

         foreach (DictionaryEntry entry in classes)
         {
            SingleTableEntityPersister abstractClass = (SingleTableEntityPersister)entry.Value;
            for (int i = 0; i < abstractClass.PropertyTypes.Length; i++)
            {
               if (abstractClass.PropertyTypes[i].ReturnedClass.UnderlyingSystemType.Equals(typeof(IList<Attachment>)))
               {
                  bool insert = true;
                  if (abstractClass.MappedSuperclass != null)
                  {
                     SingleTableEntityPersister superClass = (SingleTableEntityPersister)classes[abstractClass.MappedSuperclass];
                     if (superClass.TableName == abstractClass.TableName)
                     {
                        foreach (string name in superClass.PropertyNames)
                        {
                           if (name == abstractClass.PropertyNames[i])
                           {
                              insert = false;
                           }
                        }
                     }
                  }
                  if (insert)
                  {
                     queryTypes.Add((Type)entry.Key);
                     propertyNames.Add(abstractClass.PropertyNames[i]);
                  }
               }
            }
         }

         if (queryTypes.Count > 0)
         {
            ICriteria crit = s.CreateCriteria(typeof(Attachment), "attachment");

            for (int i = 0; i < queryTypes.Count; i++)
            {
               DetachedCriteria detachedQuery = DetachedCriteria.For(queryTypes[i])
                  .SetProjection(Projections.RowCount())
                  .CreateAlias(propertyNames[i], propertyNames[i].ToLower())
                  .Add(Property.ForName(propertyNames[i].ToLower() + ".Id").EqProperty("attachment.Id"));
               crit = crit.Add(Subqueries.Eq(0, detachedQuery));
            }
            
            IList<Attachment> attachments = crit.List<Attachment>();

            foreach (Attachment att in attachments)
            {
               s.Delete(att);
            }
         }
      }


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

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.