-->
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.  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Hibernate and Inheritance
PostPosted: Wed Mar 03, 2004 10:10 am 
Beginner
Beginner

Joined: Wed Mar 03, 2004 9:44 am
Posts: 23
Location: Brasil
Hi all, I have a class Executive which inherits of the Person class. See below:

/**
*
* Mapping for Person table.
*
* @version $Id: Person.java,v 1.5 2004/03/01 22:43:18 aryjr Exp $
* @author <a href="mailto:junior@valoriza.com.br">Ary Junior</a>
*
* @hibernate.class
* table="Person"
*
*/
public class Person {
...
public void setCompany(Company company) {
this.company = company;
}
/**
*
* @hibernate.many-to-one
* column="CompanyId"
*
* @return br.com.valoriza.imf.site.persistence.Company
*/
public Company getCompany() {
return company;
}
...
}

/**
*
* Mapping for Executive table.
*
* @version $Id: Executive.java,v 1.4 2004/02/26 15:32:07 aryjr Exp $
* @author <a href="mailto:junior@valoriza.com.br">Ary Junior</a>
*
* @hibernate.joined-subclass
* table="Executive"
*
* @hibernate.joined-subclass-key
* column="PersonId"
*/
public class Executive extends Person {
...
}

I have too the Company class which have a collection of executives, see below:

public void setExecutives(Set executives) {
this.executives = executives;
}

/**
*
* @hibernate.set
* role="executives"
* table="Executive"
* order-by="Name ASC"
* cascade="all"
* readonly="true"
* inverse="true"
*
* @hibernate.collection-key
* column="CompanyId"
*
* @hibernate.collection-one-to-many
* class="br.com.valoriza.imf.site.persistence.Executive"
*
* @return java.util.Set
*/
public Set getExecutives() {
return executives;
}

Since the CompanyId column belongs to the table Person and not it Executive table, the Hibernate return the message below:

...
Caused by: java.sql.SQLException: Column not found, message from server: "Unknown column 'executives0_.CompanyId' in 'field list'"
...

Some suggestion? This is a limitation?

[]'s

Ary Junior


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 1:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Show the mapping please

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Hibernate and Inheritance
PostPosted: Wed Mar 03, 2004 2:30 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 9:44 am
Posts: 23
Location: Brasil
Ok, the Company mapping:

...
<class
name="br.com.valoriza.imf.site.persistence.Company"
table="Company"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="CompanyId"
type="int"
>
<generator class="native">
</generator>
</id>

...

<set
name="executives"
table="Executive"
lazy="false"
inverse="true"
cascade="all"
sort="unsorted"
order-by="Nome ASC"
>

<key
column="CompanyId"
/>

<one-to-many
class="br.com.valoriza.imf.site.persistence.Executive"
/>
</set>
...

</class>
...

Now the Person and Executive mapping:

<class
name="br.com.valoriza.imf.site.persistence.Person"
table="Person"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="PersonId"
type="int"
>
<generator class="native">
</generator>
</id>

<many-to-one
name="Company"
class="br.com.valoriza.imf.site.persistence.Company"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="CompanyId"
/>

...

<joined-subclass
name="br.com.valoriza.imf.site.persistence.Executive"
table="Executive"
dynamic-update="false"
dynamic-insert="false"
>
<key
column="PersonId"
/>

</joined-subclass>
...

</class>

Thanks!!!

[]'s

Ary Junior


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 5:37 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Ah,
<key
column="CompanyId"
/>
means you must have a companyId in the Executive table

or company should be linked to Person in the one-to-many mapping

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Hibernate and Inheritance
PostPosted: Wed Mar 03, 2004 5:55 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 9:44 am
Posts: 23
Location: Brasil
But the Company have a collection of Executives and one Responsible (another class), both inherit of Person. The Executive class and the Responsible class have different associations with the class Company however they belong to the same hierarchy. I find that my problem was to have chosen the table-per-class strategy. What you think?


Top
 Profile  
 
 Post subject: Hibernate and Inheritance
PostPosted: Wed Mar 03, 2004 6:01 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 9:44 am
Posts: 23
Location: Brasil
Forgive me, I would have to say the table-per-concrete-class strategy.

