-->
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.  [ 1 post ] 
Author Message
 Post subject: Mapping <one-to-many> with user specified key column
PostPosted: Thu May 26, 2005 9:15 am 
Regular
Regular

Joined: Tue Oct 26, 2004 3:54 pm
Posts: 60
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Mapping documents:

TestataProposta.hbm.xml :

<hibernate-mapping>
<class name="it.xxx.scm.TestataProposta" table="testata_proposta">

<id name="id" column="id" type="long">
<generator class="sequence" />
</id>

<!-- Business key -->
<property name="idCommittente" type="string" column="id_committente" not-null="true"/>
<property name="idProposta" type="string" column="id_proposta"/>
<!-- Business key -->

<property name="sezione" type="string" column="sezione"/>
<property name="tipoOrdine" type="string" column="tipo_ordine" />
<property name="data" type="java.util.Date" column="data" />
<property name="destinatario" type="string" column="destinatario" />

<set name="righeProposta" lazy="true" inverse="true" cascade="all" outer-join="true">
<key>
<column name="id_proposta" />
</key>
<one-to-many class="it.xxx.scm.RigaProposta" />
</set>

</class>
</hibernate-mapping>

RigaProposta.hbm.xml :

<hibernate-mapping>
<class name="it.xxx.scm.RigaProposta" table="riga_proposta">

<id name="id" column="id" type="long">
<generator class="sequence" />
</id>

<!-- Business key -->
<property name="idCommittente" type="string" column="id_committente" not-null="true"/>
<property name="idProposta" type="string" column="id_proposta" insert="false" update="false" />
<property name="riga" type="java.lang.Integer" column="riga" />
<!-- Business key -->

<property name="idArticolo" type="string" column="id_articolo" />
<property name="quantita" type="java.math.BigDecimal" column="quantita" />

<many-to-one name="testataProposta" class="it.xxx.scm.TestataProposta" property-ref="idProposta">
<column name="id_proposta" />
</many-to-one>

</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

TestataProposta testata = session.get(TestataProposta.class, id);

Full stack trace of any exception that occurs: None


Name and version of the database you are using: PostgreSQL 7.4

The generated SQL (show_sql=true):

Hibernate: select testatapro0_.id as id1_, testatapro0_.id_committente as id2_40_1_, testatapro0_.id_proposta as id3_40_1_, test
atapro0_.sezione as sezione40_1_, testatapro0_.tipo_ordine as tipo5_40_1_, testatapro0_.data as data40_1_, testatapro0_.destinat
ario as destinat7_40_1_, righepropo1_.id_proposta as id3___, righepropo1_.id as id__, righepropo1_.id as id0_, righepropo1_.id_c
ommittente as id2_41_0_, righepropo1_.id_proposta as id3_41_0_, righepropo1_.riga as riga41_0_, righepropo1_.id_articolo as id5_
41_0_, righepropo1_.quantita as quantita41_0_ from testata_proposta testatapro0_ left outer join riga_proposta righepropo1_ on t
estatapro0_.id=righepropo1_.id_proposta where testatapro0_.id=?

Debug level Hibernate log excerpt: None useful


I'm wondering if what I'm trying to do is possible.

I have a parent table, TestataProposta, and a child table, RigaProposta.

They both have their own auto-generated primary key id. TestataProposta has a business (natural let's say) key, called "id_proposta". The children have that column too, so they can <many-to-one> to the father (and this works).

When I wrote like 150 TestataProposta object with children Hibernate correctly wrote to the database the parent and children objects with cascade="all" (so I just called testataProposta.save()), and populated the "id_proposta" column on children objects with the correct business value (not the primary key).

But when I do a query on TestataProposta, Hibernate tries to match parent's primary key (ID) with children business key (ID_PROPOSTA). And of course the parent has no children, this way. See the generated SQL above : on testatapro0_.id=righepropo1_.id_proposta

I did not find any way to specify on the <one-to-many> clause the source column for the association. Is this impossibile ?

I don't want to use the primary key for the association, because if we export the data to another database that key would be probably meaningless.

Any suggestion ?

Thanks

Giulio


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.