-->
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.  [ 5 posts ] 
Author Message
 Post subject: Middlegen - Composite id w/ key-many-to-one and HQL queries
PostPosted: Tue Jul 06, 2004 7:13 pm 
Beginner
Beginner

Joined: Thu Jun 24, 2004 1:04 pm
Posts: 35
Location: Minnesota - USA
This question is about how composite keys that use <key-many-to-one> mappings affect HQL queries. Obligatory hours searching, reading documentation and FAQs are already spent. ;)

Middlegen created my initial *.hbm.xml files based on my legacy database. Snippets from the files are at the end of this post, I've excluded the non-key properties and other junk. Notice the <composite-id> id mappings to a PK class and the use of <key-many-to-one>. The POJO and PK are generated using hbm2java.

Now the behaviour I get for an HQL like the following which matches 4 records in the SubCodeDatum table, each with a different SubCode parent, but all with the same SystemCode parent.

Code:
   "from SubCodeDatum subCodeDatum where subCodeDatum.dataElementValue like '%File%'"


Results in the following SQL:
Code:
[7/6/04 17:13:04:495 CDT] 14783e63 SystemOut     O Hibernate: select subcodedat0_.CODE as CODE, subcodedat0_.SUB_CODE as SUB_CODE, subcodedat0_.BEGIN_DT as BEGIN_DT, subcodedat0_.DATA_ELEMENT_NAME as DATA_ELE4_, subcodedat0_.UPDATE_NU as UPDATE_NU, subcodedat0_.DATA_ELEMENT_VALUE as DATA_ELE6_, subcodedat0_.CREATE_DT as CREATE_DT, subcodedat0_.CREATE_USER as CREATE_U8_, subcodedat0_.MDFCTN_DT as MDFCTN_DT, subcodedat0_.MDFCTN_USER as MDFCTN_10_ from UI_SUB_CODE_DATA subcodedat0_ where (subcodedat0_.DATA_ELEMENT_VALUE like '%File%' )
[7/6/04 17:13:04:505 CDT] 14783e63 SystemOut     O Hibernate: select systemcode0_.CODE as CODE0_, systemcode0_.UPDATE_NU as UPDATE_NU0_, systemcode0_.DESCRIPTION as DESCRIPT3_0_, systemcode0_.CREATE_DT as CREATE_DT0_, systemcode0_.CREATE_USER as CREATE_U5_0_, systemcode0_.MDFCTN_DT as MDFCTN_DT0_, systemcode0_.MDFCTN_USER as MDFCTN_U7_0_ from UI_SYSTEM_CODE systemcode0_ where systemcode0_.CODE=?
[7/6/04 17:13:04:515 CDT] 14783e63 SystemOut     O Hibernate: select subcode0_.CODE as CODE0_, subcode0_.SUB_CODE as SUB_CODE0_, subcode0_.BEGIN_DT as BEGIN_DT0_, subcode0_.UPDATE_NU as UPDATE_NU0_, subcode0_.END_DT as END_DT0_, subcode0_.CREATE_DT as CREATE_DT0_, subcode0_.CREATE_USER as CREATE_U7_0_, subcode0_.MDFCTN_DT as MDFCTN_DT0_, subcode0_.MDFCTN_USER as MDFCTN_U9_0_ from UI_SUB_CODE subcode0_ where subcode0_.CODE=? and subcode0_.SUB_CODE=? and subcode0_.BEGIN_DT=?
[7/6/04 17:13:04:545 CDT] 14783e63 SystemOut     O Hibernate: select subcode0_.CODE as CODE0_, subcode0_.SUB_CODE as SUB_CODE0_, subcode0_.BEGIN_DT as BEGIN_DT0_, subcode0_.UPDATE_NU as UPDATE_NU0_, subcode0_.END_DT as END_DT0_, subcode0_.CREATE_DT as CREATE_DT0_, subcode0_.CREATE_USER as CREATE_U7_0_, subcode0_.MDFCTN_DT as MDFCTN_DT0_, subcode0_.MDFCTN_USER as MDFCTN_U9_0_ from UI_SUB_CODE subcode0_ where subcode0_.CODE=? and subcode0_.SUB_CODE=? and subcode0_.BEGIN_DT=?
[7/6/04 17:13:04:545 CDT] 14783e63 SystemOut     O Hibernate: select subcode0_.CODE as CODE0_, subcode0_.SUB_CODE as SUB_CODE0_, subcode0_.BEGIN_DT as BEGIN_DT0_, subcode0_.UPDATE_NU as UPDATE_NU0_, subcode0_.END_DT as END_DT0_, subcode0_.CREATE_DT as CREATE_DT0_, subcode0_.CREATE_USER as CREATE_U7_0_, subcode0_.MDFCTN_DT as MDFCTN_DT0_, subcode0_.MDFCTN_USER as MDFCTN_U9_0_ from UI_SUB_CODE subcode0_ where subcode0_.CODE=? and subcode0_.SUB_CODE=? and subcode0_.BEGIN_DT=?
[7/6/04 17:13:04:555 CDT] 14783e63 SystemOut     O Hibernate: select subcode0_.CODE as CODE0_, subcode0_.SUB_CODE as SUB_CODE0_, subcode0_.BEGIN_DT as BEGIN_DT0_, subcode0_.UPDATE_NU as UPDATE_NU0_, subcode0_.END_DT as END_DT0_, subcode0_.CREATE_DT as CREATE_DT0_, subcode0_.CREATE_USER as CREATE_U7_0_, subcode0_.MDFCTN_DT as MDFCTN_DT0_, subcode0_.MDFCTN_USER as MDFCTN_U9_0_ from UI_SUB_CODE subcode0_ where subcode0_.CODE=? and subcode0_.SUB_CODE=? and subcode0_.BEGIN_DT=?