Thanks!!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 6:09 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you've a table per concrete class hierarchy, then you should have a CompanyId column in the executive table

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Hibernate and Inheritance
PostPosted: Wed Mar 03, 2004 9:49 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 9:44 am
Posts: 23
Location: Brasil
Why? This would not be redundant? If the Executive class inherits of the class Person, the JVM knows that it also has the method getCompany(). Then, the Hibernate would have to know that the Executive table has the CompanyId field, because it "inherits" of the table Person. Right or wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 4:29 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hum, you mean you have a table for Person and a table for Executive, but both are concrete classes : I've never try this case actually.

Anyway
Quote:
Then, the Hibernate would have to know that the Executive table has the CompanyId field, because it "inherits" of the table Person. Right or wrong?

And it knows that for sure, It asked this column:
Column not found, message from server: "Unknown column 'executives0_.CompanyId' in 'field list'"

The column must be in the Executive table.

Table-per-concrete-class strategy means no link between tables (only linked by classes), and thus redundance in DB

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Hibernate and Inheritance
PostPosted: Fri Mar 05, 2004 8:29 am 
Beginner
Beginner

Joined: Wed Mar 03, 2004 9:44 am
Posts: 23
Location: Brasil
Hi Emmanuel, see below the relation on database:

Image

And the association on UML:

Image

I fear to have expressed badly when I defined adopted strategy. This is a table-per-subclass strategy, not the table-per-concrete-class strategy. I think that hibernate would have to look for the field CompanyId in the table Person and not in the Executive table. In the future this would be possible? Thanks very much for the patience.

[]s

Ary Junior


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 8:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
<class
name="br.com.valoriza.imf.site.persistence.Company"
table="Company"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="CompanyId"
type="int"
>
<generator class="native">
</generator>
</id>

...

<set
name="persons"
table="Person"
lazy="false"
inverse="true"
cascade="all"
sort="unsorted"
order-by="Nome ASC"
>

<key
column="CompanyId"
/>

<one-to-many
class="br.com.valoriza.imf.site.persistence.Person"
/>
</set>
...

</class>
...

Now the Person and Executive mapping:

<class
name="br.com.valoriza.imf.site.persistence.Person"
table="Person"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="PersonId"
type="int"
>
<generator class="native">
</generator>
</id>

<many-to-one
name="Company"
class="br.com.valoriza.imf.site.persistence.Company"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="CompanyId"
/>

...

<joined-subclass
name="br.com.valoriza.imf.site.persistence.Executive"
table="Executive"
dynamic-update="false"
dynamic-insert="false"
>
<key
column="PersonId"
/>

</joined-subclass>
...

</class>


When you'll do myCompany.getPersons().get(0) you'll be able to cast on Executive or Responsable depending the cases


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 9:22 am 
Beginner
Beginner

Joined: Wed Mar 03, 2004 9:44 am
Posts: 23
Location: Brasil
This is not my problem. My problem is that the Hibernate does not know that the CompanyId column belongs to the Person table and not to the Executive table!!!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 9:45 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Have you notices the changes between your mapping and mine?

<set
name="persons"
table="Person" ---> your collection has to be mapped with person not executive
lazy="false"
inverse="true"
cascade="all"
sort="unsorted"
order-by="Nome ASC"
>

<key
column="CompanyId"
/>

<one-to-many
class="br.com.valoriza.imf.site.persistence.Person"
/>
</set>


By doing this, hibernate try to get companyId in executive Table ???

Please don't get excited....


Top
 Profile  
 
 Post subject: Hibernate and Inheritance
PostPosted: Fri Mar 05, 2004 10:05 am 
Beginner
Beginner

Joined: Wed Mar 03, 2004 9:44 am
Posts: 23
Location: Brasil
Please forgive me for the precipitation when answering its topic. Thanks very much by the attention and patience again. But, if I get all persons of Company, I will have that to verify if each person is an executive or not with the "instanceof" operator, right? He would not be ideal if the Hibernate only returned me the executives?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 10:19 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
what about something like HQL:
"select from Executive e where e.company.id = ?"

this try to get companyId from Persone Table or Executive Table?

I must respect the mapping file....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page 1, 2  Next

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.