-->
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.  [ 8 posts ] 
Author Message
 Post subject: In what the problem does not work mapping ??
PostPosted: Wed Aug 29, 2007 10:43 am 
Newbie

Joined: Wed Aug 29, 2007 10:22 am
Posts: 4
In what the problem does not work mapping??

mapping (generate MyGeneration)
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Astron.DS.NHibernate.DsGroup, DS.NHibernate" table="DS_Group">

      <id name="_Groupid" type="Int32" unsaved-value="0">
      <column name="GroupID" sql-type="int"/>
         <generator class="native"/>
      </id>
    <property type="Int32" name="_Parentid">
      <column name="ParentID" sql-type="int" not-null="false"/>
    </property>
      <property type="String" name="_Name">
      <column name="Name" sql-type="nvarchar" not-null="true" length="100"/>
    </property>
    <property type="Int32" name="_Level">
      <column name="Level" sql-type="int" not-null="true"/>
    </property>
    <property type="Decimal" name="_BackOfficeid">
      <column name="BackOfficeID" sql-type="decimal" not-null="true"/>
    </property>
    <property type="DateTime" name="_UpdateTime">
      <column name="UpdateTime" sql-type="datetime" not-null="true"/>
    </property>
   </class>
</hibernate-mapping>


class for mapping (generate MyGeneration)
Code:
public class DsGroup
   {
      #region Private Members
      private bool _isChanged;
      private bool _isDeleted;
      private int? _parentid;
      private string _name;
      private int _level;
      private decimal _backofficeid;
      private DateTime _updatetime;
      private int _groupid;       
      #endregion
      
      #region Default ( Empty ) Class Constuctor
      /// <summary>
      /// default constructor
      /// </summary>
      public DsGroup()
      {
         _parentid =  null;
         _name = null;
         _level = 0;
         _backofficeid = 0;
         _updatetime = DateTime.MinValue;
         _groupid = 0;
      }
      #endregion // End of Default ( Empty ) Class Constuctor
      
      #region Internal Accessors for NHibernate
      
      /// <summary>
      ///
      /// </summary>
      internal  int? _Parentid
      {
         get { return _parentid; }
         set { _parentid = value; }
      }
      
      /// <summary>
      ///
      /// </summary>
      internal  string _Name
      {
         get { return _name; }
         set { _name = value; }
      }
      
      /// <summary>
      ///
      /// </summary>
      internal  int _Level
      {
         get { return _level; }
         set { _level = value; }
      }
      
      /// <summary>
      ///
      /// </summary>
      internal  decimal _BackOfficeid
      {
         get { return _backofficeid; }
         set { _backofficeid = value; }
      }
      
      /// <summary>
      ///
      /// </summary>
      internal  DateTime _UpdateTime
      {
         get { return _updatetime; }
         set { _updatetime = value; }
      }
      
      /// <summary>
      ///
      /// </summary>
      internal  int _Groupid
      {
         get { return _groupid; }
         set { _groupid = value; }
      }
      
      #endregion // Internal Accessors for NHibernate

      #region Public Properties
         
      /// <summary>
      ///
      /// </summary>      
      public  int? Parentid
      {
         get { return _parentid; }
         set { _isChanged |= (_parentid != value); _parentid = value; }
      }
         
      /// <summary>
      ///
      /// </summary>      
      public  string Name
      {
         get { return _name; }
         set   
         {
            if ( value != null )
               if( value.Length > 100)
                  throw new ArgumentOutOfRangeException("Invalid value for Name", value, value.ToString());
            
            _isChanged |= (_name != value); _name = value;
         }
      }
         
      /// <summary>
      ///
      /// </summary>      
      public  int Level
      {
         get { return _level; }
         set { _isChanged |= (_level != value); _level = value; }
      }
         
      /// <summary>
      ///
      /// </summary>      
      public  decimal BackOfficeid
      {
         get { return _backofficeid; }
         set { _isChanged |= (_backofficeid != value); _backofficeid = value; }
      }
         
      /// <summary>
      ///
      /// </summary>      
      public  DateTime UpdateTime
      {
         get { return _updatetime; }
         set { _isChanged |= (_updatetime != value); _updatetime = value; }
      }
         
      /// <summary>
      ///
      /// </summary>      
      public  int Groupid
      {
         get { return _groupid; }
         set { _isChanged |= (_groupid != value); _groupid = value; }
      }
         
      /// <summary>
      /// Returns whether or not the object has changed it's values.
      /// </summary>
      public bool IsChanged
      {
         get { return _isChanged; }
      }
      
      /// <summary>
      /// Returns whether or not the object has changed it's values.
      /// </summary>
      public bool IsDeleted
      {
         get { return _isDeleted; }
      }
      
      #endregion
      
      
      #region Public Functions
      
      /// <summary>
      /// mark the item as deleted
      /// </summary>
      public void MarkAsDeleted()
      {
         _isDeleted = true;
         _isChanged = true;
      }
      
      
      #endregion
      
      
      #region Equals And HashCode Overrides
      /// <summary>
      /// local implementation of Equals based on unique value members
      /// </summary>
      public override bool Equals( object obj )
      {
         if( this == obj ) return true;
         if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
         DsGroup castObj = (DsGroup)obj;
         return ( castObj != null ) &&
            ( this._groupid == castObj.Groupid );
      }
      
      /// <summary>
      /// local implementation of GetHashCode based on unique value members
      /// </summary>
      public override int GetHashCode()
      {
         
         int hash = 57;
         hash = 27 * hash * _groupid.GetHashCode();
         return hash;
      }
      #endregion
      
   }


