-->
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.  [ 7 posts ] 
Author Message
 Post subject: Reloading ONE TO ONE...!!!
PostPosted: Wed Sep 28, 2005 2:11 pm 
Beginner
Beginner

Joined: Thu Jul 14, 2005 10:11 am
Posts: 32
Hibernate version: 3.0.5 !!

Mapping documents:

Cliente.hbm.xml....:
Code:
         <property name="idEmpresa" column="idEmpresa" not-null="true" type="integer"/> 
       
      <many-to-one name="Empresa" column="idEmpresa" class="clientes.Empresa" update="false" insert="false" />



Empresa.hbm.xml...:

Code:
      <bag name="Empleados" inverse="false">
         <key column="idEmpresa" update="false" />
         <one-to-many class="clientes.Cliente" />
      </bag>


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

Code:
session.merge(o);
session.flush();
session.refresh(o);   




The problem is that...
- Loading objects works fine. If i load a Client, Hibernate instantiates the Enterprise... but...
- If i update a client, hibernate won't refresh the Enterprise object. I keep the id, but the >> object << is not refreshed.

Ideas!?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 5:02 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
I believe this is due to your cascade attribute.

Maybe you can post you mapping documents entirely, but this is an excerpt from the documentation:

Quote:
For each basic operation of the Hibernate session - including persist(), merge(), saveOrUpdate(), delete(), lock(), refresh(), evict(), replicate() - there is a corresponding cascade style. Respectively, the cascade styles are named create, merge, save-update, delete, lock, refresh, evict, replicate. If you want an operation to be cascaded along an association, you must indicate that in the mapping document


I hope this helps you solve your problem

Regards,
Vincent.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 1:00 pm 
Beginner
Beginner

Joined: Thu Jul 14, 2005 10:11 am
Posts: 32
thanks for your reply!!!.

I tryed with cascade="all" and nothing. My entire mappings are:

Client mapping

Code:
<hibernate-mapping>

   <class name="clientes.Cliente" table="Clientes">   
         
         <id name="id" column="id" type="integer">
             <generator class="native"/>
          </id>
          
        <property name="Activo" column="Activo" not-null="true" type="boolean"/>                                                               
        <property name="Comentarios" column="Comentarios" not-null="false" length="400" type="string"/>               
      <property name="Direccion" column="Direccion" not-null="true" length="200" type="string"/>               
        <property name="EMail" column="EMail" not-null="false" length="50" type="string"/>               
        <property name="Fax" column="Fax" not-null="false" length="20" type="string"/>               
        <property name="Telefono" column="Telefono" not-null="true" length="20" type="string"/>                                       
         <property name="Login" column="Login" not-null="false" length="20" type="string" />                 
      <property name="Password" column="Password" not-null="false" length="20" type="string"/>       
      <property name="Nombre" column="Nombre" not-null="true" length="20" type="string"/>               
      <property name="Apellido" column="Apellido" not-null="false" length="20" type="string"/>                      
      <property name="TipoDocumento" column="TipoDocumento" not-null="true" type="integer"/>                      
         <property name="NumeroDocumento" column="NumeroDocumento" not-null="true" type="integer"/>            
      <property name="Celular" column="Celular" not-null="false" length="20" type="string"/>      
        <property name="CUILoCUIT" column="CUILoCUIT" not-null="true" type="boolean"/>                                                                            
      <property name="NumeroCUILoCUIT" column="NumeroCUILoCUIT" length="50" not-null="true" type="string"/>                             
        <property name="AccesoWeb" column="AccesoWeb" not-null="true" type="boolean"/>                                                                            

         <property name="idEmpresa" column="idEmpresa" not-null="true" type="integer"/> 
       
      <many-to-one name="Empresa" column="idEmpresa" class="clientes.Empresa" update="false" insert="false" />

   </class>

</hibernate-mapping>



Enterprise mapping:

