-->
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: Inheritance , one-to-many problems
PostPosted: Wed Mar 15, 2006 11:38 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
I have a one to many relation between Payment1 and Orders1. Payment1 is superclass for CreditCard and Cash. Inheritance was implemented as "Table per subclass". If I execute an hql like FROM Payment1 I can cast Payment1 to CreditCard or Cash (function of instanceof) and I can obtain properties for every Payment1 subclasses.

But when I use
Orders1 order1 = (Orders1) session.load(Orders1.class, idOrd);
Payment1 payment1 = order1.getPayment1();
if (payment1 instanceof CreditCard) {
System.out.println("Payment with credit card...");
CreditCard cc = (CreditCard) payment1;
System.out.println("Credit card type = " + cc.getCreditCardType());
} else if (payment1 instanceof Cash) {
System.out.println("Payment with cash...");
Cash cash = (Cash) payment1;
System.out.println("Cash type = " + cash.getCashType());
} else {
System.out.println(payment1.getClass().getName());
}

I obtain
lotofsamples.model.Payment1$$EnhancerByCGLIB$$47095661
in console

How can I cast to CreditCard, to obtain CreditCard properties values???

Thx.



Hibernate version:3.1.0


Mapping documents:
Orders1.hbm.xml
<class name="lotofsamples.model.Orders1" table="orders1" dynamic-insert="true" dynamic-update="true">
<id name="id" column="order1id" type="integer">
<generator class="sequence">
<param name="sequence">seq_orders1</param>
</generator>
</id>
<many-to-one name="payment1" fetch="select" cascade="all" class="lotofsamples.model.Payment1" lazy="false">
<column name="payment1id"></column>
</many-to-one>
<property name="observatie" column="observatie" type="string" length="100" not-null="false"></property>
</class>

Payment1.hbm.xml
<class name="lotofsamples.model.Payment1" table="payment1" dynamic-insert="true" dynamic-update="true" >

<id name="id" column="payment1id" type="integer">
<generator class="sequence">
<param name="sequence">seq_payment1</param>
</generator>
</id>

<property name="amount" column="amount" type="double"></property>

<set name="orders1s" table="orders1" cascade="all" inverse="true" >
<key column="payment1id"></key>
<one-to-many class="lotofsamples.model.Orders1"/>
</set>

<joined-subclass name="lotofsamples.model.CreditCard" table="credit_card" dynamic-insert="true" dynamic-update="true">
<key column="payment1id"></key>
<property name="creditCardType" column="credit_card_type" type="string" length="10" not-null="true"></property>
</joined-subclass>

<joined-subclass name="lotofsamples.model.Cash" table="cash" dynamic-insert="true" dynamic-update="true">
<key column="payment1id"></key>
<property name="cashType" column="cash_type" type="string" length="10" not-null="true"></property>
</joined-subclass>

</class>


Code between sessionFactory.openSession() and session.close():
session = new Configuration().configure().buildSessionFactory().openSession();

Integer idOrd = Integer.parseInt(JOptionPane.showInputDialog(
"Introduceti id-ul orderului", 14));
Orders1 order1 = (Orders1) session.load(Orders1.class, idOrd);

System.out.println("Id order = " + order1.getId());
System.out.println("Observatie order = " + order1.getObservatie());

Payment1 payment1 = order1.getPayment1();
System.out.println("Id payment = " + payment1.getId());
System.out.println("Amount payment = " + payment1.getAmount());

if (payment1 instanceof CreditCard) {
System.out.println("Payment with credit card...");
CreditCard cc = (CreditCard) payment1;
System.out.println("Credit card type = " + cc.getCreditCardType());
} else if (payment1 instanceof Cash) {
System.out.println("Payment with cash...");
Cash cash = (Cash) payment1;
System.out.println("Cash type = " + cash.getCashType());
} else {
System.out.println(payment1.getClass().getName());
}


Full stack trace of any exception that occurs:
None


Name and version of the database you are using:
PostgreSQL-8.1.3


The generated SQL (show_sql=true):
Hibernate: /* load lotofsamples.model.Orders1 */ select orders1x0_.order1id as order1_17_0_, orders1x0_.payment1id as payment2_17_0_, orders1x0_.observatie as observatie17_0_ from iqbyte.orders1 orders1x0_ where orders1x0_.order1id=?
Hibernate: /* load lotofsamples.model.Payment1 */ select payment1x0_.payment1id as payment1_14_0_, payment1x0_.amount as amount14_0_, payment1x0_1_.credit_card_type as credit2_15_0_, payment1x0_2_.cash_type as cash2_16_0_, case when payment1x0_1_.payment1id is not null then 1 when payment1x0_2_.payment1id is not null then 2 when payment1x0_.payment1id is not null then 0 end as clazz_0_ from iqbyte.payment1 payment1x0_ left outer join iqbyte.credit_card payment1x0_1_ on payment1x0_.payment1id=payment1x0_1_.payment1id left outer join iqbyte.cash payment1x0_2_ on payment1x0_.payment1id=payment1x0_2_.payment1id where payment1x0_.payment1id=?


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 12:21 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
http://www.hibernate.org/280.html

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.