-->
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: Parent and child(with composite-id)
PostPosted: Thu Jun 28, 2007 5:53 am 
Newbie

Joined: Sat Apr 07, 2007 6:53 am
Posts: 16
Hello Forum, I'm developing a application using Hibernate. My question is the follow:

I have got three entities: Ubicacio, Utilitari and Moviment, each one represent places, articles and movements translated to english.

So, one Ubicacio (place) has more Moviment (movements), and
one Utilitari (article) has more Moviment (movements)
and
Movement is composed by one Ubicacio (place), one Utilitari (article) and a Timestamp date.


* Ubicacio (place) is mapped to UBICACIO table,
* Utilitari (article) is mapped to UBICACIONS_ARTICLES table, and
* Moviment (movement) is mapped to HISTORIC_MOVIMENTS table.

[img]http://i16.photobucket.com/albums/b45/jeusdi/database.png
[/img]

So, the functionality og HISTORIC_MOVIMENTS (Moviment - Movements) is store all movements that refers to Ubicacio and Article. So, one article can be moved from one place to other place --> this action implies to save a movement into HISTORIC_MOVIMENTS as place1, article1, now.

I think that you can understand the situation.

So, when one article is moved from one place to other place I have to store a movement. I though create a Moviments collection into Ubicacio (place) and Utilitari (article) entities. So, when one article is moved, woulb be easy add a movement. It would be as:

utilitari.moviments.Add(new Moviment(...));

but, I have got any idea to implement that.

Code:
   <class name="Model.Utilitari" table="ARTICLES_UBICACIONS">
      <id name="Serie" column="SERIE" type="Int32" unsaved-value="-1">
         <generator class="sequence">
            <param name="sequence">SERIE_SEQUENCE</param>
         </generator>
      </id>
        <many-to-one name="Article" class="Model.Article" column="ARTICLE" lazy="false"/>
        <many-to-one name="Ubicacio" class="Model.Ubicacio" column="UBICACIO" lazy="false"/>
        <set name="Moviments" cascade="save-update" lazy="false" inverse="true">
         <key column="SERIE"/>
         <one-to-many class="Model.Moviment"/>
        </set>
   </class>


Code:
   <class name="Model.Moviment, Model" table="HISTORIC_MOVIMENTS">
      <composite-id name="Codi_intern" class="Model.Moviment_PK">
         <key-property name="Ubicacio" column="UBICACIO" type="Int32"/>
         <key-property name="Utilitari" column="SERIE" type="Int32"/>
         <key-property name="Data_moviment" column="DATA_MOVIMENT" type="timestamp"/>
      </composite-id>
      <many-to-one name="Ubicacio" class="Model.Ubicacio" column="UBICACIO" lazy="false" insert="false" update="false"/>
      <many-to-one name="Utilitari" class="Model.Utilitari" column="SERIE" lazy="false" insert="false" update="false"/>
      <property name="Data_moviment" column="DATA_MOVIMENT" type="timestamp" insert="false" update="false"/>
      <property name="Usuari" column="USUARI" type="Int32"/>
      <property name="Tipus_moviment" column="ACCIO" type="Int32"/>
   </class>


With this mapping files If:

if (utilitari.Ubicacio.Codi_intern != this.codi_ubicacio_utilitari_carregat)
{
Code:
               utilitari.Ubicacio = Model.BussinessBroker.getInstance().getUbicacio(this.codi_ubicacio_utilitari_carregat);
               utilitari.Moviments.Add(new Model.Moviment(utilitari.Ubicacio, utilitari, System.DateTime.Now, 2));

}

hibernate generates only a INSERT INTO HISTORIC_MOVIMENTS stament. It doesn't UPDATE the Ubicacio (place), and it crashes, becouse doesn't exist an Utilitari (article) as this(object) in database, becouse this Utilitari(article) has changed.

Instead of this, if I excute the follow code -->

Code:
Model.BussinessBroker.getInstance().save(utilitari);
            Model.BussinessBroker.getInstance().save(new Model.Moviment(utilitari.Ubicacio, utilitari, System.DateTime.Now, 2));


hibernate generates an UPDATE in order to update Utilitari (article) in database, and one INSERT statement in order to insert a movement into database, and that's correct.

Ohh, excuse me. I have implemented an Interceptor in order to determine if a Moviment have to be saved or updated:

Code:
      public Object IsUnsaved(Object entity)
      {
         if (entity.GetType().Equals(typeof(Model.Moviment)))
         {
            Model.Moviment moviment = (Model.Moviment)entity;
            return moviment.Decision_value == -1;
         }

         return null;
      }


Note, I Moviment (movement) never is modified. Is can help us to design this situation of other form.

I hope that you can help me. Thanks for all.


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.