-->
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: Howto update a child in a Parent/Child one-to-many relation?
PostPosted: Thu Mar 24, 2011 1:31 pm 
Newbie

Joined: Wed Mar 02, 2011 9:50 am
Posts: 15
Hi all,

I feel the documentation is not very clear on one point, and cannot find anything on internet : i have a parent/child relation between to types of objects with a cascade="all-delete-orphan" (both parent and children are already correctly inserted in database) and i just want to update one child of a parent. Here is my code:

Code:
      PlateType plate = (PlateType)updateSession.load(PlateType.class, wellToUpdate.getIndex());
      plate.getWells().add(wellToUpdate);
      updateSession.update(plate);


I really don't if this is the right way to do it, but it doesn't seem to, because an exception is raised on commit:
Code:
18:27:56,040 ERROR Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushin
g: com.andromas.bgp.web.core.model.platetype.PlateType
        at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:244)
        at org.hibernate.type.EntityType.getIdentifier(EntityType.java:449)
        at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:141)
        at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:312)
        at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2179)
        at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2542)
        at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2478)
        at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2805)
        at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:114)
        at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:180)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)


Could you help please ?

thanks


Top
 Profile  
 
 Post subject: Re: Howto update a child in a Parent/Child one-to-many relation?
PostPosted: Fri Mar 25, 2011 1:19 pm 
Newbie

Joined: Tue Mar 22, 2011 4:28 pm
Posts: 9
Can you post the mapping file of the PlateType and also the main code of what you are trying to do?

Thanks,

Prasad


Top
 Profile  
 
 Post subject: Re: Howto update a child in a Parent/Child one-to-many relation?
PostPosted: Tue Mar 29, 2011 3:16 am 
Newbie

Joined: Wed Mar 02, 2011 9:50 am
Posts: 15
Thanks for you answer, here is the mapping file for PlateType:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.andromas.bgp.web.core.model.platetype">

   <class name="PlateType" table="PLATES">
      <id name="id" type="long" column="ID">
         <generator class="native" />
      </id>
      <property name="creationDate" type="calendar" column="CREATION_TIME"
         not-null="true"></property>
      <property name="modificationDate" type="calendar" column="MODIFICATION_TIME"></property>
      <property name="barcode" type="string" column="BARCODE"></property>
      <property name="creator" type="string" column="CREATOR"
         not-null="true"></property>
      <property name="noBarcode" type="boolean" column="NOBARCODE"></property>

      
      <list name="wells" lazy="false" inverse="true" cascade="all-delete-orphan">
         <key column="PARENT_PLATE" not-null="true"/>
         <list-index column="index"/>         
         <one-to-many class="com.andromas.bgp.web.core.model.platetype.WellInfo"/>
      </list>
       
   </class>

</hibernate-mapping>


And this is for the Well :
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.andromas.bgp.web.core.model.platetype">

   <class name="WellInfo" table="WELLS">
      
      <composite-id name="id"
         class="com.andromas.bgp.web.core.model.platetype.WellInfoIdType">
         <key-many-to-one name="parentPlate" column="PARENT_PLATE" class="com.andromas.bgp.web.core.model.platetype.PlateType"></key-many-to-one>
         <key-property name="name" column="NAME" type="string" />
      </composite-id>

      <property name="index" type="long" column="INDEX" not-null="true"></property>      
      <property name="wellOrder" type="integer" column="WELL_ORDER" not-null="true"></property>
      <property name="barcode" type="string" column="BARCODE"></property>
      <property name="method" type="string" column="METHOD" not-null="true"></property>
      <property name="needAcquisition" type="boolean" column="NEED_ACQUISITION"></property>
      <property name="identificationResult" type="string" column="RESULT"></property>
      
   </class>
</hibernate-mapping>





