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.  [ 10 posts ] 
Author Message
 Post subject: cascade=all-delete-orphan Exception !
PostPosted: Thu Sep 07, 2006 8:55 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:NHibernate v1.0.2[b]

[b]Full stack trace of any exception that occurs:

$exception {"You may not dereference an collection with cascade=\"all-delete-orphan\""} System.Exception {NHibernate.HibernateException}

Name and version of the database you are using:SQLEXPRESS 2005

Environment : ASP.NET 2.0 (C#)

Dear members,

I'm facing problems in the NHibernate while trying to save a specific class... i'm facing 2 potential problems :

1)I created a class in the data access layer which configure the mappping files and handles the IsessionFactory and ISession ...
I can successfully save my class into database(persisitence) when i'm defining that class'es constructor as public ..
When i'm setting the constructor as static constructor .. .an exception is loaded and saving fails:

$exception {"You may not dereference an collection with cascade=\"all-delete-orphan\""} System.Exception {NHibernate.HibernateException}

i'm using lazy=true and cascade=all-delete-orphan

what is the problem? how to fix it ?

2)The second problem i'm facing is :
when i'm updating a class into the database... all rows in that table are automaticalled loaded as instances in c# although i'm updating only one class ... which slows the application and increases the debugging time , is that normal ? is there anything to do ?


Any help would be appreciated.

Regards,
Jawad

_________________
In Code We Trust


Top
 Profile  
 
 Post subject: Re: cascade=all-delete-orphan Exception !
PostPosted: Thu Sep 21, 2006 3:15 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
jojorico wrote:

I'm facing problems in the NHibernate while trying to save a specific class... i'm facing 2 potential problems :

1)I created a class in the data access layer which configure the mappping files and handles the IsessionFactory and ISession ...
I can successfully save my class into database(persisitence) when i'm defining that class'es constructor as public ..
When i'm setting the constructor as static constructor .. .an exception is loaded and saving fails:

$exception {"You may not dereference an collection with cascade="all-delete-orphan""} System.Exception {NHibernate.HibernateException}

i'm using lazy=true and cascade=all-delete-orphan

what is the problem? how to fix it ?


NHibernate does not work with static classes. And, for class to be lasy-loadable, there must be protected or public no-argument constructor present and the class must not be sealed (lazy loading implies that a new derived class is "compiled" at runtime.)

Otherwise, I showing some relevant code might five a light what's wrong...

BTW, the error message seems to tell that your are creating a new list instance somewhere and replacing the one cerated by NHibernate... How can this be caused by adding static constructor, I have no idea...

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject: Urgent ! Plz Help :(
PostPosted: Tue Oct 10, 2006 1:00 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Quote:
Dear Gert,

I've set lazy="true" in my .hbm.xml files .. but i'm still getting all my objects loaded .. resulting the loading of all the database.
My classes have a public not argument constructors and i'm not sealing them ...
What may be the problem ?

It's really urgent .. plz help

Thanks in Advance,
Jawad


Category.cs
Code:
namespace nhExhibition.Business
{
   #region Category

   /// <summary>
   /// Category object for NHibernate mapped table 'Category'.
   /// </summary>
   public class Category : System.IComparable
      {
      #region Member Variables
      
      protected int _id;
      protected string _cat;
      protected bool _status;
      protected Category _cathasparent;
      protected IList _cathasparentCategories;
      protected IList _idcategoryiditems;
      protected static String _sortExpression = "Id";
      protected static SortDirection _sortDirection = SortDirection.Ascending;

      #endregion

      #region Constructors

      public Category() { }

      public Category( string cat, bool status, Category cathasparent )
      {
         this._cat = cat;
         this._status = status;
         this._cathasparent = cathasparent;
      }

      #endregion

      #region Public Properties

      public int Id
      {
         get {return _id;}
         set {_id = value;}
      }

      public string Cat
      {
         get { return _cat; }
         set
         {
            if ( value == null && value.Length > 255)
               throw new ArgumentOutOfRangeException("Invalid value for Cat", value, value.ToString());
            _cat = value;
         }
      }

      public bool Status
      {
         get { return _status; }
         set { _status = value; }
      }

      public Category cathasparent
      {
         get { return _cathasparent; }
         set { _cathasparent = value; }
      }

      public IList cathasparentCategories
      {
         get { return _cathasparentCategories; }
         set { _cathasparentCategories = value; }
      }

      public IList idcategoryiditems
      {
         get
         {
            if (_idcategoryiditems==null)
               {
               _idcategoryiditems = new ArrayList();
            }
            return _idcategoryiditems;
         }
         set { _idcategoryiditems = value; }
      }

        public static String SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

        public static SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }
      #endregion
      
        #region IComparable Methods
        public int CompareTo(object obj)
        {
         if (!(obj is Category))
            throw new InvalidCastException("This object is not of type Category");
         
         int relativeValue;
         switch (SortExpression)
         {
            case "Id":
               relativeValue = this.Id.CompareTo(((Category)obj).Id);
               break;
            case "Cat":
               relativeValue = this.Cat.CompareTo(((Category)obj).Cat);
               break;
            case "Status":
               relativeValue = this.Status.CompareTo(((Category)obj).Status);
               break;
                default:
                    goto case "Id";
         }
            if (Category.SortDirection == SortDirection.Ascending)
            relativeValue *= -1;
         return relativeValue;
      }
      #endregion
   }

   #endregion
}

