-->
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: I can't update ids in a table which are foreign keys
PostPosted: Thu Feb 18, 2010 6:25 am 
Newbie

Joined: Thu Feb 18, 2010 6:11 am
Posts: 5
Hi, I'm working now in a struts2 + spring 2.5 + hibernate 3 application and I'm having some problems updating fields of a table related with others. I have created a form with a combobox where the user can select the value of the children and it's working for the creation but not for the update. There it only works the update of the own parent's fields
Here is my configuration
Code:
<class name="com.tecnoy.itacsa.modelo.Orders" table="orders" catalog="itacsa" >
        <id name="intIdOrder" type="int">
            <column name="int_idOrder" />
            <generator class="identity" />
        </id>
        <many-to-one name="fleetCarrier" class="com.tecnoy.itacsa.modelo.FleetCarrier"  fetch="join" update="false" insert="false" >
            <column name="int_idCarrier" />
        </many-to-one>
        <many-to-one name="zoneLocations" class="com.tecnoy.itacsa.modelo.ZoneLocations" fetch="join" update="false" insert="false">
            <column name="int_idLocation" />
        </many-to-one>
        <many-to-one name="users" class="com.tecnoy.itacsa.modelo.Users" fetch="join" update="false" insert="false">
            <column name="int_idUser" />
        </many-to-one>
        <many-to-one name="ordersStatus" class="com.tecnoy.itacsa.modelo.OrdersStatus" fetch="join" update="false" insert="false">
            <column name="int_idOrdersStatus" />
        </many-to-one>
        <many-to-one name="fleetTruck" class="com.tecnoy.itacsa.modelo.FleetTruck" fetch="join" update="false" insert="false">
            <column name="int_idTruck" />
        </many-to-one>
        <many-to-one name="fleetDrivers" class="com.tecnoy.itacsa.modelo.FleetDrivers" fetch="join" update="false" insert="false">
            <column name="int_idDriver" />
        </many-to-one>
        <many-to-one name="merchanType" class="com.tecnoy.itacsa.modelo.MerchanType" fetch="join" update="false" insert="false" >
            <column name="int_idMercType" />
        </many-to-one>
        <property name="varOrderTrans" type="string">
            <column name="var_orderTrans" not-null="true" />
        </property>
        <property name="varDestiny" type="string">
            <column name="var_destiny" length="400" />
        </property>
        <property name="varNoteDelivery" type="string">
            <column name="var_noteDelivery" />
        </property>
        <property name="datDatePredictedDel" type="date">
            <column name="dat_datePredictedDel" length="10" />
        </property>
        <property name="datDateRec" type="date">
            <column name="dat_dateRec" length="10" />
        </property>
        <property name="datLastModified" type="date">
            <column name="dat_lastModified" length="10" />
        </property>
        <property name="datAlbaran" type="date">
            <column name="dat_albaran" length="10" />
        </property>
        <property name="varMercType" type="string">
            <column name="var_mercType" />
        </property>
        <set name="historyStatuses" inverse="true">
            <key>
                <column name="int_idOrder" not-null="true" />
            </key>
            <one-to-many class="com.tecnoy.itacsa.modelo.HistoryStatus" />
        </set>
    </class>


And here is the configuration of one of the children
Code:
<hibernate-mapping>
    <class name="com.tecnoy.itacsa.modelo.MerchanType" table="merchan_type" catalog="itacsa" lazy="false">
        <id name="intIdMercType" type="java.lang.Integer">
            <column name="int_idMercType" />
            <generator class="identity" />
        </id>
        <property name="varMercTypeDescription" type="string">
            <column name="var_MercTypeDescription" length="100" not-null="true" />
        </property>
        <property name="booStatus" type="java.lang.Boolean">
            <column name="boo_status" />
        </property>
        <set name="orderses" inverse="true" >
            <key>
                <column name="int_idMercType" not-null="true" />
            </key>
            <one-to-many class="com.tecnoy.itacsa.modelo.Orders" />
        </set>
    </class>
</hibernate-mapping>


I'm also using the OpenSessionInViewFilter but I don't know if it's working
Code:
<filter>
      <filter-name>hibernateFilter</filter-name>
      <filter-class>
         org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
      </filter-class>
   </filter>
   <filter-mapping>
      <filter-name>hibernateFilter</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

Sorry if I'm missing information but I don't know exactly what else you need of my configuration
Does anyone knows what I'm missing?
Thanks


Top
 Profile  
 
 Post subject: Re: I can't update ids in a table which are foreign keys
PostPosted: Thu Feb 18, 2010 7:28 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
I can't update ids in a table which are foreign keys


I immagine you run into ConstraintsViolation exceptions ?
Is it that what happens when you are trying to update the childrens' id's?


Top
 Profile  
 
 Post subject: Re: I can't update ids in a table which are foreign keys
PostPosted: Thu Feb 18, 2010 9:07 am 
Newbie

