-->
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.  [ 4 posts ] 
Author Message
 Post subject: bad one-to-one SQL
PostPosted: Wed Jul 19, 2006 10:30 pm 
Newbie

Joined: Fri Apr 01, 2005 5:20 pm
Posts: 14
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 1.0.2.0


I have a one-to-one or one-to-none situation with menu to protracforms. When I select the menu Object based on the MenuGroup I get the following SQL. It all looks good except for this.ItemID=protracfor2_.FormID. It should read this.FormID=protracfor2_.FormID

NHibernate: SELECT this.ItemID as ItemID2_, this.Type as Type2_, this.SortOrder as SortOrder2_, this.MenuGroupID as MenuGrou2_2_, this.Description as Descript3_2_, this.ItemName as ItemName2_, menugroup1_.MenuGroupID as MenuGrou1_0_, menugroup1_.Description as Descript2_0_, protracfor2_.FormID as FormID1_, protracfor2_.FormName as FormName1_, protracfor2_.Category as Category1_ FROM ProTrac.dbo.Menu this left outer join ProTrac.dbo.MenuGroup menugroup1_ on this.MenuGroupID=menugroup1_.MenuGroupID left outer join ProTrac.dbo.Form protracfor2_ on this.ItemID=protracfor2_.FormID WHERE this.MenuGroupID = @p0
@p0 = '1'


Here is my xml and the parts of my classes that I think matter.

Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="DTP.Data.Menu,DTP" table="Menu">
      <id name="Id" column="ItemID" type="Int64" >
         <generator class="native"/>
      </id>
    <many-to-one name="MenuGroup" column="MenuGroupID"  class="DTP.Data.MenuGroup,DTP" />
      <property column="Description"  name="Description" not-null="true" length="50" />
      <property column="SortOrder"  name="SortOrder" />
      <property column="Type"  name="Type" not-null="true" length="50" />
      <property column="ItemName" name="ItemName" length="50" />
    <one-to-one name="ProTracForm" foreign-key="FormID" class="DTP.Data.ProTracForm,DTP" "/>
   </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="DTP.Data.ProTracForm,DTP" table="Form">
      <id name="Id" column="FormID" type="Int64">
         <generator class="native"/>
      </id>
      <bag name="LabelFormList" inverse="true" lazy="true" >
         <key column="FormID" />
         <one-to-many class="DTP.Data.LabelForm,DTP" />
      </bag>
      <bag name="RoleFormList" inverse="true" lazy="true" >
         <key column="FormID" />
         <one-to-many class="DTP.Data.RoleForm,DTP" />
      </bag>
    <one-to-one name="Menu" class="DTP.Data.Menu,DTP" constrained="true" />
      <property column="FormName" type="String" name="FormName" not-null="true" length="30" />
    <property column="Category" type="Byte" name="Category" />
   </class>
</hibernate-mapping>

public sealed class ProTracForm 
   {

        private Menu m_menu;


        public Menu Menu
        {
            get { return m_menu; }
            set
            {
                m_isChanged |= (m_menu != value);
                m_menu = value;
            }
        }
        public override string ToString()
        {
            return m_formname;
        }
…   
}




   
    public sealed class Menu : BaseObject
   {

      #region Private Members
      private bool m_isChanged;
         private ProTracForm m_form;
          private MenuGroup m_menugroup;
      private string m_description;
      private int m_sortorder;
      private string m_type;
      private string m_itemname;       
      #endregion

      #region Default ( Empty ) Class Constuctor
      /// <summary>
      /// default constructor
      /// </summary>
      public Menu()
      {

      }
      #endregion // End of Default ( Empty ) Class Constuctor

      #region Public Properties
         
      /// <summary>
      ///
      /// </summary>

        public override long Id
        {
            get { return m_id; }
            set { m_id = value; }
        }
         
      /// <summary>
      ///
      /// </summary>
        public MenuGroup MenuGroup
        {
            get { return m_menugroup; }
            set
            {
                if (value == null)
                    throw new ArgumentOutOfRangeException("Null value not allowed for Group", value, "null");

                m_isChanged |= (m_menugroup != value);
                m_menugroup = value;
            }

        }

        /// <summary>
        ///
        /// </summary>
        public ProTracForm ProTracForm
        {
            get { return m_form; }
            set
            {
                m_isChanged |= (m_form != value);
                m_form = value;
            }

        }
         
      public bool IsChanged
      {
         get { return m_isChanged; }
      }
            
      #endregion


   }



_________________
Thanks,

