-->
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.  [ 1 post ] 
Author Message
 Post subject: Issue with subclasses and one-to-one
PostPosted: Wed Jun 01, 2005 9:21 am 
Newbie

Joined: Fri Jan 23, 2004 3:37 pm
Posts: 14
Hi all!
This post an issue reporting, not help support.

I'm using Hibernate 3.0.5, and have a class hierarchy like this:
IAccountOwner: top level
Application: extends IAccountOwner and has a bag of accounts
BasicElement: extends IAccountOwner and don't direclty relates to account
Administrator: extends BasicElement and don't direclty relates to account
Member: extends BasicElement and as a one-to-one relation to account.

When I try to load any of the concrete classes (Application, Administrator or Member), all works fine, but, when I try to load an instance of IAccountOwner where the id is an Application (has N accounts), an error occours, saying that more than one records were found with the same id.

I've noticed that it was happening because of the one-to-one relationship between Member to Account. Hibernate tried to load any IAccountOwner using that one-to-one, but Application had multiple accounts. Got it?

That same mapping was working on Hibernate 2.1. I fixed it using fetch="select" on the one-to-one, but, when I load a Member specifically, I would like the fetch to be join. I just think that Hibernate could not assume that one-to-one when loading parent class instances.

Best regards,
Luis.

Hibernate version: 3.0.5

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false">

<class name="nl.strohalm.cyclos.entities.accounts.IAccountOwner" table="members" abstract="true">
<id name="id" type="long" column="member_id" unsaved-value="null">
<generator class="native"/>
</id>
<discriminator column="subclass" type="string" force="true"/>

<subclass name="nl.strohalm.cyclos.entities.application.Application" discriminator-value="application">

<bag name="accounts" lazy="false" fetch="select" >
<key column="owner_id"/>
<one-to-many class="nl.strohalm.cyclos.entities.accounts.Account"/>
</bag>

<component name="invoices" class="nl.strohalm.cyclos.entities.accounts.InvoiceList">
<bag name="invoicesList" lazy="true">
<key column="from_account_id"/>
<one-to-many class="nl.strohalm.cyclos.entities.accounts.Invoice"/>
</bag>
</component>
</subclass>

<subclass name="nl.strohalm.cyclos.entities.members.BasicElement" abstract="true">
<property name="postalCode" column="postalCode" type="string"/>
<property name="telephone1" column="telephone1" type="string"/>
<property name="telephone2" column="telephone2" type="string"/>
<property name="email" column="email" type="string"/>
<property name="address" column="address" type="string"/>
<property name="firstName" column="firstName" type="string"/>
<property name="lastName" column="lastName" type="string"/>
<property name="creationDate" column="creationDate" type="calendar"/>

<one-to-one name="user" class="nl.strohalm.cyclos.entities.access.BasicUser" />
<many-to-one name="group" column="group_id" class="nl.strohalm.cyclos.entities.access.Group"/>

<bag name="alertList" lazy="true" cascade="all">
<key column="member"/>
<one-to-many class="nl.strohalm.cyclos.entities.application.alerts.AccountAlert"/>
</bag>

<subclass name="nl.strohalm.cyclos.entities.members.Administrator" discriminator-value="admin">
</subclass>

<subclass name="nl.strohalm.cyclos.entities.members.Member" discriminator-value="member">
<property name="province" column="province" type="string"/>

<property name="hideAddress" column="hideAddress" type="boolean"/>
<property name="hideBirthday" column="hideBirthday" type="boolean"/>
<property name="hideGender" column="hideGender" type="boolean"/>
<property name="hidePostalCode" column="hidePostalCode" type="boolean"/>
<property name="hideTelephone1" column="hideTelephone1" type="boolean"/>
<property name="hideTelephone2" column="hideTelephone2" type="boolean"/>
<property name="hideFax" column="hideFax" type="boolean"/>

<property name="url" column="url" type="string"/>
<property name="fax" column="fax" type="string"/>
<property name="gender" column="gender" type="byte"/>
<property name="birthDay" column="birthDay" type="calendar"/>

<property name="field1" column="field1" type="string"/>
<property name="field2" column="field2" type="string"/>
<property name="field3" column="field3" type="string"/>
<property name="field4" column="field4" type="string"/>
<property name="field5" column="field5" type="string"/>

<many-to-one name="area" column="area_id" class="nl.strohalm.cyclos.entities.application.location.Area"/>
<many-to-one name="businessType" column="businessType_id" class="nl.strohalm.cyclos.entities.members.businessType.BusinessType"/>
<property name="city" column="city" type="string"/>
<many-to-one name="broker" column="broker_id" class="nl.strohalm.cyclos.entities.members.broker.Brokering"/>