Looks like it is loading all the parent records too! Is this because the the Composite PK are defined to have references to the parent? From the <key-many-to-one> mapping?

If I were to replace the <key-many-to-one> mapping with <key-property> mappings and then add additional <many-to-one lazy="true" inverse="true"> associations, these extra SQL would not be executed?

Assuming that even works, what are the downsides to this approach? How could I get Middlegen to generate *.hbm.xml in that fashion?

For that matter, what is the point of <key-many-to-one> in the first place?

The composite id class structure I'm using here also makes it a p.i.t.a. to query on an id attribute "owned" by the top most parent class. For example, at 3 deep I've got "subCodeDatum.comp_id.subCode.comp_id.systemCode.code = :code", and we've got tables much deeper coming up yet. I'm hoping to find something simpler to use. :)

Thank you for any assistance you can provide.

--gus

The XML snippets are:

SystemCode:
Code:
<class
    name="xxx.SystemCode"
    table="UI_SYSTEM_CODE"
   
>
    <id
        name="code"
        type="java.lang.String"
        column="CODE"
    >
        <generator class="assigned" />
    </id>

    <!-- bi-directional one-to-many association to SubCode -->
    <bag
       
        name="subCodes"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="CODE" />
        </key>
        <one-to-many
            class="xxx.SubCode"
        />
    </bag>


SubCode:
Code:
<class
    name="xxx.SubCode"
    table="UI_SUB_CODE"
>
    <composite-id name="comp_id" class="xxx.SubCodePK">
        <!-- bi-directional many-to-one association to SystemCode -->
        <key-many-to-one
           name="systemCode"
           class="xxx.SystemCode"
       >
           <column name="CODE" />
       </key-many-to-one>
        <key-property
            name="subCode"
            column="SUB_CODE"
            type="java.lang.String"
            length="30"
        />
        <key-property
            name="beginDt"
            column="BEGIN_DT"
            type="java.sql.Date"
            length="10"
        />
    </composite-id>   

    <!-- associations -->
    <!-- bi-directional one-to-many association to SubCodeDatum -->
    <bag
        name="subCodeData"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="CODE" />
            <column name="SUB_CODE" />
            <column name="BEGIN_DT" />
        </key>
        <one-to-many
            class="xxx.SubCodeDatum"
        />
    </bag>