John


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 10:49 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
To do that you need to use property-ref on the one-to-one. That's not in NHibernate. So change from using one-to-one to many-to-one unique="true" property-ref="FormID". You'll need to add a FormID property for that to work.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 20, 2006 2:09 pm 
Newbie

Joined: Fri Apr 01, 2005 5:20 pm
Posts: 14
tenwit wrote:
To do that you need to use property-ref on the one-to-one. That's not in NHibernate. So change from using one-to-one to many-to-one unique="true" property-ref="FormID". You'll need to add a FormID property for that to work.


Hey,

Thanks for the quick reply. I have made some head way but still missing something.

I changed the menu.hbm.xml to many-to-one and got some SQL that is closer.



The SQL now has the ProTracForm object where I was expecting the FormID. Here is the SQL that is generated.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="DTP.Data.Menu,DTP" table="Menu">
      <id name="Id" column="ItemID" type="Int64" >
         <generator class="native"/>
      </id>
    <many-to-one name="MenuGroup" column="MenuGroupID"  class="DTP.Data.MenuGroup,DTP" />
      <property column="Description"  name="Description" not-null="true" length="50" />
      <property column="SortOrder"  name="SortOrder" />
      <property column="Type"  name="Type" not-null="true" length="50" />
      <property column="ItemName" name="ItemName" length="50" />
    <many-to-one name="ProTracForm" unique="true"   class="DTP.Data.ProTracForm,DTP"  />
   </class>
</hibernate-mapping>


Code:
NHibernate: SELECT this.ItemID as ItemID2_, this.Type as Type2_, this.SortOrder as SortOrder2_, this.MenuGroupID as MenuGrou2_2_, this.Description as Descript3_2_, this.ItemName as ItemName2_, this.ProTracForm as ProTracF7_2_, menugroup1_.MenuGroupID as MenuGrou1_0_, menugroup1_.Description as Descript2_0_, protracfor2_.FormID as FormID1_, protracfor2_.FormName as FormName1_, protracfor2_.Category as Category1_ FROM ProTrac.dbo.Menu this left outer join ProTrac.dbo.MenuGroup menugroup1_ on this.MenuGroupID=menugroup1_.MenuGroupID left outer join ProTrac.dbo.Form protracfor2_ on [b]this.ProTracForm=protracfor2_.FormID[/b] WHERE this.MenuGroupID = @p0
@p0 = '1'


You said to put in property-ref="FormID" but I am not sure about doing that. Do you mean to add a FormID to my Menu object along side the ProTracForm? That would give me this XML
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="DTP.Data.Menu,DTP" table="Menu">
      <id name="Id" column="ItemID" type="Int64" >
         <generator class="native"/>
      </id>
    <many-to-one name="MenuGroup" column="MenuGroupID"  class="DTP.Data.MenuGroup,DTP" />
      <property column="Description"  name="Description" not-null="true" length="50" />
      <property column="SortOrder"  name="SortOrder" />
      <property column="Type"  name="Type" not-null="true" length="50" />
      <property column="ItemName" name="ItemName" length="50" />
    <many-to-one name="ProTracForm" unique="true"  property-ref="FormID"  class="DTP.Data.ProTracForm,DTP"  />
   </class>
</hibernate-mapping>


then it says I don't have a FormID in the ProTracForm, so I used my primary key that is already there "Id" and it can not find that.

Reading the docs, I thought the property-ref was for when you have a FK that is not the PK of that foriegn object.

Thanks for the help.

_________________
Thanks,

John


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 20, 2006 2:37 pm 
Newbie

Joined: Fri Apr 01, 2005 5:20 pm
Posts: 14
Hey,

I changed the xml mappings to include the column so I ended up with this.

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="DTP.Data.Menu,DTP" table="Menu">
      <id name="Id" column="ItemID" type="Int64" >
         <generator class="native"/>
      </id>
    <many-to-one name="MenuGroup" column="MenuGroupID"  class="DTP.Data.MenuGroup,DTP" />
      <property column="Description"  name="Description" not-null="true" length="50" />
      <property column="SortOrder"  name="SortOrder" />
      <property column="Type"  name="Type" not-null="true" length="50" />
      <property column="ItemName" name="ItemName" length="50" />
    <many-to-one name="ProTracForm" unique="true" column="FormID" class="DTP.Data.ProTracForm,DTP"  />
   </class>
</hibernate-mapping>


That gives me the correct SQL.

Of course now I have just moved on to my next issue so :)

Thanks for the help!

_________________
Thanks,

John


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