<one-to-one name="account" class="nl.strohalm.cyclos.entities.accounts.Account" property-ref="owner" fetch="select"/>

<component name="referenceList" class="nl.strohalm.cyclos.entities.members.references.ReferenceList">
<bag name="given" lazy="true">
<key column="from_member_id"/>
<one-to-many class="nl.strohalm.cyclos.entities.members.references.Reference"/>
</bag>
<bag name="received" lazy="true">
<key column="to_member_id"/>
<one-to-many class="nl.strohalm.cyclos.entities.members.references.Reference"/>
</bag>
</component>

<component name="contactList" class="nl.strohalm.cyclos.entities.members.contacts.ContactList">
<bag name="contacts" lazy="true">
<key column="member_id"/>
<one-to-many class="nl.strohalm.cyclos.entities.members.contacts.Contact"/>
</bag>
</component>

<component name="adsList" class="nl.strohalm.cyclos.entities.members.ads.AdsList">
<bag name="ads" lazy="true">
<key column="member_id"/>
<one-to-many class="nl.strohalm.cyclos.entities.members.ads.Ad"/>
</bag>
</component>

<bag name="moneyContributionsPayed" lazy="true">
<key column="member_id"/>
<one-to-many class="nl.strohalm.cyclos.entities.contributions.MoneyContributionHistory"/>
</bag>

<component name="invoices" class="nl.strohalm.cyclos.entities.accounts.InvoiceList">
<bag name="invoicesList" lazy="true">
<key column="from_account_owner_id"/>
<one-to-many class="nl.strohalm.cyclos.entities.accounts.Invoice"/>
</bag>
</component>

</subclass>

</subclass>

</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Session session = PersistenceFactory.getSession();
try {
try {
System.out.println("AccountOwner");
System.out.println(session.load(IAccountOwner.class, new Long(1)));
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("Application");
System.out.println(session.load(Application.class, new Long(1)));
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("Administrator");
System.out.println(session.load(Administrator.class, new Long(2)));
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("Member");
System.out.println(session.load(Member.class, new Long(3)));
} catch (Exception e) {
e.printStackTrace();
}
} finally {
session.close();
}

Full stack trace of any exception that occurs:
org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: nl.strohalm.cyclos.entities.accounts.IAccountOwner
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:137)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:101)
at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersister.java:2471)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:351)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:332)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:113)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:151)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:79)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:603)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:596)
at nl.strohalm.cyclos.Main.main(Main.java:56)

Name and version of the database you are using:
MySQL 4.1