Exception:
Quote:
Error creating context 'spring.root': Error creating object with name 'DSFromBOU
pdateDAO_SF' defined in 'file [D:\Projects\Windows\DeviceServerFull\Source\DS\bi
n\Debug\Config\DSFromBOUpdateDAO.xml]' : Initialization of object failed : The f
ollowing types may not be used as proxies:
Astron.DS.NHibernate.DsGroup: method set__UpdateTime should be virtual
Astron.DS.NHibernate.DsGroup: method set_BackOfficeid should be virtual
Astron.DS.NHibernate.DsGroup: method set_Groupid should be virtual
Astron.DS.NHibernate.DsGroup: method set__Name should be virtual
Astron.DS.NHibernate.DsGroup: method get__UpdateTime should be virtual
Astron.DS.NHibernate.DsGroup: method set_Parentid should be virtual
Astron.DS.NHibernate.DsGroup: method get_Name should be virtual
Astron.DS.NHibernate.DsGroup: method get__Level should be virtual
Astron.DS.NHibernate.DsGroup: method set__Parentid should be virtual
Astron.DS.NHibernate.DsGroup: method get_UpdateTime should be virtual
Astron.DS.NHibernate.DsGroup: method get_Level should be virtual
Astron.DS.NHibernate.DsGroup: method get_IsDeleted should be virtual
Astron.DS.NHibernate.DsGroup: method set__BackOfficeid should be virtual
...


Do not pay attention on 'spring.root' there the enclosed exceptions, source NHibernate.

Hibernate version:1.2.0.4000


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 11:08 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,

You should mark all your methods and properties as 'virtual'.

Example:

Code:
      internal virtual int? _Parentid
      {
         get { return _parentid; }
         set { _parentid = value; }
      }


That should do it.

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 4:30 pm 
Newbie

Joined: Wed Aug 29, 2007 10:22 am
Posts: 4
Tried, it is impossible. And then has closely read through Exception and has noticed, that it swears even on those properties which to 'mapping' have no relation:
Quote:
Astron.DS.NHibernate.DsGroup: method get_IsDeleted should be virtual
[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 2:46 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hello,

Why is it impossible ? (note: you cannot mark your constructor nor private methods as virtual, but it should be possible for all the rest).

It is normal that NHibernate complains about properties and methods that aren't mapped. For all lazy-loaded entities, a proxy is created that overrides all properties and methods (hence the obligation of using virtual). Even those that aren't mapped (or at least the checking mechanism isn't differentiating between the two).

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 07, 2007 5:24 pm 
Beginner
Beginner

Joined: Mon Apr 24, 2006 1:43 pm
Posts: 22
You can also put
Code:
default-lazy="false"
attribute in your hibernate-mapping. This is not recommended, but should work.
Also, you can modify your MyGeneration template to always put this attribute for you, so that you don't have to modify by hand. As an alternative, you could modify template to always create all virtual properties.

HTH, Tom


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 30, 2008 2:18 pm 
Newbie

Joined: Sun Mar 30, 2008 2:06 pm
Posts: 7
xasp wrote:
Hello,

Why is it impossible ? (note: you cannot mark your constructor nor private methods as virtual, but it should be possible for all the rest).

It is normal that NHibernate complains about properties and methods that aren't mapped. For all lazy-loaded entities, a proxy is created that overrides all properties and methods (hence the obligation of using virtual). Even those that aren't mapped (or at least the checking mechanism isn't differentiating between the two).


Hey:
that may be the solution for the given excption (which i myself just recieved), and i went to investigate because i wanted to understand the cause...
It still doesn't make it clear:
1st Why would they want properties to be virtual? So that they may create some stub that inherits from a perfectly usable class? If they want to stuff data into a Mapped class, than the class should be perfectly useble, therefore not requiring any virtual declaration.

Normally using virtual implies => someone will want to override.

2nd So if it's supposed to be virutal,then why do the tutorials not declare the properties as virtual as well?

here is a link: http://www.hibernate.org/362.html#A9

Somewhere in that page will be a grey text box with a class definition, and no virtual methods or properties!
(by the way: if they do overide the class with some sort of stub: shouldn't classe be declared as virtual? Or you don't want to do that so that you can create your own instances in save them in the data base?)

Thanks in advance.

_________________
"close this world and open the next." Serial Experiments Lain


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 3:18 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,

NHibernate overrides the properties for the lazy-loading mechanism -> it creates an empty stub class derived from your base class. Your base class will actually contain no information at that moment.
The first time you invoke one of the properties of your class, it will 'hydrate' your class with all the data. But to detect when that call occurs, it has to override all the properties to insert that mechanism.

Why the samples don't include the virtual keyword, I cannot say. But I can guess that this is because a) the samples are derived from Java and that in Java all methods are virtual by default or b) the samples are rather old (before NHibernate version 2.0, the default was lazy=false, and for non-lazy classes the properties don't have to be virtual).

(you cannot declare a virtual class -> the virtual keyword only applies to properties and methods. A class is by default overridable unless you use the 'sealed' keyword)

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 01, 2008 4:57 am 
Newbie

Joined: Sun Mar 30, 2008 2:06 pm
Posts: 7
Xasp, just to say:
thank you for your time and help, it was very enlighting!

My best regards.

_________________
"close this world and open the next." Serial Experiments Lain


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