-->
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: Null foreing key on many-to-one
PostPosted: Sun Nov 20, 2005 1:19 pm 
Newbie

Joined: Fri Nov 18, 2005 11:44 am
Posts: 6
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Mapping documents:
CATEGORY:
Code:
    <class
        name="com.eaf.model.TemparioPartiDanno"
        table="atm_tempario_parti_danno"
        select-before-update="false"
        dynamic-update="true"
        dynamic-insert="true"
        optimistic-lock="all"
    >
      <id
            name="codTParteDanno"
            column="cod_t_parte_danno"
            type="java.lang.String"
            length="3"
         unsaved-value="undefined"
        >
            <generator class="assigned">
            </generator>
....
    </class>
</hibernate-mapping>

ITEM:
Code:
<hibernate-mapping>
    <class
        name="com.eaf.model.AtmFattDettaglioCommesse"
        table="atm_fatt_dettaglio_commesse"
        select-before-update="false"
        dynamic-update="false"
        dynamic-insert="false"
        optimistic-lock="version"
    >
      <id
            name="idFattDettaglioCommesse"
            column="id_fatt_dettaglio_commesse"
            type="java.lang.Integer"
        >
            <generator class="increment">
            </generator>
        </id>
....

      </many-to-one>
    <!-- imported bi-directional many-to-one association to TemparioPartiDanno -->
      <many-to-one
         name="TemparioPartiDanno"
         class="com.eaf.model.TemparioPartiDanno"
            not-null="false"
         outer-join="auto"
      >   
         <column name="cod_t_parte_danno" />
      </many-to-one>

    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

I am using Appfuse with Spring to manage hibernate sessions

Full stack trace of any exception that occurs:

Code:
[eaf] DEBUG [http-8080-Processor24] AtmFattDettaglioCommesseDAOHibernate.saveAtmFattDettaglioCommesse(75) | atmFattDettaglioCommesse's id: 9
[eaf] ERROR [http-8080-Processor24] AbstractFlushingEventListener.performExecutions(277) | Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.eaf.model.TemparioPartiDanno
   at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
   at org.hibernate.type.EntityType.getIdentifier(EntityType.java:99)
   at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:63)
   at org.hibernate.persister.entity.BasicEntityPersister.dehydrate(BasicEntityPersister.java:1617)
   at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:1963)
   at org.hibernate.persister.entity.BasicEntityPersister.updateOrInsert(BasicEntityPersister.java:1909)
   at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2149)
   at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:75)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) ...


Name and version of the database you are using:

Postgres 8.0

The generated SQL (show_sql=true):
n/a
Debug level Hibernate log excerpt:
n/a


The question, in short is:
I have CATEGORY table , with an assigned generator.
I have a ITEM table, that may have a CATEGORY, but may also have NO categorization, thus requiring a NULL foreing key to category.

Maybe I'm doing something wrong, but I cannot get hibernate accept a null foreing key on this relation since I always get the above exception.
Is there a way to avoid this?
Thanks in advance.

Francesco Ronchi


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 20, 2005 2:42 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
There is a property of type TemparioPartiDanno on whatever instance you are trying to update. This property is not referencing a TemparioPartiDanno that is in persistent state (it is detached or transient). Bring it into persistent state ("save the transient instance before flushing") before the update.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 7:14 am 
Newbie

Joined: Fri Nov 18, 2005 11:44 am
Posts: 6
Actually, I set the property to "NULL", so I can't understand where is this "transient" or unsaved object.
All I need is to have the property set to null, but it seems that null values to the foreing key are interpreted as a transient object.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 6:36 am 
Newbie

Joined: Fri Nov 18, 2005 11:44 am
Posts: 6
dennisbyrne wrote:
This property is not referencing a TemparioPartiDanno that is in persistent state (it is detached or transient). Bring it into persistent state ("save the transient instance before flushing") before the update.


You are right! I was going crazy searching the problem within the XML data definition files, while the problem was in the Java class.
The Jave code created by AppFuse Generator contained this for each many-to one mapped property:
Code:
   public com.eaf.model.TipiDanno getTipiDanno () {
   // instantiate a new association entity if it is null
      if (tipiDanno == null) {
         tipiDanno = new com.eaf.model.TipiDanno ();
      }
      return tipiDanno;
   }   


Beside the fact that I cannot figure why one should create an object in a getter method, i changed a bit the code and now null foreing keys are working just fine.

For the mental health of other people with the same problem, here is how i changed my code:
Code:
   public com.eaf.model.TipiDanno getTipiDanno () {
      return tipiDanno;
   }   


   public String getCodTipoDanno() {
      if(this.getTipiDanno()!=null)
         return this.getTipiDanno().getCodTipoDanno();
      else
               return null;
   }
   
  /**
   * Set the codTipoDanno
   */   
   public void setCodTipoDanno(String aValue) {
      if(this.getTipiDanno()!=null)
         this.getTipiDanno().setCodTipoDanno(aValue);
      else if(aValue !=null) {
         this.tipiDanno = new TipiDanno();
         this.tipiDanno.setCodTipoDanno(aValue);
      }
   }


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.