-->
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: Wrong foreign key value used for querying Set assoc.
PostPosted: Mon Dec 26, 2011 2:34 pm 
Newbie

Joined: Tue Sep 23, 2008 4:44 pm
Posts: 15
I have three classes A, B(subclass of A), and C. B has a Set of elements of C.
In the database there are three corresponding tables A_TABLE, B_TABLE, and C_TABLE, and an additional view B_VIEW which is what B is mapped to(instead of B_TABLE, because the view has data from additional tables). On inserts into B_VIEW, a trigger in the database inserts a row into B_TABLE using a sequence. The code and mapping for A&B has existed before as did all the database tables and views. I am trying to add a mapping for C.

While the data is inserted into C_TABLE correctly (due to B lacking an Id, there is an insert of B only followed by update of B with the set of C attached ), the issue is that on the read path (load collections from database), hibernate does not load anything because in the criteria query it tries to use the value of aId (instead of value of bId) in the query, even though the column name is correct. The issue arises because B is mapped using a <join> without an entity id.

My Question is whether there is a way to fix this, perhaps by telling hibernate that it should use bId as the foreign key instead of aId during the load. I know this is twisted, but the code has existed for a long time. TIA for any suggestions and help.

Query at runtime:
Code:
select [list of columns] from C where c.B_ID=<hibernate incorrectly puts value of **aId** here, whereas it should be **bId**>


Java classes:
Code:
class A
long aId;
...<other properties>

class B extends A
long bId; // This is actually only read, but not initialized before write as the corresponding DB column is written to by a trigger-> Pre existing code.
Set <C> setOfCElements;
String aName;


class C
B b;
long cId;
long name;


Hibernate Mappings

A.hbm.xml
Code:
<class name="my.pkg.A" table="A_TABLE" ...>
  <id name="aId" column="A_ID">
  <discriminator column="DISCRIMINATOR">
  <natural-id>
   <property name="name"     column="NAME"  not-null="true"  length="4000" />
  </natural-id>



B.hbm.xml

Code:
<subclass discriminator-value="B" extends="my.pkg.A" name="my.pkg.B">

<set name="setOfCElements" table="B_TABLE"
            inverse="true" lazy="false" fetch="select" cascade="all" >
            <key>
                <column name="B_ID" not-null="true" />
            </key>
            <one-to-many class="my.pkg.C" />
</set>

<join fetch="select" table="B_VIEW">
<key on-delete="cascade" column="A_ID"/>
<property name="bId" column="B_ID">
.....[other properties defined]


C.hbm.xml
Code:
<id name="cId" column="C_ID" type="long">
         <generator class="sequence">
            <param name="sequence">c_seq</param>
         </generator>
</id>

<many-to-one name="b" class="my.pkg.B" fetch="select" unique-key="unique_c_name" property-ref="bId">  <!-- referencing B_ID here as the foreign key -->
            <column name="B_ID" not-null="true" />
</many-to-one>


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.