-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping one class to multiple tables
PostPosted: Fri Dec 07, 2007 5:33 am 
Newbie

Joined: Fri Dec 07, 2007 5:02 am
Posts: 4
Location: Haugesund, Norway
Hi.

I need some help on this one. I'm trying to migrate an old Lotus Notes app into some java, and came across a problem. In general, I have this sql which I want to convert to a hbm... Should be a breeze, I thought. But the join tag is not as flexible as I first thought.

This is the sql:
Code:
     SELECT s.idnumeric          AS id,
            S.SAMPLED_DATE       AS sampleDate,
            S.SAMPLING_POINT     AS samplePoint,
            sp.DESCRIPTION       AS description,
            S.CARGO              AS cargo,
            sp.POINT_LOCATION    AS location,
            R.NAME               AS component,
            R.TEXT,              AS value,
            R.UNITS              AS unit,
            vc.ORDER_NUMBER      AS vcOrderNo,
            T.ORDER_NUM          AS testOrderNo 
       FROM SAMPLE S,
            TEST T,
            RESULT R,
            SAMPLE_POINT sp,
            VERSIONED_COMPONENT vc
      WHERE S.ID_NUMERIC = T.SAMPLE
        AND T.TEST_NUMBER = R.TEST_NUMBER
        AND S.SAMPLING_POINT = sp.IDENTITY
        AND T.ANALYSIS = vc.ANALYSIS
        AND R.NAME = vc.NAME 
        AND R.TEXT <> ' ' 
        and S.STATUS <> 'S'

Since this joins across multiple tables where only SAMPLEPOINT and TEST have references to the SAMPLE class, Hibernate seems have trouble joining correctly.

This is my attempt at a map:
Code:
<hibernate-mapping default-access="field">
  <class name="no.gassco.track.domain.sampleManager.Sample" table="SAMPLE">
    <id name="id" type="string" column="ID_NUMERIC" length="10" />

    <property name="sampleDate" type="date" column="SAMPLED_DATE" length="7" />
    <property name="status" type="string" column="STATUS" length="1" />
    <property name="cargo" type="string" column="CARGO" length="8" />

    <many-to-one name="location" cascade="none" lazy="false" column="LOCATION_ID" />
    <many-to-one name="samplePoint" cascade="none" lazy="false" column="SAMPLING_POINT" />

    <!-- Associations -->
    <join table="TEST">
      <key column="SAMPLE" property-ref="id" />
      <property name="testId" type="string" column="TEST_NUMBER" length="10" />
      <property name="testOrder" type="string" column="ORDER_NUM" length="10" />
      <property name="analysis" type="string" column="ANALYSIS" length="10" />
    </join>
    <join table="RESULT">
      <key column="TEST_NUMBER" property-ref="testId" />
      <property name="componentName" type="string" column="NAME" length="40" />
      <property name="unit" type="string" column="UNITS" length="10" />
      <property name="numValue" type="double" column="VALUE" length="126" />
      <property name="textValue" type="string" column="TEXT" length="234" />
    </join>
    <join table="VERSIONED_COMPONENT">
      <key column="ANALYSIS" property-ref="analysis" />
      <property name="componentOrder" type="string" column="ORDER_NUMBER" length="10" />
    </join>
  </class>
</hibernate-mapping>

But this map wants every join to use the SAMPLE class' primary key as reference. The resulting where clause is then:
Code:
       FROM SAMPLE S,
            TEST T,
            RESULT R,
            SAMPLE_POINT sp,
            VERSIONED_COMPONENT vc
      WHERE S.ID_NUMERIC = T.SAMPLE
        AND S.ID_NUMERIC = R.TEST_NUMBER
        AND S.ID_NUMERIC = vc.ANALYSIS

which is completelly wrong.

What can I do to make this map work as the sql statement presented at the top does?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 7:13 am 
Newbie

Joined: Fri Dec 07, 2007 5:02 am
Posts: 4
Location: Haugesund, Norway
I think I solved it, though it might not be the most elegant of solutions:
Code:
  <class name="no.gassco.track.domain.sampleManager.Sample" table="SAMPLE">
    <id name="id" type="string" column="ID_NUMERIC" length="10" />

    <property name="sampleDate" type="date" column="SAMPLED_DATE" length="7" />
    <property name="status" type="string" column="STATUS" length="1" />
    <property name="cargo" type="string" column="CARGO" length="8" />

    <many-to-one name="location" cascade="none" lazy="false" column="LOCATION_ID" />
    <many-to-one name="samplePoint" cascade="none" lazy="false" column="SAMPLING_POINT" />

    <!-- Associations -->
    <join table="TEST" >
      <subselect>
        select T.SAMPLE       AS sample,
               T.ORDER_NUM    AS testOrder,
               R.NAME         AS componentName,
               R.VALUE        AS numValue,
               R.TEXT         AS textValue,
               R.UNITS        AS unit,
               V.ORDER_NUMBER AS componentOrder
          from Test T,
               Result R,
               Versioned_Component V
         where T.TEST_NUMBER = R.TEST_NUMBER
           and T.ANALYSIS    = V.ANALYSIS
           and R.NAME        = V.NAME
           and R.TEXT not like ' '
      </subselect>
      <key column="SAMPLE"/>
      <property name="testOrder" type="string" column="testOrder" length="10" />
      <property name="componentName" type="string" column="componentName" length="40" />
      <property name="numValue" type="double" column="numValue" length="126" />
      <property name="textValue" type="string" column="textValue" length="234" />
      <property name="unit" type="string" column="unit" length="10" />
      <property name="componentOrder" type="string" column="componentOrder" length="10" />
    </join>
  </class>


Any suggestions as how to map this without involving inline sql, would be appreciated.

Thanks.


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