-->
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: Odd problem: one-to-one lazy="false" property not
PostPosted: Fri Aug 17, 2007 11:33 am 
Newbie

Joined: Mon Aug 13, 2007 8:49 pm
Posts: 9
Guys, I've got a head scratcher here that doesn't make any sense to me...

Here's the problem:

Code:
NHibernate.ISession session = NHibernate.NHibernateHttpModule.CurrentSession;

NHibernate.IQuery query = session.CreateQuery("from prf_ProfileItemTitle");

foreach(prf_ProfileItemTitle itemTitle in query.List())
{
     // sometimes ItemAdapter is null! should never be according to the data...
     Debug.WriteLine(string.Format("prf_ProfileItemTitle.ItemAdapter.Id = {0}", itemTitle.ItemAdapter.Id));

}



When iterating the section.ProfileItemTitles collection, some of the prf_ProfileItemTitle.ItemAdapter references are null! In the database, every row for prf_ProfileItemTitle has a non-null int with referential integrity pointing to a valid ItemAdapterID row in prf_ItemAdapter.

With the following data:
The database table "prf_ProfileItemTitle" has rows like so:
Code:
prf.ProfileItemTitle table:
TitleID     SectionID   TitleName                                          ItemAdapterID
----------- ----------- -------------------------------------------------- -------------
11          1           Gender                                             10
12          1           BirthDate                                          1
13          1           FirstName                                          1
21          1           MiddleName                                         1
22          1           LastName                                           1

prf.ItemAdapter table:
ItemAdapterID AdapterName
------------- --------------------------------------------------
1             Short Answer
2             Essay
3             Select One
4             Scriptures
5             Address
6             CommDevice
9             DropDownList
10            HrzRadioButtonList
11            CheckBoxList
12            RadioButtonListColumns


Notice that the rows of ItemAdapterID's in the prf_ProfileItemTitle table are 10, 1, 1, 1, 1.

When iterating the collection of returned entities from NHib, the first TWO prf_ProfileItemTitle entities HAVE the correct prf_ItemAdapter entity set to the property, BUT the third,fourth,fifth, which ALL contain the SAME entity as the SECOND do not! It's very bizarre, and I do not understand why this could be...

(Next post will contain DDL to recreate the scenario)


Last edited by enewton76 on Fri Aug 17, 2007 12:13 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 11:34 am 
Newbie

Joined: Mon Aug 13, 2007 8:49 pm
Posts: 9
(Create a new Database called LightsCore)

