-->
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.  [ 12 posts ] 
Author Message
 Post subject: Empty Collections being returned even though sql returns thm
PostPosted: Fri Nov 23, 2007 8:20 pm 
Newbie

Joined: Thu Nov 01, 2007 1:06 pm
Posts: 12
I'm at a total loss here.

I'm attempting to load a one-to-many set collection and the POJO isn't reflecting what the SQL is saying.

I'm doing something like this:

--------------
Set barSet = foo.getBars();
log(barSet.size());
for(Iterator it = barSet.iterator(); it.hasNext();)
{
...
}
log("DONE WORKING WITH BARS");

----------------

No matter what the resulting SQL pulls back I ALWAYS get a set of size zero and consequently it never iterates.

My JDBC tracing is showing me that the queries are executing fine. And if I set "bar"'s fetching to be non-lazy I'll even see them prefetched!!!!

Why is none of this making it back into my application????

The kicker is, if I go from a many-to-one relationship it works fine!

ie. bar.getFoo() works PERFECTLY

Please help me I'm going crazy...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 24, 2007 3:05 pm 
Newbie

Joined: Thu Nov 01, 2007 1:06 pm
Posts: 12
Ok. I'm still at a total loss here. I'm going to put up my mappings and maybe someone can tell me if I've screwed up somewhere?

<class name="Title" table="VTITLE">
<composite-id name="id" class="TitleId">
<key-property name="ltbDistrictCd" type="string">
<column name="LTB_DISTRICT_CD" length="2"/>
</key-property>
<key-property name="titleNmbr" type="string">
<column name="TITLE_NMBR" length="2"/>
</key-property>
</composite-id>

<set name="titleParcelIntersections" inverse="true">
<key>
<column name="LTB_DISTRICT_CD" not-null="true"/>
<column name="TITLE_NMBR" not-null="true"/>
</key>
<one-to-many class="TitleParcelIntersection" />
</set>

</class>

<class name="TitleParcelIntersection" table="VTPART">

<composite-id name="id" class="TitleParcelIntersectionId">
<key-property name="ltbDistrictCd" type="string" >
<column name="LTB_DISTRICT_CD" length="2" />
</key-property>
<key-property name="titleNmbr" type="string">
<column name="TITLE_NMBR" length="11"/>
</key-property>
<key-property name="prmnntPrclId" type="integer">
<column name="PRMNNT_PRCL_ID" length="4"/>
</key-property>
</composite-id>

<many-to-one name="title" class="Title" insert="false" update="false">
<column name ="LTB_DISTRICT_CD" />
<column name="TITLE_NMBR" />
</many-to-one>

</class>



I'm using JTA in websphere to control the session.

Here is some test code I've written to illustrate the situation.

----------

Title title = findTitle(); //Assume this finds the title
Set parcelIntersections = title.getTitleParcelIntersections();

log("Title Parcel Intersections: " + parcelIntersections.size());

