-->
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.  [ 13 posts ] 
Author Message
 Post subject: Two one-to-one relationship to the same table
PostPosted: Wed Mar 03, 2004 3:30 pm 
Newbie

Joined: Thu Dec 11, 2003 12:10 pm
Posts: 16
I have 3 classes.
Account, LimitedAccount and Application
LimitedAccount extends Account and only overwrite some methods.

and all classes are stored using hibernate

My mappings:

Code:
<class name="nl.strohalm.cyclos.entities.accounts.IAccountOwner" table="members">
        <id name="id" type="long" column="member_id">
            <generator class="native"/>
        </id>
        <discriminator column="subclass" type="string"/>
       
    <subclass name="nl.strohalm.cyclos.entities.application.Application">
        <one-to-one name="bankAccount"/>
        <one-to-one name="communityAccount"/>
    </subclass>
</class>


Code:
<class name="nl.strohalm.cyclos.entities.accounts.Account" table="accounts">
      <id name="id" type="long" column="account_id">
            <generator class="native"/>
      </id>
      <discriminator column="subclass" type="string"/>
      <property name="creationDate" column="creationDate" type="calendar"/>
      <property name="lastAmount" column="lastAmount" type="float"/>
      
      <many-to-one name="owner" column="owner_id" class="nl.strohalm.cyclos.entities.accounts.IAccountOwner"/>   
      
      <subclass name="nl.strohalm.cyclos.entities.accounts.LimitedAccount"/>
</class>


If I put 'property-ref="owner"' in one-to-one relationships Hibernate can't find protected property owner into LimitedAccount although this class extends Account!!!

When I load the application instance I get this exception:

Code:
net.sf.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: nl.strohalm.cyclos.entities.accounts.LimitedAccount (loaded object was of wrong class)


Reading the forum I find some soluctions as "lightweight class pattern" in others....
but these don't solve my problem!!

My question is: Can't I have 2 one-to-one relationships to inherited classes mapped in the same table????


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 3:38 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Try explicitly mapping the types of the associations.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 3:45 pm 
Newbie

Joined: Thu Dec 11, 2003 12:10 pm
Posts: 16
Sorry!!

Code:
<class name="nl.strohalm.cyclos.entities.accounts.IAccountOwner" table="members">
        <id name="id" type="long" column="member_id">
            <generator class="native"/>
        </id>
        <discriminator column="subclass" type="string"/>
       
    <subclass name="nl.strohalm.cyclos.entities.application.Application">
        <one-to-one name="bankAccount" class="nl.strohalm.cyclos.entities.accounts.Account"/>
        <one-to-one name="communityAccount" class="nl.strohalm.cyclos.entities.accounts.LimitedAccount"/>
    </subclass>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 3:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Does not change anything? Please show the stacktrace and log ... the relations in the database are set up correctly (all fks match etc)?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 4:07 pm 
Newbie

Joined: Thu Dec 11, 2003 12:10 pm
Posts: 16
My complete stack Trace!!


Code:
net.sf.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: nl.strohalm.cyclos.entities.accounts.LimitedAccount (loaded object was of wrong class)
   at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:456)
   at net.sf.hibernate.loader.Loader.getRow(Loader.java:423)
   at net.sf.hibernate.loader.Loader.doQuery(Loader.java:209)
   at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
   at net.sf.hibernate.loader.Loader.doList(Loader.java:950)
   at net.sf.hibernate.loader.Loader.list(Loader.java:941)
   at net.sf.hibernate.loader.CriteriaLoader.list(CriteriaLoader.java:118)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3571)
   at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:223)



All relations in the database are seted up correctly!!