The generated SQL (show_sql=true):
select iaccountow0_.member_id as member1_8_, iaccountow0_.postalCode as postalCode14_8_, iaccountow0_.telephone1 as telephone4_14_8_, iaccountow0_.telephone2 as telephone5_14_8_, iaccountow0_.email as email14_8_, iaccountow0_.address as address14_8_, iaccountow0_.firstName as firstName14_8_, iaccountow0_.lastName as lastName14_8_, iaccountow0_.creationDate as creatio10_14_8_, iaccountow0_.group_id as group11_14_8_, iaccountow0_.province as province14_8_, iaccountow0_.hideAddress as hideAdd13_14_8_, iaccountow0_.hideBirthday as hideBir14_14_8_, iaccountow0_.hideGender as hideGender14_8_, iaccountow0_.hidePostalCode as hidePos16_14_8_, iaccountow0_.hideTelephone1 as hideTel17_14_8_, iaccountow0_.hideTelephone2 as hideTel18_14_8_, iaccountow0_.hideFax as hideFax14_8_, iaccountow0_.url as url14_8_, iaccountow0_.fax as fax14_8_, iaccountow0_.gender as gender14_8_, iaccountow0_.birthDay as birthDay14_8_, iaccountow0_.field1 as field24_14_8_, iaccountow0_.field2 as field25_14_8_, iaccountow0_.field3 as field26_14_8_, iaccountow0_.field4 as field27_14_8_, iaccountow0_.field5 as field28_14_8_, iaccountow0_.area_id as area29_14_8_, iaccountow0_.businessType_id as busines30_14_8_, iaccountow0_.city as city14_8_, iaccountow0_.broker_id as broker32_14_8_, iaccountow0_.subclass as subclass8_, basicuser1_.user_id as user1_0_, basicuser1_.password as password18_0_, basicuser1_.userName as userName18_0_, basicuser1_.lastLogin as lastLogin18_0_, basicuser1_.subclass as subclass0_, group2_.group_id as group1_1_, group2_.name as name11_1_, group2_.description as descript4_11_1_, group2_.subclass as subclass1_, area3_.area_id as area1_2_, area3_.name as name19_2_, businessty4_.businessType_id as business1_3_, businessty4_.name as name20_3_, brokering5_.brokering_id as brokering1_4_, brokering5_.broker_id as broker2_21_4_, brokering5_.brokered_id as brokered3_21_4_, brokering5_.note as note21_4_, brokering5_.commissionDate as commissi5_21_4_, member6_.member_id as member1_5_, member6_.postalCode as postalCode14_5_, member6_.telephone1 as telephone4_14_5_, member6_.telephone2 as telephone5_14_5_, member6_.email as email14_5_, member6_.address as address14_5_, member6_.firstName as firstName14_5_, member6_.lastName as lastName14_5_, member6_.creationDate as creatio10_14_5_, member6_.group_id as group11_14_5_, member6_.province as province14_5_, member6_.hideAddress as hideAdd13_14_5_, member6_.hideBirthday as hideBir14_14_5_, member6_.hideGender as hideGender14_5_, member6_.hidePostalCode as hidePos16_14_5_, member6_.hideTelephone1 as hideTel17_14_5_, member6_.hideTelephone2 as hideTel18_14_5_, member6_.hideFax as hideFax14_5_, member6_.url as url14_5_, member6_.fax as fax14_5_, member6_.gender as gender14_5_, member6_.birthDay as birthDay14_5_, member6_.field1 as field24_14_5_, member6_.field2 as field25_14_5_, member6_.field3 as field26_14_5_, member6_.field4 as field27_14_5_, member6_.field5 as field28_14_5_, member6_.area_id as area29_14_5_, member6_.businessType_id as busines30_14_5_, member6_.city as city14_5_, member6_.broker_id as broker32_14_5_, member7_.member_id as member1_6_, member7_.postalCode as postalCode14_6_, member7_.telephone1 as telephone4_14_6_, member7_.telephone2 as telephone5_14_6_, member7_.email as email14_6_, member7_.address as address14_6_, member7_.firstName as firstName14_6_, member7_.lastName as lastName14_6_, member7_.creationDate as creatio10_14_6_, member7_.group_id as group11_14_6_, member7_.province as province14_6_, member7_.hideAddress as hideAdd13_14_6_, member7_.hideBirthday as hideBir14_14_6_, member7_.hideGender as hideGender14_6_, member7_.hidePostalCode as hidePos16_14_6_, member7_.hideTelephone1 as hideTel17_14_6_, member7_.hideTelephone2 as hideTel18_14_6_, member7_.hideFax as hideFax14_6_, member7_.url as url14_6_, member7_.fax as fax14_6_, member7_.gender as gender14_6_, member7_.birthDay as birthDay14_6_, member7_.field1 as field24_14_6_, member7_.field2 as field25_14_6_, member7_.field3 as field26_14_6_, member7_.field4 as field27_14_6_, member7_.field5 as field28_14_6_, member7_.area_id as area29_14_6_, member7_.businessType_id as busines30_14_6_, member7_.city as city14_6_, member7_.broker_id as broker32_14_6_, account8_.account_id as account1_7_, account8_.creationDate as creation3_2_7_, account8_.ownerName as ownerName2_7_, account8_.owner_id as owner5_2_7_, account8_.credit_limit as credit6_2_7_, account8_.subclass as subclass7_ from members iaccountow0_ left outer join users basicuser1_ on iaccountow0_.member_id=basicuser1_.user_id left outer join groups group2_ on iaccountow0_.group_id=group2_.group_id left outer join areas area3_ on iaccountow0_.area_id=area3_.area_id left outer join businessTypes businessty4_ on iaccountow0_.businessType_id=businessty4_.businessType_id left outer join brokering brokering5_ on iaccountow0_.broker_id=brokering5_.brokering_id left outer join members member6_ on brokering5_.broker_id=member6_.member_id left outer join members member7_ on brokering5_.brokered_id=member7_.member_id left outer join accounts account8_ on iaccountow0_.member_id=account8_.owner_id and account8_.subclass in ('account', 'limited_account') where iaccountow0_.member_id=? and iaccountow0_.subclass in ('admin', 'member', 'application')


Debug level Hibernate log excerpt:


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.