for(Iterator it = parcelIntersections.iterator(); it.hasNext();)
{
TitleParcelIntersection tpi = (TitleParcelIntersection)it.next();
log(tpi.getId().getPrmnntPrclId();
}

---------

The log for this looks like:


getting Title instance with id: ca.ltsa.srs.database.entity.TitleId@5e821f3f
[11/24/07 10:36:51:643 PST] 6bd93f59 SystemOut O Hibernate:
/* load ca.ltsa.srs.database.entity.Title */ select
title0_.LTB_DISTRICT_CD as LTB1_34_0_,
title0_.TITLE_NMBR as TITLE2_34_0_,
title0_.TTL_STTS_CD as TTL3_34_0_,
title0_.CMN_PRPTY_IND as CMN4_34_0_,
title0_.TTL_CNVRSN_IND as TTL5_34_0_,
title0_.TTL_ENTRD_DT as TTL6_34_0_,
title0_.TTL_ENTRD_TM as TTL7_34_0_,
title0_.ROOT_OF_TITLE as ROOT8_34_0_,
title0_.MRKT_VALUE_AMNT as MRKT9_34_0_,
title0_.NATURE_OF_XFER1 as NATURE10_34_0_,
title0_.NATURE_OF_XFER2 as NATURE11_34_0_,
title0_.TTL_CNCL_DT as TTL12_34_0_,
title0_.TTL_CNCL_TM as TTL13_34_0_,
title0_.TTL_REMARKS as TTL14_34_0_,
title0_.DCMNT_MSTR_NMBR as DCMNT15_34_0_,
title0_.DCMNT_DISTRICT_CD as DCMNT16_34_0_
from
VTITLE title0_
where
title0_.LTB_DISTRICT_CD=?
and title0_.TITLE_NMBR=?

[11/24/07 10:36:53:044 PST] 6bd93f59 SystemOut O Hibernate:
/* load one-to-many ca.ltsa.srs.database.entity.Title.titleParcelIntersections */ select
titleparce0_.LTB_DISTRICT_CD as LTB1_1_,
titleparce0_.TITLE_NMBR as TITLE2_1_,
titleparce0_.PRMNNT_PRCL_ID as PRMNNT3_1_,
titleparce0_.LTB_DISTRICT_CD as LTB1_41_0_,
titleparce0_.TITLE_NMBR as TITLE2_41_0_,
titleparce0_.PRMNNT_PRCL_ID as PRMNNT3_41_0_,
titleparce0_.PRCL_TEXT_SEQ_NO as PRCL4_41_0_,
titleparce0_.TTL_STTS_CD as TTL5_41_0_,
titleparce0_.TTL_CNVRSN_IND as TTL6_41_0_
from
VTPART titleparce0_
where
titleparce0_.LTB_DISTRICT_CD=?
and titleparce0_.TITLE_NMBR=?

TitleParcelIntersections: 0


---------------------------------------

That's it! My jdbc trace indeed shows data coming back for the title parcel intersections but the POJO never sees any of it.


I'm getting more than a little frustrated with this now...any help would be GREATLY appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 25, 2007 7:18 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
One end of the relationship has to be the controlling end. In a bi-directional one-to-many this is usually the "many" end, indicated by setting inverse="true" on the collection (as you have done).

However, you've also set insert="false", update="false" on the "many" end, making this inverse also.

You're telling hibernate that neither class is the controlling end of this relationship. Remove the insert and update attribs from the TitleParcelIntersection mapping.

See "7.4.1. one to many / many to one" in the docs for an example.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 25, 2007 12:56 pm 
Newbie

Joined: Thu Nov 01, 2007 1:06 pm
Posts: 12
I've tried that. If I don't have the insert and update statements like I do I get an error when hibernate starts up saying that they are duplicate columns and need to specify exactly that...

Is there a way around it?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 25, 2007 2:18 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
TitleParcelIntersection needs to hold a reference to the primary key of Title - this is how we know which TitleParcelIntersection belong to which Title. You've got a problem with your current mappings because the names you've chosen to hold this reference clash with columns in TitleParcelIntersection.

The names don't have to be the same as the PK, they just represent a link to that PK. So just rename change your mappings thus:

In Title:
Code:
<set name="titleParcelIntersections" inverse="true">
<key>
*** <column name="LTB_DISTRICT_CD_FK" not-null="true"/>
*** <column name="TITLE_NMBR_FK" not-null="true"/>
</key>
<one-to-many class="TitleParcelIntersection" />
</set>


In TitleParcelIntersection:
Code:
<many-to-one name="title" class="Title">
*** <column name ="LTB_DISTRICT_CD_FK" />
*** <column name="TITLE_NMBR_FK" />
</many-to-one>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 25, 2007 4:30 pm 
Newbie

Joined: Thu Nov 01, 2007 1:06 pm
Posts: 12
I just tried that and all that does is give me SQL errors since those column names don't exist.

I find it so odd that with my original setup it actually does the query and appears to actually load the results but they never actually get associated with my "Title" so title.getTitleParcelIntersections() always returns an empty set...

It is so weird. I'm begining to think it has something to do with my JTA configuration because all the mapping changes I'm trying all result in the same thing.


I liked the idea of the different column name reference but the sql just didn't like that. Any more thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 1:57 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Maybe I've missunderstood. Is this a one to many mapping or a one to one? Seems odd (if one to many) that the primary key for both tables contains the
same 2 column names.

If its truely a one to many then which columns represent that one to many mapping? Where is the data that links Title to TitleParcelIntersection?

It can't be TitleParcelIntersection.LTB_DISTRICT_CD/LTB_DISTRICT_CD.TITLE_NMBR because this is defined as the primary key so must be unique i.e. a one-to-one relationship.

Consider a parent-child relationship with 2 tables:

Code:
Parent
------
id  (PK)
name

Child
-----
id  (PK)
name
age


Although there maybe a one to many relationship in the real world, there's no data in the above schema to represent this relationship. However, I could define a hibernate mapping that wouldn't produce errors but wouldn't give the correct results either:

Code:
<class name="Parent">
<id name="id"/>
<set name="children" inverse="true">
<key column="id"/>
<one-to-many class="Child" />
</set>
...
</class>

<class name="Child">
<id name="id"/>
<many-to-one name="organization"
    column="id"
    class="Parent"/>
...
</class>


This is basically what you have done.

Changing the name of the PK to make the example more obvious and adding a column to hold the mapping data we get this:
Code:
Parent
------
parent_id  (PK)
name

Child
-----
child_id  (PK)
name
age
parent_id  (FK)


See how the child table now contains a like to its parent so we can know which child belongs to which parent row. This is basically what I told you to do in my last post. Clearly I have to modify the child table to add a column (2 columns in your case) to hold the parent id.

Resultant mappings:

Code:
<class name="Parent">
<id name="parent_id"/>
<set name="children" inverse="true">
<key column="parent_id"/>
<one-to-many class="Child" />
</set>
...
</class>

<class name="Child">
<id name="child_id"/>
<many-to-one name="organization"
    column="parent_id"
    class="Parent"/>
...
</class>



Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 2:58 am 
Newbie

Joined: Thu Nov 01, 2007 1:06 pm
Posts: 12
TitleParcelIntersection is actually an intersection table between two others: Title and Parcel

Title has the primary keys LTB_DISTRICT_CD and TITLE_NMBR
Parcel has the primary key PRMNT_PRCL_ID

There is a many to many relationship between them that is solved by the TitleParcelIntersection table.

So, the primary key of the Intersection table is a combination of both Title and Parcel.


The thing that is really blowing my mind here is that when I monitor the JDBC calls I can see the exact correct data for the Intersection class coming back, it just never makes it into the Title object.

I even put the second link (Intersection - Parcel) as non-lazy with a join and Hibernate even automatically fires off those secondary queries! But, the results still never make it into the initial Title object!

I really appreciate the time you have been taking to help me. I've tried so many different combinations of mapping these tables together and none of them give me the results I'm expecting.

I'm begining to think this is some sort of bug I'm exploiting in either Websphere 5.1's JTA or in Hibernate's transaction manager usage with it. I've posted another thread about a bit of quirkiness in my startup log for Hibernate but no one has noticed it yet -- there appear to be two Transaction Managers loading.

I'm so behind schedule I'm just going to have to manually load these collections and assign them to the object as I go (kind of defeating the purpose of hibernate).

Again, thanks for you help and any more is greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 4:19 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I saw your post about the two transaction managers and checked out the hibernate source briefly (3.2.5.ga) but could see no way of generating the log messages you had. I doubt there's a problem there. I've used hibernate on websphere before without too much difficulty. You could always run your code outside the container to give you peace of mind that it doesn't work there either ;)

Ok, I'm starting to understand what you are doing. You need to move the many-to-one mapping into the ID definition as currently you have those columns mapped twice. Instead of key-property use key-many-to-one. I've never done this with a composite inside another composite but I don't see why it shouldn't work:
Code:
<class name="TitleParcelIntersection" table="VTPART">

<composite-id name="id" class="TitleParcelIntersectionId">
  <key-many-to-one name="title" class="Title" >
    <column name ="LTB_DISTRICT_CD length="2"/>
    <column name="TITLE_NMBR" length="11"/>
  </key-many-to-one>

  <key-property name="prmnntPrclId" type="integer">
    <column name="PRMNNT_PRCL_ID" length="4"/>
  </key-property>
</composite-id>

<!-- Remove this as now replaced by mapping in ID definition
<many-to-one name="title" class="Title" insert="false" update="false">
<column name ="LTB_DISTRICT_CD" />
<column name="TITLE_NMBR" />
</many-to-one>
-->

</class>


You'll probably want to do the same with Parcel, unless the parcel_id is what you really need.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 12:39 pm 
Newbie

Joined: Thu Nov 01, 2007 1:06 pm
Posts: 12
Ya, I tried that one a while ago but I just gave it a shot again with the same results.

I'm going to have to come back and revisit this issue but for now I'm so pressed for time I'm just going to have to do manual lookups of the relationships. Sort of a hybrid between straight JDBC and Hibernate -- Ugly I know but I know it works.

Every manual, every piece of advise, and every new attempt I make SHOULD work (and under the covers it looks like it is) but I get no cigar at the end of the battle.

I'm going to post my mappings and cfg file for these three tables in question in case there is something totally glaring here.

Cheers mate.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

         <property name="hibernate.session_factory_name">HibernateSessionFactory</property>
         <property name="hibernate.max_fetch_depth">1</property>
         <property name="hibernate.default_batch_fetch_size">4</property>

      <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WebSphereTransactionManagerLookup</property>
      <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
      <property name="hibernate.current_session_context_class">jta</property>
      <property name="hibernate.transaction.flush_before_completion">true</property>
      <property name="hibernate.transaction.auto_close_session">true</property>

         <property name="hibernate.connection.datasource">jdbc/db2devlaix_EFCS</property>
         <property name="hibernate.connection.release_mode">auto</property>

      <property name="hibernate.show_sql">true</property>
      <property name="hibernate.format_sql">true</property>
      <property name="hibernate.use_sql_comments">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
       
       
        <mapping resource="Cccde.hbm.xml"/>
        <mapping resource="Charge.hbm.xml"/>
        <mapping resource="ChargeCorrection.hbm.xml"/>
        <mapping resource="ChargeCorrectionTextDetail.hbm.xml"/>
        <mapping resource="ChargeCorrectionTextHeader.hbm.xml"/>
        <mapping resource="ChargeEndorsement.hbm.xml"/>
        <mapping resource="ChargeImpact.hbm.xml"/>
        <mapping resource="ChargeImpactRemarks.hbm.xml"/>
        <mapping resource="ChargeOwnerIntersection.hbm.xml"/>
        <mapping resource="ChargeOwnershipGroup.hbm.xml"/>
        <mapping resource="ChargeOwnershipGroupRemarks.hbm.xml"/>
        <mapping resource="Client.hbm.xml"/>
        <mapping resource="ClientAddress.hbm.xml"/>
        <mapping resource="ClientTitleIntersection.hbm.xml"/>
        <mapping resource="Dcmnt.hbm.xml"/>
        <mapping resource="DocRf.hbm.xml"/>
        <mapping resource="Eform.hbm.xml"/>
        <mapping resource="HistoricTitleTaxAuthorityIntersection.hbm.xml"/>
        <mapping resource="Image.hbm.xml"/>
        <mapping resource="LegalNotation.hbm.xml"/>
        <mapping resource="LegalNotationCorrection.hbm.xml"/>
        <mapping resource="LegalNotationImpact.hbm.xml"/>
        <mapping resource="LegalNotationText.hbm.xml"/>
        <mapping resource="Parcel.hbm.xml"/>
        <mapping resource="ParcelActiveTitleTaxAuthorityIntersection.hbm.xml"/>
        <mapping resource="ParcelActivityNotificationRequest.hbm.xml"/>
        <mapping resource="ParcelCorrectionTextDetail.hbm.xml"/>
        <mapping resource="ParcelCorrectionTextHeader.hbm.xml"/>
        <mapping resource="ParcelFullLegalDescription.hbm.xml"/>
        <mapping resource="ParcelFullLegalDescriptionText.hbm.xml"/>
        <mapping resource="ParcelMarkUp.hbm.xml"/>
        <mapping resource="ShortLegalSubdivided.hbm.xml"/>
        <mapping resource="ShortLegalUnsubdivided.hbm.xml"/>
        <mapping resource="TaxAuthority.hbm.xml"/>
        <mapping resource="Title.hbm.xml"/>
        <mapping resource="TitleCorrection.hbm.xml"/>
        <mapping resource="TitleCorrectionReason.hbm.xml"/>
        <mapping resource="TitleCorrectionTextDetail.hbm.xml"/>
        <mapping resource="TitleCorrectionTextHeader.hbm.xml"/>
        <mapping resource="TitleOwnership.hbm.xml"/>
        <mapping resource="TitleOwnershipRemarks.hbm.xml"/>
        <mapping resource="TitleParcelIntersection.hbm.xml"/>
        <mapping resource="TitleTransfer.hbm.xml"/>
        <mapping resource="TransferofChargeOwnership.hbm.xml"/>
        <mapping resource="Trcde.hbm.xml"/>

    </session-factory>
</hibernate-configuration>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="ca.ltsa.srs.database.entity.Title" table="VTITLE">
      <composite-id name="id" class="ca.ltsa.srs.database.entity.TitleId">
         <key-property name="ltbDistrictCd" type="string">
            <column  name="LTB_DISTRICT_CD" length="2"/>
         </key-property>
         <key-property name="titleNmbr" type="string">
            <column name="TITLE_NMBR" length="11"/>
         </key-property>
      </composite-id>
      
      <property name="ttlSttsCd" type="string">
         <column name="TTL_STTS_CD" length="1" not-null="true"/>
      </property>
      <property name="cmnPrptyInd" type="string">
         <column name="CMN_PRPTY_IND" length="1" not-null="true"/>
      </property>
      <property name="ttlCnvrsnInd" type="string">
         <column name="TTL_CNVRSN_IND" length="1" not-null="true"/>
      </property>
      <property name="ttlEntrdDt" type="date">
         <column name="TTL_ENTRD_DT" not-null="false"/>
      </property>
      <property name="ttlEntrdTm" type="time">
         <column name="TTL_ENTRD_TM" not-null="false"/>
      </property>
      <property name="rootOfTitle" type="string">
         <column name="ROOT_OF_TITLE" length="50" not-null="false"/>
      </property>
      <property name="mrktValueAmnt" type="string">
         <column name="MRKT_VALUE_AMNT" length="11" not-null="true"/>
      </property>
      <property name="natureOfXfer1" type="string">
         <column name="NATURE_OF_XFER1" length="3" not-null="true"/>
      </property>
      <property name="natureOfXfer2" type="string">
         <column name="NATURE_OF_XFER2" length="3" not-null="true"/>
      </property>
      <property name="ttlCnclDt" type="date">
         <column name="TTL_CNCL_DT" not-null="false"/>
      </property>
      <property name="ttlCnclTm" type="time">
         <column name="TTL_CNCL_TM" not-null="false"/>
      </property>
      <property name="ttlRemarks" type="string">
         <column name="TTL_REMARKS" length="40" not-null="true"/>
      </property>
      
      <set name="historicalTaxAuthorityIntersections" inverse="true">
         <key>
            <column name ="LTB_DISTRICT_CD" not-null="true"/>             
            <column name="TITLE_NMBR" not-null="true" />
         </key>
         <one-to-many class="ca.ltsa.srs.database.entity.HistoricTitleTaxAuthorityIntersection"/>
      </set>   
      <set name="titleOwnerships" inverse="true">
          <key>
               <column name="LTB_DISTRICT_CD" not-null="true"/> 
               <column name="TITLE_NMBR" not-null="true"/>             
          </key>
          <one-to-many class="ca.ltsa.srs.database.entity.TitleOwnership" />
      </set>   
      
      <set name="chargeImpacts" inverse="true">
          <key>
               <column name="LTB_DISTRICT_CD" /> 
               <column name="TITLE_NMBR" />             
          </key> 
         <one-to-many class="ca.ltsa.srs.database.entity.ChargeImpact" />
      </set>   
      
      <set name="titleParcelIntersections" inverse="true">
          <key>
              <column name="LTB_DISTRICT_CD" not-null="true"/> 
              <column name="TITLE_NMBR" not-null="true"/>             
          </key>
          <one-to-many class="ca.ltsa.srs.database.entity.TitleParcelIntersection" />
      </set>

      <many-to-one name="masterDocument" class="ca.ltsa.srs.database.entity.Dcmnt" insert="false" update="false">
         <column name="DCMNT_MSTR_NMBR" length="11" not-null="false"/>
         <column name="DCMNT_DISTRICT_CD" length="2" not-null="true"/>      
      </many-to-one>
      
   </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="ca.ltsa.srs.database.entity.TitleParcelIntersection" table="VTPART" >
      
      <composite-id name="id" class="ca.ltsa.srs.database.entity.TitleParcelIntersectionId">   
         <key-property name="ltbDistrictCd" type="string" >
            <column name="LTB_DISTRICT_CD"  length="2" />
         </key-property>
         <key-property name="titleNmbr" type="string">
            <column name="TITLE_NMBR" length="11"/>
         </key-property>
               
         <key-property name="prmnntPrclId" type="integer">
            <column name="PRMNNT_PRCL_ID" length="4"/>
         </key-property>   
         
      </composite-id>      
      <property name="prclTextSeqNo" type="short">
         <column name="PRCL_TEXT_SEQ_NO" length="2" not-null="true"/>
      </property>
         <property name="ttlSttsCd" type="string">
         <column name="TTL_STTS_CD" length="1" not-null="true"/>
      </property>
         <property name="ttlCnvrsnInd" type="string">
         <column name="TTL_CNVRSN_IND" length="1" not-null="true"/>
      </property>      
         
      <many-to-one  name="title" class="ca.ltsa.srs.database.entity.Title" insert="false" update="false">
            <column name ="LTB_DISTRICT_CD"  />             
            <column name="TITLE_NMBR"  />
      </many-to-one>
      
      <many-to-one name="parcel" class="ca.ltsa.srs.database.entity.Parcel" insert="false" update="false">
         <column name ="PRMNNT_PRCL_ID" />   
      </many-to-one>
      
   </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="ca.ltsa.srs.database.entity.Parcel" table="VPARCL">
       <id name="prmnntPrclId" column="PRMNNT_PRCL_ID" type="integer" length="4" >
            <generator class="native"/>
        </id>
       
      <property name="prclSttsCd" type="string">
         <column name="PRCL_STTS_CD" length="1" not-null="true"/>
      </property>            
         <property name="ltbDistrictCd" type="string">
         <column name="LTB_DISTRICT_CD" length="2" not-null="true"/>
      </property>   
         <property name="lmtdAcsInd" type="string">
         <column name="LMTD_ACS_IND" length="1" not-null="true"/>
      </property>   
         <property name="prntgInd" type="string">
         <column name="PRNTG_IND" length="1" not-null="true"/>
      </property>   
         <property name="conversionInd" type="string">
         <column name="CONVERSION_IND" length="1" not-null="true"/>
      </property>   
         <property name="airspaceInd" type="string">
         <column name="AIRSPACE_IND" length="1" not-null="true"/>
      </property>   
         <property name="prclTypInd" type="string">
         <column name="PRCL_TYP_IND" length="1" not-null="true"/>
      </property>   
         <property name="strataPlanNmbr" type="string">
         <column name="STRATA_PLAN_NMBR" length="11" not-null="false"/>
      </property>   
         <property name="prclIncmpltInd" type="string">
         <column name="PRCL_INCMPLT_IND" length="1" not-null="true"/>
      </property>            
      
      <set name="parcelMarkups" inverse="true">
          <key>
               <column name="PRMNNT_PRCL_ID" />                         
          </key> 
         <one-to-many class="ca.ltsa.srs.database.entity.ParcelMarkUp" />
      </set>
        <set name="titleParcelIntersections" inverse="true">
          <key>
               <column name="PRMNNT_PRCL_ID" />                         
          </key> 
         <one-to-many class="ca.ltsa.srs.database.entity.TitleParcelIntersection" />
      </set>
       <set name="shortLegalSubdivideds" inverse="true">
          <key>
               <column name="PRMNNT_PRCL_ID" />                         
          </key> 
         <one-to-many class="ca.ltsa.srs.database.entity.ShortLegalSubdivided" />
      </set>
      <set name="shortLegalUnsubdivideds" inverse="true">
          <key>
               <column name="PRMNNT_PRCL_ID" />                         
          </key> 
         <one-to-many class="ca.ltsa.srs.database.entity.ShortLegalUnsubdivided" />
      </set>
      <list name="parcelFullLegals" inverse="true">         
         <key>
         <column name="PRMNNT_PRCL_ID"></column>
         </key>
         <list-index column="PRCL_TEXT_SEQ_NO" base="1"/>
         <one-to-many class="ca.ltsa.srs.database.entity.ParcelFullLegalDescription"/>
      </list>
      <set name="parcelTaxAuthIntersections" inverse="true">
         <key>
            <column name="PRMNNT_PRCL_ID"></column>
         </key>
         <one-to-many class="ca.ltsa.srs.database.entity.ParcelActiveTitleTaxAuthorityIntersection"/>
      </set>
      
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 5:17 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Right, I've taken the mappings you've had trouble with (title->titleParcelIntersection->parcel) and created a minimal stand alone test (no data fields, just mappings).

I've managed to save a title with 2 parcels and load it back succesfully. Code and mappings included below. Hopefully it will give you some insight into where you might be going wrong.

HibernateUtil.java
Code:
package test.jonnyo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {

    private static SessionFactory factory;

    static {
       Configuration config = new Configuration()
          .addClass(Title.class)
// HSQLDB Config
          .setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
         .setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver")
         .setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test")
          .setProperty("hibernate.connection.username", "sa")
          .setProperty("hibernate.connection.password", "")
          .setProperty("hibernate.hbm2ddl.auto", "create-drop")
          
// MYSQL Config
//          .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
//         .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
//         .setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test")
//         .setProperty("hibernate.connection.username", "user")
//         .setProperty("hibernate.connection.password", "password")
//         .setProperty("hibernate.hbm2ddl.auto", "create-drop")
                  
         .setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider")
         .setProperty("hibernate.show_sql", "true");
        HibernateUtil.setSessionFactory(config.buildSessionFactory());
    }

    public static synchronized Session getSession() {
        if (factory == null) {
            factory = new Configuration().configure().buildSessionFactory();
        }
        return factory.openSession();
    }

    public static void setSessionFactory(SessionFactory factory) {
        HibernateUtil.factory = factory;
    }
}


TestIt.java
Code:
package test.jonnyo;

import org.hibernate.Session;

public class TestIt {

   public static void main(String[] args) {
      TitleId titleId = createTestData();
      
      Session s = HibernateUtil.getSession();
      s.beginTransaction();
      
      Title title = (Title)s.load(Title.class, titleId);
      System.out.println("TitleParcelIntersections: "+title.getTitleParcelIntersections().size());
      
      s.getTransaction().commit();
      s.close();
   }

   private static TitleId createTestData() {
      Session s = HibernateUtil.getSession();
      s.beginTransaction();

      System.out.println("creating test data");
      TitleId titleId = new TitleId("AD", "12345AB");
      Title title = new Title();
      title.setId(titleId);
      s.save(title);
      
      Parcel parcel1 = new Parcel();
      s.save(parcel1);

      Parcel parcel2 = new Parcel();
      s.save(parcel2);

      TitleParcelIntersectionId titleParcelIntersectionId1 = new TitleParcelIntersectionId(title, parcel1);
      TitleParcelIntersection titleParcelIntersection1 = new TitleParcelIntersection();
      titleParcelIntersection1.setId(titleParcelIntersectionId1);
      s.save(titleParcelIntersection1);

      TitleParcelIntersectionId titleParcelIntersectionId2 = new TitleParcelIntersectionId(title, parcel2);
      TitleParcelIntersection titleParcelIntersection2 = new TitleParcelIntersection();
      titleParcelIntersection2.setId(titleParcelIntersectionId2);
      s.save(titleParcelIntersection2);

      s.getTransaction().commit();
      s.close();
      System.out.println("test data created");      
      return titleId;
   }
}


Title.hbm.xml (contains mappings for all classes)
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test.jonnyo">

<class name="Title" table="title" >
       
   <composite-id name="id" class="TitleId">
         <key-property name="ltbDistrictCd" type="string">
            <column  name="LTB_DISTRICT_CD" length="2"/>
         </key-property>
         <key-property name="titleNmbr" type="string">
            <column name="TITLE_NMBR" length="11"/>
         </key-property>
    </composite-id>     

    <set name="titleParcelIntersections" inverse="true">
          <key>
              <column name="LTB_DISTRICT_CD" not-null="true"/>
              <column name="TITLE_NMBR" not-null="true"/>             
          </key>
          <one-to-many class="TitleParcelIntersection" />
    </set>

</class>

<class name="TitleParcelIntersection" table="titleParcelIntersection">
   <composite-id name="id" class="TitleParcelIntersectionId">
      <key-many-to-one name="title" class="Title">
         <column name="LTB_DISTRICT_CD"/>
         <column name="TITLE_NMBR"/>
      </key-many-to-one>
      <key-many-to-one name="parcel" class="Parcel">
         <column name="PRMNNT_PRCL_ID" length="4"/>
      </key-many-to-one>
   </composite-id>     
</class>

<class name="Parcel" table="parcel">
       <id name="prmnntPrclId" column="PRMNNT_PRCL_ID" type="integer" length="4" >
            <generator class="native"/>
        </id>
</class>
     
</hibernate-mapping>


Title.java
Code:
package test.jonnyo;

import java.util.Set;

public class Title {
   private TitleId id;
   private Set<TitleParcelIntersection> titleParcelIntersections;
   
   public TitleId getId() {
      return id;
   }
   public void setId(TitleId id) {
      this.id = id;
   }
   public Set<TitleParcelIntersection> getTitleParcelIntersections() {
      return titleParcelIntersections;
   }
   public void setTitleParcelIntersections(Set<TitleParcelIntersection> titleParcelIntersections) {
      this.titleParcelIntersections = titleParcelIntersections;
   }
   
   
}


TitleParcelIntersection.java
Code:
package test.jonnyo;

public class TitleParcelIntersection {
   private TitleParcelIntersectionId id;

   public TitleParcelIntersectionId getId() {
      return id;
   }
   public void setId(TitleParcelIntersectionId id) {
      this.id = id;
   }
}


Parcel.java
Code:
package test.jonnyo;

public class Parcel {
   private int prmnntPrclId;

   public int getPrmnntPrclId() {
      return prmnntPrclId;
   }

   public void setPrmnntPrclId(int prmnntPrclId) {
      this.prmnntPrclId = prmnntPrclId;
   }
}


TitleId.java
Code:
package test.jonnyo;

import java.io.Serializable;

public class TitleId implements Serializable {
   private String ltbDistrictCd;
   private String titleNmbr;
   
   protected TitleId() {
   }
   
   public TitleId(String ltbDistrictCd, String titleNmbr) {
      this.ltbDistrictCd = ltbDistrictCd;
      this.titleNmbr = titleNmbr;
   }
   
   public String getLtbDistrictCd() {
      return ltbDistrictCd;
   }
   public void setLtbDistrictCd(String ltbDistrictCd) {
      this.ltbDistrictCd = ltbDistrictCd;
   }
   public String getTitleNmbr() {
      return titleNmbr;
   }
   public void setTitleNmbr(String titleNmbr) {
      this.titleNmbr = titleNmbr;
   }

   @Override
   public boolean equals(Object obj) {
      if (obj == null || !(obj instanceof TitleId)) {
         return false;
      }
      TitleId other = (TitleId)obj;
      return (other.ltbDistrictCd.equals(this.ltbDistrictCd) &&
            other.titleNmbr.equals(this.titleNmbr));
   }
   
   @Override
   public int hashCode() {
      return ltbDistrictCd.hashCode() + titleNmbr.hashCode();
   }
}


TitleParcelIntersectionId.java
Code:
package test.jonnyo;

import java.io.Serializable;

public class TitleParcelIntersectionId implements Serializable {
   private Title title;
   private Parcel parcel;
   
   public TitleParcelIntersectionId() {
   }
   
   public TitleParcelIntersectionId(Title title, Parcel parcel) {
      this.title = title;
      this.parcel = parcel;
   }
   
   public Title getTitle() {
      return title;
   }
   public void setTitle(Title title) {
      this.title = title;
   }
   public Parcel getParcel() {
      return parcel;
   }
   public void setParcel(Parcel parcel) {
      this.parcel = parcel;
   }

   @Override
   public boolean equals(Object obj) {
      if (obj == null || !(obj instanceof TitleParcelIntersectionId)) {
         return false;
      }
      TitleParcelIntersectionId other = (TitleParcelIntersectionId)obj;
      return (other.title.equals(this.title) &&
            other.parcel.equals(this.parcel));
   }
   
   @Override
   public int hashCode() {
      return title.hashCode() + parcel.hashCode();
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 6:22 pm 
Newbie

Joined: Thu Nov 01, 2007 1:06 pm
Posts: 12
I have not yet figured out WHY my stuff didn't work but I've figured out a very easy workaround that uses my mappings.

It seems that whenever I use the Hibernate Session to "load" or "get" the Title object nothing works right. However, I wrote a simple HQL:
Code:
from Title where id.titleNmbr=:title and id.ltbDistrictCd=:ltb

and the Title Object I get back out of that works just perfectly for me!

I have no idea why get/load mess up for me but this is a very very easy workaround to get the exact same functionality!

Cheers and thanks for sticking with me through this problem it is very much appreciated.


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