</class>


SubCodeData
Code:
<class     name="xxx.SubCodeDatum"
    table="UI_SUB_CODE_DATA"
>
    <composite-id name="comp_id" class="xxx.SubCodeDatumPK">
        <!-- bi-directional many-to-one association to SubCode -->
        <key-many-to-one
           name="subCode"
           class="xxx.SubCode"
       >
           <column name="CODE" />
           <column name="SUB_CODE" />
           <column name="BEGIN_DT" />
       </key-many-to-one>
        <key-property
            name="dataElementName"
            column="DATA_ELEMENT_NAME"
            type="java.lang.String"
            length="100"
        />
    </composite-id>   

    <property
        name="dataElementValue"
        type="java.lang.String"
        column="DATA_ELEMENT_VALUE"
        not-null="true"
        length="1000"
    />
    <!-- associations -->

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 8:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Quote:
Looks like it is loading all the parent records too! Is this because the the Composite PK are defined to have references to the parent? From the <key-many-to-one> mapping?


Yes. Now the relationship is not lazy so it will load the relationship graph.

Quote:
If I were to replace the <key-many-to-one> mapping with <key-property> mappings and then add additional <many-to-one lazy="true" inverse="true"> associations, these extra SQL would not be executed?

Assuming that even works, what are the downsides to this approach? How could I get Middlegen to generate *.hbm.xml in that fashion?


It would be the same, your suggested mapping changes result in identical mapping symantics. Middlegen R5 allow you to select either mapping approach (not released as yet - in CVS). The solution is the use of class and/or relationship lazy attributes.

Quote:
For that matter, what is the point of <key-many-to-one> in the first place?


Its an alternative that many users have asked for but its not the recommended approach (I changed Middlegen from the non-key to key-many-to-one at R3 (I think) because users wanted it that way). R5 will allow either approach. The default is for non key mapping method.

[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 9:53 pm 
Beginner
Beginner

Joined: Thu Jun 24, 2004 1:04 pm
Posts: 35
Location: Minnesota - USA
Quote:
It would be the same, your suggested mapping changes result in identical mapping symantics. Middlegen R5 allow you to select either mapping approach (not released as yet - in CVS). The solution is the use of class and/or relationship lazy attributes.

I did have "lazy=true" on the relation I was going to add. Am I not understanding something, or was it just a mis-read on your part?

Apart from that, your response is most reassuring, as it affirms that I'm not too far off the mark in understanding how this all works.

I'll change the mappings around tonight, rerun hbm2java, change my code and HQLs around and see if things still work. ;)

You guys are the best. Thanks again!

BTW, Do you have any outstanding areas in which developers can contribute back to the project? I'd like to give something back in my spare time. I couldn't find anything on the site, nor a TODO.txt in the source.

--gus


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 2:49 am 
Beginner
Beginner

Joined: Thu Jun 24, 2004 1:04 pm
Posts: 35
Location: Minnesota - USA
Ok, got it to work.

For my one-to-many relation, I had to:
1) On the many side of the relation, use insert="false" update="false". This was required, or the Configuration would not deploy.
2) On the one side of the relation, I added the 'proxy' attribute on the <class> declaration so the parent object would not get initialized when I load a children only.

Still a bit confused by the difference in syntax (lack of symmetry) in how one-to-many (proxy=) and many-to-one(lazy=true) lazy initialization are each handled in the .hbm.xml file. I'll research this a bit more, make sure I'm not missing anything.

I much preferring the simpler composite PK classes.

Thanks again.

--gus


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 7:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Glad you have sorted the issue out.

To help the project, look through the Hibernate JIRA issues and pick one, upload the patch. I'd be happy to have a some issues tackled in the Middlegen hibernate plugin. email david@hibernate.org if you would like to have a go.


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