In log this is my query:
Code:
Hibernate: select this.member_id as member_id7_, account1_.account_id as account_id0_, account1_.subclass as subclass0_, account1_.creationDate as creation3_0_, account1_.lastAmount as lastAmount0_, account1_.owner_id as owner_id0_, account2_.account_id as account_id1_, account2_.subclass as subclass1_, account2_.creationDate as creation3_1_, account2_.lastAmount as lastAmount1_, account2_.owner_id as owner_id1_, iaccountow3_.member_id as member_id2_, iaccountow3_.subclass as subclass2_, iaccountow3_.postalCode as postalCode2_, iaccountow3_.telephone1 as telephone12_, iaccountow3_.telephone2 as telephone22_, iaccountow3_.city as city2_, iaccountow3_.email as email2_, iaccountow3_.address as address2_, iaccountow3_.firstName as firstName2_, iaccountow3_.lastName as lastName2_, iaccountow3_.enabled as enabled2_, iaccountow3_.creationDate as creatio12_2_, iaccountow3_.group_admin_id as group_a13_2_, iaccountow3_.province as province2_, iaccountow3_.hideAddress as hideAdd15_2_, iaccountow3_.hideBirthday as hideBir16_2_, iaccountow3_.hideGender as hideGender2_, iaccountow3_.hidePostalCode as hidePos18_2_, iaccountow3_.hideTelephone1 as hideTel19_2_, iaccountow3_.hideTelephone2 as hideTel20_2_, iaccountow3_.hideFax as hideFax2_, iaccountow3_.url as url2_, iaccountow3_.fax as fax2_, iaccountow3_.keywords as keywords2_, iaccountow3_.gender as gender2_, iaccountow3_.birthDay as birthDay2_, iaccountow3_.group_member_id as group_m27_2_, iaccountow3_.area_id as area_id2_, groupadmin4_.group_id as group_id3_, groupadmin4_.name as name3_, groupmembe5_.group_id as group_id4_, groupmembe5_.name as name4_, area6_.area_id as area_id5_, area6_.name as name5_, limitedacc7_.account_id as account_id6_, limitedacc7_.creationDate as creation3_6_, limitedacc7_.lastAmount as lastAmount6_, limitedacc7_.owner_id as owner_id6_ from members this left outer join accounts account1_ on this.member_id=account1_.owner_id left outer join accounts account2_ on account1_.account_id=account2_.account_id left outer join members iaccountow3_ on account2_.owner_id=iaccountow3_.member_id left outer join groups groupadmin4_ on iaccountow3_.group_admin_id=groupadmin4_.group_id left outer join groups groupmembe5_ on iaccountow3_.group_member_id=groupmembe5_.group_id left outer join areas area6_ on iaccountow3_.area_id=area6_.area_id left outer join accounts limitedacc7_ on this.member_id=limitedacc7_.account_id where 1=1 and this.subclass='nl.strohalm.cyclos.entities.application.Application'


No more entries in log becouse this is the first select of application!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 4:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Are you sure the fk in communityAccount does not refer to a row which is a Account?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 4:32 pm 
Newbie

Joined: Thu Dec 11, 2003 12:10 pm
Posts: 16
Yes, I'm sure becouse communityAccount is a LimitedAccount instance

In Application class:
Code:
private Account bankAccount;
private LimitedAccount communityAccount;


With related getters and setters!


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

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please check the row with id 1 in the accounts table, is there really the discriminator for LimitedAccount in the "subclass" column?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 4:53 pm 
Newbie

Joined: Thu Dec 11, 2003 12:10 pm
Posts: 16
No becouse the row with id = 1 is the bankAccount with subclass = Account

Code:
+------------+-----------------------------------------------------+---------------------+------------+----------+
| account_id | subclass                                            | creationDate        | lastAmount | owner_id |
+------------+-----------------------------------------------------+---------------------+------------+----------+
|          1 | nl.strohalm.cyclos.entities.accounts.Account        | 2004-03-03 16:19:10 |          0 |        1 |
|          2 | nl.strohalm.cyclos.entities.accounts.LimitedAccount | 2004-03-03 16:19:10 |          0 |        1 |


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

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I really suspect the association is messed up. Please show the record in the other table


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 8:08 am 
Newbie

Joined: Thu Dec 11, 2003 12:10 pm
Posts: 16
This is the only row with subclass = Application

Code:
mysql> select member_id, subclass from members;
+-----------+-----------------------------------------------------+
| member_id | subclass                                            |
+-----------+-----------------------------------------------------+
|         1 | nl.strohalm.cyclos.entities.application.Application |


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

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Object with id 1was not of the specified subclass: nl.strohalm.cyclos.entities.accounts.LimitedAccount

and
1 | nl.strohalm.cyclos.entities.accounts.Account

This is expected.

Should be
1 | nl.strohalm.cyclos.entities.accounts.LimitedAccount

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 7:58 am 
Newbie

Joined: Thu Dec 11, 2003 12:10 pm
Posts: 16
Ok, I just agree this!!

To solve this problem I tryed to use property-ref in one-to-one relations,
but I get other problem:

Please read my other thread in forum:
http://forum.hibernate.org/viewtopic.php?t=928645


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