Code:
<hibernate-mapping>

    <class name="clientes.Empresa" table="Empresas">

         <id name="id" column="id" type="integer">
             <generator class="native"/>
          </id>
          
        <property name="Activo" column="Activo" not-null="true" type="boolean"/>                                                               
        <property name="Comentarios" column="Comentarios" not-null="false" length="400" type="string"/>               
      <property name="Direccion" column="Direccion" not-null="true" length="200" type="string"/>               
        <property name="EMail" column="EMail" not-null="false" length="50" type="string"/>               
        <property name="Fax" column="Fax" not-null="false" length="20" type="string"/>               
        <property name="Telefono" column="Telefono" not-null="true" length="20" type="string"/>                                       
         
      <!-- Datos de la Empresa -->

      <property name="Denominacion" column="Denominacion" not-null="true" length="100" type="string"/>       
      <property name="RazonSocial" column="RazonSocial" not-null="true" length="100" type="string"/>       
      <property name="SitioWeb" column="SitioWeb" not-null="false" length="50" type="string"/>               
      <property name="CUIT" column="CUIT" not-null="true" type="string"/>                             
         
      <bag name="Empleados" inverse="false">
         <key column="idEmpresa" update="false" />
         <one-to-many class="clientes.Cliente" />
      </bag> 
               
    </class>

</hibernate-mapping>






Thanks a lot, really...!!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 4:39 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
I was looking at your mapping. Why do you keep a property on the empresaId and a relationship to the Empresa? This seems redundant to me.

Code:
<property name="idEmpresa" column="idEmpresa" not-null="true" type="integer"/>
       
      <many-to-one name="Empresa" column="idEmpresa" class="clientes.Empresa" update="false" insert="false" />


Also, could you post your java code. I am curious to see how refresh your entity and also what makes you think that the object does not change...

If you mean that the hashcode of the object does not change, that is expected and correct. If you mean that you change an attribute and it does not show, then, it's strange.

Post the code and I'll take a look at it.

Vincent.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 4:52 pm 
Beginner
Beginner

Joined: Thu Jul 14, 2005 10:11 am
Posts: 32
Vincent,

I've got two properties because it's a web app. Basically, i can't keep the track of the 'related object' in an html form... i can keep only the ID.

When i modify something, i get setted the ID via web. I can't put a java-object in an html combo box.

The java code is really simple. This is from my class Cliente (Client):

Code:
   public String update(){      
   
      HibernateUtil.update(this);   
      new Log((Map)ActionContext.getContext().get("session"),"Modificacion de Cliente [" + nombre +"]");            
      addActionMessage("Modificacion exitosa");
      System.out.println("Empresa: " + e);
      return SUCCESS;
   }   


And this is from HibernateUtil:

Code:
   public static void update(Object o){
      HibernateUtil.beginTransaction();
      Session session = HibernateUtil.getSession();                           

      session.merge(o);
      session.flush();
      session.refresh(o);   
   }


If i load an object, everything works fine. The proxy gets instantiated and i can read the 'related object'.

But... when i update an object... because of the architecture of my framework (webwork), a whole new object gets instantiated, with the attributes sent by the client's browser. So, i get only 'idEmpresa'. I needed to 'refresh' the association because the hibernate-proxy gets lost on the way.


Thanks again Vincent..!!!

George


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 5:04 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Ok. I understand better now what you are doing.
Have you tried initalizing the Empresa object using the Hibernate object?:

Code:

public String update(){         
      HibernateUtil.update(this);
      Hibernate.initialize(this.getEmpresa());
      return SUCCESS;
   }


What happens ?

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 12:27 pm 
Beginner
Beginner

Joined: Thu Jul 14, 2005 10:11 am
Posts: 32
I've just tried the 'Hibernate.initialize' method. No results... :(

It's just that the pointer to Empresa is null when the update() method is called.

I've got a simple idea... i guess i'll have to load manually the related-objects with a hql select.

I'm REALLY thankful. Really !!!


George,
Buenos Aires, Argentina


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