Category.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="nhExhibition.Business.Category, nhExhibition.Business" table="Category">
      <id name="Id" type="Int32" unsaved-value="0">
         <column name="idcategory" length="4" sql-type="int" not-null="true" unique="true" index="PK_Category"/>
         <generator class="native" />
      </id>
      <property name="Cat" type="String">
         <column name="cat" length="255" sql-type="varchar" not-null="true"/>
      </property>
    <property name="Status" type="Boolean">
      <column name="status" length="1" sql-type="bit" not-null="true"/>
    </property>
    <many-to-one name="cathasparent" class="nhExhibition.Business.Category, nhExhibition.Business">
      <column name="cathasparent" length="4" sql-type="int" not-null="false"/>
    </many-to-one>
    <bag name="cathasparentCategories" inverse="true" lazy="true">
      <key column="cathasparent"/>
      <one-to-many class="nhExhibition.Business.Category, nhExhibition.Business"/>
    </bag>
      <bag name="idcategoryiditems" table="Item_Category" inverse="false" lazy="true">
         <key>
            <column name="idcategory" length="4" sql-type="int" not-null="true"/>
         </key>
         <many-to-many class="nhExhibition.Business.Item, nhExhibition.Business">   
            <column name="iditem" length="4" sql-type="int" not-null="true"/>
         </many-to-many>
      </bag>
   </class>
</hibernate-mapping>

_________________
In Code We Trust