DDL:
Code:
use LightsCore
go
CREATE SCHEMA prf
GO
/****** Object:  Table [prf].[ItemAdapter]    Script Date: 08/17/2007 11:24:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [prf].[ItemAdapter](
   [ItemAdapterID] [int] IDENTITY(1,1) NOT NULL,
   [AdapterName] [varchar](50) NOT NULL
CONSTRAINT [PK_ItemTitleAdapter] PRIMARY KEY CLUSTERED
(
   [ItemAdapterID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [prf].[ProfileItemTitle]    Script Date: 08/17/2007 11:24:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
/* NOTE THAT TitleID is NOT a sql generated identity, in the true DB this ID comes from another "MASTER TITLES" type of table */
CREATE TABLE [prf].[ProfileItemTitle](
   [TitleID] [int] NOT NULL,
   [SectionID] [int] NOT NULL,
   [ItemAdapterID] [int] NOT NULL,
   [TitleName] [varchar](50) NOT NULL
CONSTRAINT [PK_MemberProfileArea] PRIMARY KEY CLUSTERED
(
   [TitleID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Title of items within group' , @level0type=N'SCHEMA',@level0name=N'prf', @level1type=N'TABLE',@level1name=N'ProfileItemTitle'
GO
ALTER TABLE [prf].[ProfileItemTitle]  WITH CHECK ADD  CONSTRAINT [FK_ProfileItemTitle_ItemAdapter] FOREIGN KEY([ItemAdapterID])
REFERENCES [prf].[ItemAdapter] ([ItemAdapterID])
GO
ALTER TABLE [prf].[ProfileItemTitle] CHECK CONSTRAINT [FK_ProfileItemTitle_ItemAdapter]

/* build data */
set identity_insert prf.ItemAdapter on
insert into prf.ItemAdapter ( ItemAdapterID, AdapterName ) values ( 1, 'Short Answer' );
insert into prf.ItemAdapter ( ItemAdapterID, AdapterName ) values ( 2, 'Essay' );
insert into prf.ItemAdapter ( ItemAdapterID, AdapterName ) values ( 3, 'Select One' );
insert into prf.ItemAdapter ( ItemAdapterID, AdapterName ) values ( 4, 'Scriptures' );
insert into prf.ItemAdapter ( ItemAdapterID, AdapterName ) values ( 5, 'Address' );
insert into prf.ItemAdapter ( ItemAdapterID, AdapterName ) values ( 6, 'CommDevice' );
insert into prf.ItemAdapter ( ItemAdapterID, AdapterName ) values ( 9, 'DropDownList' );
insert into prf.ItemAdapter ( ItemAdapterID, AdapterName ) values ( 10, 'HrzRadioButtonList' );
insert into prf.ItemAdapter ( ItemAdapterID, AdapterName ) values ( 11, 'CheckBoxList' );
insert into prf.ItemAdapter ( ItemAdapterID, AdapterName ) values ( 12, 'RadioButtonListColum' );
set identity_insert prf.ItemAdapter off

insert into prf.ProfileItemTitle ( TitleID, SectionID, TitleName, ItemAdapterID ) values ( 11, 1, 'Gender', 10);
insert into prf.ProfileItemTitle ( TitleID, SectionID, TitleName, ItemAdapterID ) values ( 12, 1, 'BirthDate', 1);
insert into prf.ProfileItemTitle ( TitleID, SectionID, TitleName, ItemAdapterID ) values ( 13, 1, 'FirstName', 1);
insert into prf.ProfileItemTitle ( TitleID, SectionID, TitleName, ItemAdapterID ) values ( 21, 1, 'MiddleName', 1);
insert into prf.ProfileItemTitle ( TitleID, SectionID, TitleName, ItemAdapterID ) values ( 22, 1, 'LastName', 1);
insert into prf.ProfileItemTitle ( TitleID, SectionID, TitleName, ItemAdapterID ) values ( 32, 3, 'Languages', 10);
insert into prf.ProfileItemTitle ( TitleID, SectionID, TitleName, ItemAdapterID ) values ( 41, 3, 'RelationshipStatus', 9);
insert into prf.ProfileItemTitle ( TitleID, SectionID, TitleName, ItemAdapterID ) values ( 42, 2, 'EmailAddresses', 1);
insert into prf.ProfileItemTitle ( TitleID, SectionID, TitleName, ItemAdapterID ) values ( 43, 2, 'Addresses', 5);
insert into prf.ProfileItemTitle ( TitleID, SectionID, TitleName, ItemAdapterID ) values ( 44, 2, '[BusinessAddress]', 5);

/* select data */
select ia.*, it.* from prf.ItemAdapter ia join prf.ProfileItemTitle it on it.ItemAdapterID=ia.ItemAdapterID


Mappings:
Code:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="LightsCore.prf_ItemAdapter, LightsCore" table="prf.ItemAdapter">
      <id name="Id" type="Int32" unsaved-value="null">
         <column name="ItemAdapterID" length="4" sql-type="int" not-null="true" unique="true" index="PK_ItemTitleAdapter"/>
         <generator class="native" />
      </id>
      <property name="AdapterName" type="String">
         <column name="AdapterName" length="50" sql-type="varchar" not-null="true"/>
      </property>
   </class>

   <class name="LightsCore.prf_ProfileItemTitle, LightsCore" table="prf.ProfileItemTitle">
      <cache region="Profiles.Configuration" usage="nonstrict-read-write" />
      <id name="Id" type="Int32" unsaved-value="null">
         <column name="TitleID" length="4" sql-type="int" not-null="true" unique="true" index="PK_MemberProfileArea"/>
         <generator class="native" />
      </id>
      <property name="TitleName" type="String">
         <column name="TitleName" length="50" sql-type="varchar" not-null="true"/>
      </property>
      <one-to-one name="ItemAdapter" class="LightsCore.prf_ItemAdapter, LightsCore" lazy="false" fetch="join">
      </one-to-one>
   </class>

</hibernate-mapping>



Entity classes:
Code:



using System;
using System.Collections;
using System.ComponentModel;

namespace LightsCore
{
   /// <summary>
   /// prf_ProfileItemTitle object for NHibernate mapped table 'prf.ProfileItemTitle'.
   /// </summary>
   public class prf_ProfileItemTitle
   {
      #region Member Variables

      protected int _id; //1
      protected global::System.String _titleName; //2
      protected prf_ItemAdapter _prfItemAdapter;

      #endregion

      #region Constructors

      public prf_ProfileItemTitle() { }

      #endregion

      #region Public Properties

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

      public virtual global::System.String TitleName
      {
         get { return _titleName; }
         set { _titleName = value; }
      }

      public virtual prf_ItemAdapter ItemAdapter
      {
         get { return _prfItemAdapter; }
         set { _prfItemAdapter = value; }
      } //1

      #endregion
      
   }

   /// <summary>
   /// prf_ItemAdapter object for NHibernate mapped table 'prf.ItemAdapter'.
   /// </summary>
   public class prf_ItemAdapter
   {
      #region Member Variables
      
      protected int _id;
      protected global::System.String _adapterName;

      #endregion

      #region Constructors

      public prf_ItemAdapter() { }
      
      #endregion

      #region Public Properties

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

      public virtual global::System.String AdapterName
      {
         get { return _adapterName; }
         set { _adapterName = value; }
      }
      
      // begin relation

      #endregion
   }
}




Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 3:52 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
I'm actually willing to bet that the adapter values you are getting on the first two titles are CheckBoxList and RadioButtonListColumns, respectively. This is because a one-to-one relation joins on the primary keys of both tables.

However, the relation from Title to Adapter is many-to-one, not one-to-one. Changing it thusly in your mapping should fix the problem. See here.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 20, 2007 11:03 am 
Newbie

Joined: Mon Aug 13, 2007 8:49 pm
Posts: 9
Thanks marcel,

You pointed me in the right direction. I was using the one-to-one incorrectly.


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.