-->
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.  [ 4 posts ] 
Author Message
 Post subject: inheritance mapping rules issue
PostPosted: Tue Feb 14, 2006 9:52 am 
Newbie

Joined: Mon Feb 13, 2006 1:28 pm
Posts: 3
Hi everyone.

This is a question about inheritance mapping. I cannot seem to find a clear answer to it. Is there a rule about joined-subclass key column name in the case of a "table per subclass" strategy ?

Let's have an example :

<class name="Payment" table="PAYMENT">
<id name="id" type="long" column="PAYMENT_ID">
<generator class="native"/>
</id>
<property name="amount" column="AMOUNT"/>
...
<joined-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">
<key column="PAYMENT_ID"/>
<property name="creditCardType" column="CCTYPE"/>
...
</joined-subclass>
<joined-subclass name="CashPayment" table="CASH_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
<joined-subclass name="ChequePayment" table="CHEQUE_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
</class>

For each joined-subclass, could one set a key column name that is different from parent class id column name ? Table column names like CC_PAYMENT_ID, CA_PAYMENT_ID, CH_PAYMENT_ID for the subclass tables for example.

I believe the column names must the same for each table and I will tell you why.

Let's say a Customer (custid, address, phone, fax) is a Person (id, firstname, lastname) and the mapping looks like this :
<class name="Person" table="PERSON">
<id name="id" type="long" column="PERS_ID">
<generator class="increment"/>
</id>
...
<joined-subclass name="Customer" table="CREDIT_PAYMENT">
<key column="CUST_ID"/>
...
</joined-subclass>
</class>


I noticed that if I launch a HQL query like :
* select id, address from Customer

generated SQL would look like this :
* select cust0_1_.CUST_ID as col_0_0_,
* cust0_1_.PERS_NAME as col_1_0_
* from CUSTOMER cust0_ inner join PERSON cust0_1_
* on cust0_.CUST_ID = cust0_1_.PERS_ID

which naturally throws an Exception. Indeed, in the first line, the PERSON alias is used to bring CUST_ID.

This would not happen if CUSTOMER key column name was PERS_ID (same as parent). That is why I'm asking this question : is there an implicit naming rule about column name in the case of inheritance ?

By the way, this is not related but, speaking of inheritance, it does not seem that lazy loading works in the object inheritance tree. In the case of a mapping defining that all classes inherit from a single parent root class, when an object is loaded, Hibernate loads everything.

I hope I'm wrong. Am I ?

Thanks for any thought.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 12:13 pm 
Newbie

Joined: Wed Jan 28, 2004 5:04 pm
Posts: 4
The column names do not have to be the same, but the subclassed table ID has to be populated by the parent table ID.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 1:27 pm 
Newbie

Joined: Mon Feb 13, 2006 1:28 pm
Posts: 3
chazman00 wrote:
The column names do not have to be the same, but the subclassed table ID has to be populated by the parent table ID.


Many thanks chazman for your reply.

From your reply, I understand that when an instance of a subclass is created, the parent table ID should be generated (increment, sequence, etc..) and the ID value must be stored in both subclassed table ID and parent table ID. It is currently the way it works and it works fine.

The problem comes from the generated select query which is still incorrect and I can't seem to sort this out.

Any thoughts ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 2:13 pm 
Newbie

Joined: Wed Jan 28, 2004 5:04 pm
Posts: 4
Well, I think hib may be getting confused by your query. What happens if you just do the following HQL:

select cust from Customer cust


EDIT: I also notice from your example that there is no mention of the Address. I'm concerned that there is something in your mapping files that I can't see here. Can you post the objects and relevent mapping?


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