Top
 Profile  
 
 Post subject: Re: Urgent ! Plz Help :(
PostPosted: Tue Oct 10, 2006 3:02 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
jojorico wrote:
Quote:
Dear Gert,

I've set lazy="true" in my .hbm.xml files .. but i'm still getting all my objects loaded .. resulting the loading of all the database.
My classes have a public not argument constructors and i'm not sealing them ...
What may be the problem ?


The code and mapping seems to be Ok. How Do You determine that it is no lazy-loading?

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject: NO Lazy-Loading !
PostPosted: Tue Oct 10, 2006 5:54 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Dear Gert,

When i'm debugging my code, i notice the following:

When loading a Category Object from the db, its corresponding items are loaded which is natural and normal ...
But what is not normal is that our category's items , when loaded , contain collection of other objects --> so these objects are also loaded and so on ...

_________________
In Code We Trust


Top
 Profile  
 
 Post subject: Re: NO Lazy-Loading !
PostPosted: Tue Oct 10, 2006 6:32 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
jojorico wrote:
Dear Gert,

When i'm debugging my code, i notice the following:

When loading a Category Object from the db, its corresponding items are loaded which is natural and normal ...
But what is not normal is that our category's items , when loaded , contain collection of other objects --> so these objects are also loaded and so on ...

Well, inspecting object in debugger also triggers lazy loading.. Better inspect SQL server activity.

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject: LOL
PostPosted: Tue Oct 10, 2006 7:24 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Quote:
Dear Gert,

I'm using the sqlserver express2005 ..
What did u mean by "inspect sqlserver activity " ... How can i inspect sqlserver activity ?
Does that mean that the Lazy loading is done well ? If so, How this can be while i'm getting all the collections loaded.

Thanks A Lot for your help and support

Regards,
Jojo

_________________
In Code We Trust


Top
 Profile  
 
 Post subject: Re: LOL
PostPosted: Tue Oct 10, 2006 7:47 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
jojorico wrote:
I'm using the sqlserver express2005 ..
What did u mean by "inspect sqlserver activity " ... How can i inspect sqlserver activity ?


SQL server profiler AFAIK.

Or set show_sql=true in NHIbernate configuration and inspect the cosnole output of program.

jojorico wrote:
Does that mean that the Lazy loading is done well ?


No, it mean You can not use debugger to determine if the collection is lazy loaded or not.
jojorico wrote:
If so, How this can be while i'm getting all the collections loaded.


The question is still: How do You know that all collections are loaded?

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject: Debuggings !
PostPosted: Fri Oct 13, 2006 3:37 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Dear Gert,

Quote:
I know that the collections are loading cause when i set some breakpoints @ the loading of a set(list) of objects ( session.CreateCriteria(typeof(DATATUTORIALS.BLL.DEPARTMENT).TOLIST()) , i'm getting all the objects loaded ...

That is not the problem but suppose Department contains a list of UniversityClasses and UniversityClasses contains a collection(list) of students, in the debugging, i'm getting all the Departments loaded and all the UniversityClasses Loaded which is normal ...
but also the collections inside UniversityClasses are also loaded and so on ...

Another thing, when i'm writing my owwn HQL,ex :

IList retrieveddept =
Code:
session.Find(" Select dept.name,dept.id From DATATUTORIALS.BLL.DEPARTMENT as dept");


i want to use the retrieved list of department to databind it to a GRIDVIEW, the structure of the IList does not permit that ...
i'm getting some kind of private members in the IList , what is the problem in that ?
Am i missing somethin ?
is there any alternative !


p.s:is there any sample application handling relationship (delete objects and relations, insert and update) .. i've worked on Justin Gehtland Application and a lot other Nhibernate sample applications but none of them gives u what u really need in real applications

thanks a lot for your time and support.

Regards,
Jojo

_________________
In Code We Trust


Top
 Profile  
 
 Post subject: ISSUE RESOLVED ?
PostPosted: Mon Oct 16, 2006 4:49 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
dear Gert,

i''m using a dbManager class which loads the configuration and i'm opening 1 session abd connecting and disconnecting to that session .

does that affect the nhibernate lazy loading ?

p.s:i'm using session.connect() and session.disconnect() in order to minimize the access to the database, is this true ?
or is the connection to the database opened in both cases ( if session is connected or not) ?

how can i optimize .

This is some of the code :

Code:
        static Configuration config;
        static ISessionFactory factory;
        static ISession session;

        static DBHandler()
        {
            config = new Configuration();
            config.AddClass(typeof(DataTutorials.BLL.Department));
            //config.AddClass(typeof(DataTutorials.BLL.Student));
            //config.AddClass(typeof(DataTutorials.BLL.Professor));
            config.AddClass(typeof(DataTutorials.BLL.Person));
            config.AddClass(typeof(DataTutorials.BLL.UniversityClass));

            try
            {
                factory = config.BuildSessionFactory();
                session = factory.OpenSession();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                string errormsg = ex.Message;
                throw new ArgumentOutOfRangeException("DBHandler - ERROR IN THE CONSTRUCTOR ", ex, ex.Message);
            }
        }

        public IList GetDepartments()
        {
            session.Reconnect();
            ITransaction tx= session.BeginTransaction();
            IList retrievedDepts=null;
            try
            {
                retrievedDepts = session.CreateCriteria(typeof(DataTutorials.BLL.Department)).List();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                string errormsg = ex.Message;
                throw new ArgumentOutOfRangeException("DBHandler - ERROR IN getDepartments ", ex, ex.Message);
            }
            session.Disconnect();
            return retrievedDepts;
        }
        public Department GetDepartment(int deptID)
        {
            session.Reconnect();
            ITransaction tx = session.BeginTransaction();
            Department retrievedDept = null;
            try
            {
                retrievedDept = (Department)session.Load(typeof(DataTutorials.BLL.Department), deptID);
            }
            catch (Exception ex)
            {
                tx.Rollback();
                throw new ArgumentOutOfRangeException("DBHandler - ERROR IN Getting Department   ", ex, ex.Message);
            }
            session.Disconnect();
            return retrievedDept;
        }
        public void SaveDepartment(Department dept)
        {
            session.Reconnect();
            ITransaction tx = session.BeginTransaction();

            try
            {
                session.SaveOrUpdate(dept);
                tx.Commit();
            }
            catch(Exception ex)
            {
                tx.Rollback();
                throw new ArgumentOutOfRangeException("DBHandler - ERROR IN Save Department ", ex, ex.Message);
            }
            session.Disconnect();
        }
        public void DeleteDepartment(Department dept)
        {
            session.Reconnect();
            ITransaction tx = session.BeginTransaction();
            try
            {
                //session.deleteItem(Item);
                session.Delete(dept);
                tx.Commit();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                throw new ArgumentOutOfRangeException(ex.Message, ex, ex.Message);
            }
            session.Disconnect();
        }


i would be glad to hear ur comments and suggestions .
Regards,
Jojo

_________________
In Code We Trust


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