Joined: Thu Feb 18, 2010 6:11 am
Posts: 5
No, I'm not getting any exception or at least not one the server reply with.
Hibernate leave this log
Code:
Hibernate: update itacsa.orders set var_orderTrans=?, var_destiny=?, var_noteDelivery=?, dat_datePredictedDel=?, dat_dateRec=?, dat_lastModified=?, dat_albaran=?, var_mercType=? where int_idOrder=?

In the update there isn't any field from a different table and I've used the debug to watch the information that it's given to HibernateTemplate.merge (or update, I've used both) and all the properties that I want to update are filled but they aren't updated


Top
 Profile  
 
 Post subject: Re: I can't update ids in a table which are foreign keys
PostPosted: Thu Feb 18, 2010 9:29 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Considering that you have mapped all your <many-to-one>:s with update="false" insert="false" I find it surprising that it works when inserting.


Top
 Profile  
 
 Post subject: Re: I can't update ids in a table which are foreign keys
PostPosted: Thu Feb 18, 2010 9:39 am 
Newbie

Joined: Thu Feb 18, 2010 6:11 am
Posts: 5
First I have to say that I described badly the problem because what I said was the parent in fact was the child. In my application there is a table call orders that stores the id of entries of other tables to get their information (for example the id of a type of merchandise to get its description)
I've changed the attribute update="true" in my *.hbm file and now I'm getting the constraint exception
Code:
18-02-10 14:14:53,288 ERROR [http-8084-1][JDBCExceptionReporter(78)]: Cannot add or update a child row: a foreign key constraint fails (`itacsa`.`orders`, CONSTRAINT `fk_idCarrier` FOREIGN KEY (`int_idCarrier`) REFERENCES `fleet_carrier` (`int_idCarrier`) ON DELETE NO ACTION ON UPDATE NO ACTION)
18-02-10 14:14:53,295 ERROR [http-8084-1][AbstractFlushingEventListener(301)]: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

That hapens if I leave a foreign key field without information because in my application that means a 0 value for the id. As there isn't an entry in the other table with 0 id, I have this exception (I think). I tried initializing the field with null or with a new object of the parent class but then I have this exception
Code:
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.tecnoy.itacsa.modelo.MerchanType; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.tecnoy.itacsa.modelo.MerchanType

This doesn't happen in the insertion but I guess it's because I have in the *.hbm file the attribute insert="false" but for the insertions it works fine like this and for the update it doesn't work


Top
 Profile  
 
 Post subject: Re: I can't update ids in a table which are foreign keys
PostPosted: Thu Feb 18, 2010 9:59 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
There are a lot of things I don't understand. When you create a new Orders object, how do, for example, a value get into the int_idCarrier column? With inserst="false" Hibernate should not include this column in the insert.

Quote:
That hapens if I leave a foreign key field without information because in my application that means a 0 value for the id.


How do you get a 0 in there? A "field without information", is that some kind of special object with id=0? I strongly suggest that you use null and not 0 to indicate a "field without information".


Top
 Profile  
 
 Post subject: Re: I can't update ids in a table which are foreign keys
PostPosted: Thu Feb 18, 2010 10:14 am 
Newbie

Joined: Thu Feb 18, 2010 6:11 am
Posts: 5
I use this to put the information inside the object MerchanType that is in the Order object
Code:
<s:form action="ordersFilter" theme="simple" cssClass="yform">
   <s:hidden name="order.users.intIdUser" id="order.users.intIdUser"
      value="" />
   <table>
      ...
         <td><s:label>Tipo de mercancĂ­a: </s:label></td>
         <td><s:select id="order.merchanType.intIdMercType"
            name="order.merchanType.intIdMercType"
            headerValue="--- Seleccione Tipo Mercancia ---" headerKey="0"
            list="listadoTipoMercancias" listKey="intIdMercType"
            listValue="varMercTypeDescription" /></td>
      ...
   </table>
</s:form>

When the user don't select anything, the merchan object gets initialize with a 0 value.
Then I do this
Code:
public class Orders  implements java.io.Serializable {
   private static final long serialVersionUID = 1L;
   private Integer intIdOrder;
     private MerchanType merchanType;
     private Date datDateRec;
     private Date datLastModified;
     private Date datAlbaran;
     private String varMercType;
...

public void update(Orders order) {
      Orders persistent = orderDAO.loadById(order.getIntIdOrder());
      if (persistent != null) {
         try {
            BeanUtils.copyProperties(persistent, order);
            orderDAO.persist(persistent);
         } catch (IllegalAccessException e) {
            e.printStackTrace();
         } catch (InvocationTargetException e) {
            e.printStackTrace();
         }
      } else {
         orderDAO.persist(order);
      }

With this and the configuration I have, I only get updated the information of the order object (for example datAlbaran) that isn't in another object (for example the merchanType object where the id of the type of merchan is stored)
When there is no merchanType selected I've tried initializing it to a new MerchanType or to null to get rid of the 0 value that I get from the form but then I get the error I mention before
Why does the insert work with insert="false"? I don't really know. I tried that configuration in order to solve this problem with the not selected information and it worked but with the update, if I put the update="false" I get updated the information of the order object but not the information stored in the other objects of the class Order


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.