-->
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.  [ 9 posts ] 
Author Message
 Post subject: One-To-Many-Collections with joined-sublcass
PostPosted: Fri Feb 06, 2004 4:43 am 
Newbie

Joined: Thu Feb 05, 2004 6:04 am
Posts: 5
Hi all,

I have the following problem:

I have a class called Project that has a collection (set) called financeRealTransactions in a one-to-many relation. The referenced class ist FinanceReal and is a joined-subclass of AbstractTransaction. The problem seems to be the the key column that is used in the collection is part of AbstractTransaction and not FinanceReal. Every time I try to access the collection Hibernate throws a "net.sf.hibernate.JDBCException: could not initialize collection". If I run the Schema{Export|Update} tool it tries to create a column "project" to the financereal table. I expected Hibernate to realise that the key column is part of the parent class (AbstractTransaction) and not the child class (FinanceReal).

Am I doing something wrong here? Is there a way to tell Hibernate to look into the AbstractTransaction table? Is this a bug? Or a missing feature? Or am I just thinking into the wrong direction here?

I am using Hibernate 2.1.2. The relevant part of my mapping document follows:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
   <class name="persistence.PersistentObject" table="PERSISTENTOBJECT">
      <id name="id" type="long" unsaved-value="-1">
         <generator class="native"/>
      </id>
      <version column="version" name="version" type="timestamp"/>

      <joined-subclass name="co_objects.Project" table="project">
         <key column="id"/>

         <property name="name" type="string" length="100" not-null="true"/>
         <set name="financeRealTransactions">
            <key column="project"/>
            <one-to-many class="transactions.FinanceReal"/>
         </set>
      </joined-subclass>
      
      <joined-subclass name="transactions.AbstractTransaction" table="abstract_trans">
         <key column="id"/>
         <property name="relevantPointInTime" type="date" not-null="true"/>
         <many-to-one name="project" class="co_objects.Project" not-null="true"/>
         <joined-subclass name="transactions.FinanceReal" table="finance_real">
            <key column="id"/>
            <property name="invoiceDate" type="date"/>
            <property name="invoiceReference" type="string" length="50"/>
         </joined-subclass>
      </joined-subclass>
   </class>
</hibernate-mapping>


Thanks for any help, MadEagle


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 4:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I dunno, seems to me that Hibernate s being quite sane here...

Change to:

Code:
<set name="financeRealTransactions">
        <key column="project"/>
        <one-to-many class="transactions.AbstractTransaction"/>
</set>


If the association is really to the superclass table.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 5:01 am 
Newbie

Joined: Thu Feb 05, 2004 6:04 am
Posts: 5
OK, that would solve the problem but AbstractTransaction has more than one subclass (I ommitted the others to keep the mapping document readable) so this set would then give me all AbstractTransactions and not only FinanceReals, right? I have to admit I didn't test this. Is my assupmtion correct?

Thanks for your answer, MadEagle


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 7:59 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
True

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 8:32 am 
Newbie

Joined: Thu Feb 05, 2004 6:04 am
Posts: 5
Hm, is there any reason why Hibernate behaves that way? From my (pretty uninformed an inexperienced) point of view I would have expected "object oriented" behaviour rather than "table oriented".

MadEagle


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 9:37 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't follow you.
You told Hibernate that AbstractTransaction has a many-to-one with project, this is fine then to set the FK in the AbstractTransaction and not in financial.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 9:56 am 
Beginner
Beginner

Joined: Tue Nov 11, 2003 4:49 am
Posts: 47
Location: Florence, Italy
I overused OO ineritance in Hibernate, against all I read here, and it works GREAT.
I had your same problem, and I solved it this way:
    -make a method in the subclass with the name of the Set key
    -let it call the superclass attribute for the getter, and do NOTHING in the setter
this way the mapping create a column in the Set class that actually is used only by the relationship.
Ciao.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 08, 2004 7:22 am 
Newbie

Joined: Thu Feb 05, 2004 6:04 am
Posts: 5
emmanuel wrote:
You told Hibernate that AbstractTransaction has a many-to-one with project, this is fine then to set the FK in the AbstractTransaction and not in financial.


AbstractTransaction is not one of the classes that I want to use in my business logic, but FinanceReal that extends it is. There is a bunch of classes that extend AbstractTransaction. They all need the many-to-one with project but they all have different attributes that extend AbstractTransaction. Therefore the many-to-one is in AbstractTransaction but if for example I try to get all FinanceReal objects for a project Hibernate tries to look into the FinanceReal table instead of AbstractTransaction.

To model the classes in a way that Hibernate would support would mean to put in every child class of AbstractTransaction the project property which defeats the principles of object oriented programming, right?

MadEagle


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 08, 2004 7:24 am 
Newbie

Joined: Thu Feb 05, 2004 6:04 am
Posts: 5
l.masini wrote:
I overused OO ineritance in Hibernate, against all I read here, and it works GREAT.
I had your same problem, and I solved it this way:
    -make a method in the subclass with the name of the Set key
    -let it call the superclass attribute for the getter, and do NOTHING in the setter
this way the mapping create a column in the Set class that actually is used only by the relationship.


Hm, interesting idea, will try that out and see if it solves my problem. Thanks.

MadEagle


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