-->
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: many-ton-one problem
PostPosted: Wed Jul 08, 2009 1:18 pm 
Newbie

Joined: Thu Jan 22, 2009 2:49 am
Posts: 13
Hi

I am having 2 classes with Bussiness and TaxOffice with many-to-one assossiation
Bussiness
Code:
<class name="Bussiness" table="BUSSINESS">

        <id name="id" type="integer" column="BUSSINESS_ID">
            <generator class="identity"/>
        </id>

        <property name="afm" column="AFM" type="string" not-null="true"/>
        <property name="name" column="NAME" type="string" not-null="true"/>

        <many-to-one name="taxOffice"
                    class="TaxOffice"
                    lazy="false"
                    column="TAX_OFFICE_ID"/>
</class>

TaxOffice
Code:
<class name="TaxOffice" table="TAX_OFFICE">

        <id name="id" type="integer" column="TAX_OFFICE_ID">
            <generator class="identity"/>
        </id>

        <property name="code" column="TAX_OFFICE_CODE" type="string"/>
        <property name="name" column="TAX_OFFICE_NAME" type="string" not-null="true"/>
       
</class>


I want sometimes an object bussiness of Bussiness class not have an object of TaxOffice class so when
am i giving bussiness.setTaxOffice(null) am getting the following error

Quote:
Exception occurred during event dispatching:
org.hibernate.TransientObjectException: object references an unsaved transient i
nstance - save the transient instance before flushing: TaxOffice
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(Fore
ignKeys.java:242)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:430)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:619)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(Abst
ractEntityPersister.java:3151)....


wich means that hibernate tries to update the TaxOffice object first.
I have tried to change the properties on many-to-one like this
Code:
<many-to-one name="taxOffice"
                    class="TaxOffice"
                    lazy="false"
                    update="false"
                    insert="false"
                    column="TAX_OFFICE_ID"/>

the message from hibarnate disappears but the column on TAX_OFFICE_ID on table BUSSINESS is always null and
object never assosiated.

How can i have the many-to-one assosiation with the ability an object of Bussiness same times have a TaxOffice and sometimes not?


Top
 Profile  
 
 Post subject: Re: many-ton-one problem
PostPosted: Thu Jul 09, 2009 3:10 am 
Newbie

Joined: Thu Jul 09, 2009 3:06 am
Posts: 1
such a really wonderful information for me................thanks you,i am very happy that know wonderful info.

_________________
Resveratrol


Top
 Profile  
 
 Post subject: Re: many-ton-one problem
PostPosted: Thu Jul 09, 2009 3:30 am 
Newbie

Joined: Thu Jan 22, 2009 2:49 am
Posts: 13
thanx but the problem still remains


Top
 Profile  
 
 Post subject: Re: many-ton-one problem
PostPosted: Fri Jul 10, 2009 8:33 am 
Newbie

Joined: Fri May 29, 2009 2:21 am
Posts: 12
Hey,

As u said that sometimes u want ur "Business" to get saved with associated "TaxOffice" and sometimes having Taxoffice = null u just have to set "cascade=all" in ur Business hbm such as below.

Code:
<class name="Bussiness" table="BUSSINESS">

        <id name="id" type="integer" column="BUSSINESS_ID">
            <generator class="identity"/>
        </id>

        <property name="afm" column="AFM" type="string" not-null="true"/>
        <property name="name" column="NAME" type="string" not-null="true"/>

        <many-to-one name="taxOffice"
                    class="TaxOffice"
                    lazy="false"
                    column="TAX_OFFICE_ID"
                    [b]cascade="all"[/b]/>
</class>



and just try saving Business obj in ur session i.e. session.save(Business).
It should work as expected.

cheers
Nitesh


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.