And what i want to do is to update a value of a well inside the DB, with the following code:
Code:
   private void updateWellRecord(WellInfo wellToUpdate) {

      logger.debug("well To Insert is acquisition() " + wellToUpdate.isNeedAcquisition());
      logger.debug("Is is : " + wellToUpdate.getId());
      logger.debug("wellToUpdate.getId() method " + wellToUpdate.getMethod());
      logger.debug("wellToUpdate.getId() barcode " + wellToUpdate.getBarcode());

      if (wellToUpdate.getId() == null ||
            wellToUpdate.getId().getName() == null ||
            wellToUpdate.getBarcode() == null ||
            wellToUpdate.getMethod() == null) {
         logger.warn("Well information not complete, cannot insert object in database at the moment");
         return;
      }
      SessionFactory sessionFactory = (SessionFactory) HibernateUtil.getSessionFactory();
      Session updateSession = sessionFactory.openSession();
      updateSession.beginTransaction();
      
      PlateType plate = (PlateType)updateSession.load(PlateType.class, wellToUpdate.getIndex());
      plate.getWells().add(wellToUpdate);
      updateSession.update(plate);
      
      updateSession.getTransaction().commit();
      updateSession.close();
      

   }


Top
 Profile  
 
 Post subject: Re: Howto update a child in a Parent/Child one-to-many relation?
PostPosted: Tue Mar 29, 2011 8:24 am 
Newbie

Joined: Wed Mar 02, 2011 9:50 am
Posts: 15
Anyone know how to update a child already inserted into the database with a parent/child one to many relationship ?
I suppose this must be quite easy normally ?

Thanks a lot ! :)


Top
 Profile  
 
 Post subject: Re: Howto update a child in a Parent/Child one-to-many relation?
PostPosted: Tue Mar 29, 2011 9:24 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
You specified
Code:
inverse="true"

in your OneToMany mapping which means that in your case the children are handling the relation and not the parent!

So you should modify and update the WellInfo directly, not the PlateType!


Top
 Profile  
 
 Post subject: Re: Howto update a child in a Parent/Child one-to-many relation?
PostPosted: Tue Mar 29, 2011 10:12 am 
Newbie

Joined: Wed Mar 02, 2011 9:50 am
Posts: 15
thanks, but actually i've just tried it, like this:
Code:
SessionFactory sessionFactory = (SessionFactory) HibernateUtil.getSessionFactory();
      Session updateSession = sessionFactory.openSession();
      updateSession.beginTransaction();
      
      WellInfo databaseWell = (WellInfo)updateSession.get (WellInfo.class, wellToUpdate.getId());
      databaseWell.setBarcode(wellToUpdate.getBarcode());
      databaseWell.setMethod(wellToUpdate.getMethod());   
      
      updateSession.update(databaseWell);
      
      updateSession.getTransaction().commit();
      updateSession.close();


and when updating, i get the following error:
Code:
16:10:56,415  INFO Error performing load command
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushin
g: com.andromas.bgp.web.core.model.platetype.PlateType
        at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:244)
        at org.hibernate.type.EntityType.getIdentifier(EntityType.java:449)
        at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:141)
        at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:312)
        at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1769)
        at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1740)
        at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1612)
        at org.hibernate.loader.Loader.doQuery(Loader.java:717)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
        at org.hibernate.loader.Loader.loadEntity(Loader.java:1933)
        at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86)
        at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:76)
        at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3270)
        at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:496)
        at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:477)
        at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227)
        at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285)
        at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
        at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
        at org.hibernate.impl.SessionImpl.get(SessionImpl.java:997)
        at org.hibernate.impl.SessionImpl.get(SessionImpl.java:990)
        at com.andromas.bgp.servlet.RESTHandler.updateWellRecord(RESTHandler.java:407)


Which transcient instance it is talking about ?

thank you


Top
 Profile  
 
 Post subject: Re: Howto update a child in a Parent/Child one-to-many relation?
PostPosted: Tue Mar 29, 2011 11:11 am 
Newbie

Joined: Wed Mar 02, 2011 9:50 am
Posts: 15
Ok i solved my problem by retrievent parent object from database and then setting as parent of my child and saving it priori to my child (don't why i have to get and save the parent, as it is already in the database).

Code:
SessionFactory sessionFactory = (SessionFactory) HibernateUtil.getSessionFactory();
      Session updateSession = sessionFactory.openSession();
      updateSession.beginTransaction();
            
      PlateType plate = (PlateType)updateSession.load(PlateType.class, wellToUpdate.getIndex());
      
      wellToUpdate.getId().setParentPlate(plate);
      updateSession.update(wellToUpdate.getId().getParentPlate());
      updateSession.update(wellToUpdate);
      updateSession.getTransaction().commit();
      updateSession.close();



thanks anyway !


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.