-->
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: Hibernate return the abstract class
PostPosted: Thu Nov 17, 2005 7:07 am 
Newbie

Joined: Thu Nov 17, 2005 6:58 am
Posts: 2
Hi, I have this mapping:

<hibernate-mapping package="model">
<class name="Partner" table="partners" abstract="true">
<id name="id" type="long" column="id">
<generator class="hilo"/>
</id>

<discriminator column="partnerType" type="string" force="true"/>

<property name="nameDisplay"/>
</class>

<subclass name="Contact" extends="Partner" discriminator-value="contact">
<join table="contacts">
<key column="partners_id"/>

<property name="nameFirst"/>
<property name="nameMiddle"/>
<property name="nameLast"/>
<many-to-one name="company" column="company_id"/>
</join>
</subclass>

<subclass name="Company" extends="Partner" discriminator-value="company">
<join table="company">
<key column="partners_id"/>

<property name="nameOfficial"/>
</join>
</subclass>

<class name="Invoice" table="invoices">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>

<many-to-one name="partner" column="partners_id" fetch="join"/>
<property name="summary"/>
</class>

The class Partner is really abstract:

public abstract class Partner {
private Long id;
private String nameDisplay;

public Long getId() {
return id;
}

public String getNameDisplay() {
return nameDisplay;
}

private void setId(Long id) {
this.id = id;
}

public void setNameDisplay(String nameDisplay) {
this.nameDisplay = nameDisplay;
}
}

But in this code I have ClassCastException:

try {
tx = session.beginTransaction();
Query q = session.createQuery(
"from Invoice"
);
result = q.list();
tx.commit();
} catch (HibernateException he) {
if (tx != null) {
tx.rollback();
}
throw he;
}

Invoice invoice = (Invoice) result.get(0);
Partner partner;
partner = invoice.getPartner();
System.out.println(((Contact) partner).getNameFirst()); // !!!!!!! Exception

I need to get the objects of subclasses such as Contact or Company. Please, say me where is my error... =(


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 11:17 am 
Newbie

Joined: Tue Nov 15, 2005 5:41 am
Posts: 16
Before you cast the object, what is the object's type?

You can just print it before trying the cast


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 11:38 am 
Newbie

Joined: Thu Nov 17, 2005 6:58 am
Posts: 2
Neil T wrote:
Before you cast the object, what is the object's type?

You can just print it before trying the cast


Class Invoice has method:
Code:
public Partner getPartner() { ... }


This method return all Partner objects associated with this Invoice. But the Partner class is abstract! Then I try to cast real type program throw exception.

Hibernate load the Partner classes from multiple tables, but I cant resolve the real type of class. The method:
Code:
invoice.getPartner().getClass().getName()

everything return "Partner". And i lost the real type of objects.

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 12:00 pm 
Newbie

Joined: Tue Nov 15, 2005 5:41 am
Posts: 16
have you tried using interfaces? I have a similar set up (but using joined-sublclasses with their own tables). I can cast with no problems...

I specify this kind of thing:

<class name="org.thorne.lettings.referencedata.api.Contact" table="contact">
<id name="id" column="id" unsaved-value="-1">
<generator class="identity"/>
</id>
<component name="personName" class="org.thorne.lettings.referencedata.api.PersonNameImpl">
<property name="firstName" column="first_name"/>
<property name="secondName" column="second_name"/>
<property name="middleName1" column="middle_name_1"/>
<property name="middleName2" column="middle_name_2"/>
<many-to-one name="title" class="org.thorne.lettings.referencedata.api.TitleImpl" column="title_id_fk" unique="true" cascade="none" />
</component>
<one-to-one name="address" class="org.thorne.lettings.referencedata.api.AddressImpl" property-ref="contact" cascade="all"/>
<one-to-one name="homeNumber" class="org.thorne.lettings.referencedata.api.TelephoneNumberImpl" cascade="all" />
<one-to-one name="workNumber" class="org.thorne.lettings.referencedata.api.TelephoneNumberImpl" cascade="all"/>
<one-to-one name="mobileNumber" class="org.thorne.lettings.referencedata.api.TelephoneNumberImpl" cascade="all"/>
<joined-subclass name="org.thorne.lettings.referencedata.api.TenantImpl" table="tenant">
<key column="id"/>
<many-to-one name="maritalStatus" class="org.thorne.lettings.referencedata.api.MaritalStatusImpl" column="marital_status_id_fk" unique="true" cascade="none" />
<property name="pets" column="has_pets"/>
<property name="petDescription" column="pet_description"/>
<property name="smoker" column="is_smoker"/>
<property name="children" column="has_children"/>
<component name="employmentDetails" class="org.thorne.lettings.referencedata.api.EmploymentDetailsImpl" >
<many-to-one name="employmentType" class="org.thorne.lettings.referencedata.api.EmploymentTypeImpl" column="employment_type_id_fk" unique="true" cascade="none" />
<property name="occupation"/>
<many-to-one name="employer" class="org.thorne.lettings.referencedata.api.EmployerImpl" column="employer_id_fk" unique="true" cascade="none" />
</component>
</joined-subclass>
<joined-subclass name="org.thorne.lettings.referencedata.api.EmployerImpl" table="employer">
<key column="id"/>
<set name="employees" cascade="none" inverse="true" >
<key column="employer_id_fk"/>
<one-to-many class="org.thorne.lettings.referencedata.api.TenantImpl"/>
</set>
</joined-subclass>
</class>

That is I have an interface called Contact, (which is implemented by ContactImpl) and two concrete subclasses EmployerImpl and TenantImpl.

Notice that I never use ContactImpl in my mapping - just the interface Contact.

Try introducing an interface and use that in your mapping class.


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.