-->
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: delete all objects and rows from a collection
PostPosted: Wed Apr 15, 2009 2:40 pm 
Newbie

Joined: Fri Oct 31, 2008 11:35 am
Posts: 12
I can´t erase the rows from a child table, always i must to have at least one object in my collection if I remove all objects from my collection don´t delete anything.



Code:
<hibernate-mapping>
  <class dynamic-insert="false" dynamic-update="false" mutable="true"
         name="Comercio.Pedimento.Pedimento" optimistic-lock="version"
         polymorphism="implicit" select-before-update="false" table="PEDIMENTO">
    <id column="ID_PEDIMENTO" name="idPedimento" unsaved-value="0">
      <generator class="assigned"/>
    </id>
    <set cascade="delete,all-delete-orphan" inverse="true" lazy="false" name="fecha" table="DETALLE_FECHA">
      <key column="ID_PEDIMENTO" not-null="true"/>
      <one-to-many class="Comercio.Pedimento.DetalleFecha"/>
    </set>
  </class>
</hibernate-mapping>



<hibernate-mapping>
  <class dynamic-insert="false" dynamic-update="false" mutable="true"
  name="Comercio.Pedimento.DetalleFecha" optimistic-lock="version"
  polymorphism="implicit" select-before-update="false" table="DETALLE_FECHA">
    <id column="ID_DETALLE_FECHA" name="idDetalleFecha" >
          <generator  class="increment"/>
    </id>
   
    <many-to-one column="ID_PEDIMENTO" name="pedimento"  not-null="true"/>

    <property column="FECHA" name="fecha" type="date"/>   
    <many-to-one cascade="none" class="Comercio.Pedimento.TipoFecha" column="ID_TIPO_FECHA" insert="true" lazy="false" name="tipoFecha" update="true"/>   
  </class>
</hibernate-mapping>



this is my code for remove objects

Code:

public void removerFechas(){
        if(currentDetalleFecha != null){
            try{
               
                    Iterator itFecha =
getCurrentPedimento().getFecha().iterator();
                    DetalleFecha marcado = null;
                    while(itFecha.hasNext()){
                       
                        DetalleFecha aux = (DetalleFecha)itFecha.next();
                        if(aux.isMarcado()){
                            currentDetalleFecha = null;
                            marcado = aux;
                            break;
                        }
                    }
                    getCurrentPedimento().getFecha().remove(marcado);
                    marcado = null;

                if(getCurrentPedimento().getFecha().size() == 0)
                        getCurrentPedimento().setFecha(null);
            }catch(java.util.NoSuchElementException ex){
                ex.printStackTrace();
            }
        }
    }


Top
 Profile  
 
 Post subject: use Iterator.remove()
PostPosted: Thu Apr 16, 2009 12:55 am 
Newbie

Joined: Sat Mar 28, 2009 5:47 pm
Posts: 14
to remove an object from a collection while using an Iterator,
invoke iterator.remove();
It is incorrect to remove directly from the collection,
as the iterator will get confused, and not iterate correctly.


http://java.sun.com/javase/6/docs/api/j ... ml#remove()


Top
 Profile  
 
 Post subject: I can´t delete all tables rows
PostPosted: Thu Apr 16, 2009 9:50 am 
Newbie

Joined: Fri Oct 31, 2008 11:35 am
Posts: 12
I try use iterators remove but the problem persist

Code:
public void removerFechas(){
        if(currentDetalleFecha != null){
            try{
                    Iterator itFecha =
                     getCurrentPedimento().getFecha().iterator();
                    DetalleFecha marcado = null;
                    while(itFecha.hasNext()){
                       
                        DetalleFecha aux = (DetalleFecha)itFecha.next();
                        if(aux.isMarcado()){
                            currentDetalleFecha = null;
                            marcado = aux;
                            itFecha.remove();
                            //break;
                        }
                    }
                    //getCurrentPedimento().getFecha().remove(marcado);
                    marcado = null;
                    if(getCurrentPedimento().getFecha().size() == 0)
                        getCurrentPedimento().setFecha(null);
            }catch(java.util.NoSuchElementException ex){
                ex.printStackTrace();
            }
        }
    }



Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 2:32 pm 
Newbie

Joined: Sat Mar 28, 2009 5:47 pm
Posts: 14
This is a bit dubious:
Code:
getCurrentPedimento().setFecha(null);


I would expect that Hibernate expects Fecha to be a Collection,
(even an empty collection); setting it to null may cause unusual behavior.
Check the log to see if there is some NullPointerException that stops your update.

And do you session.update(getCurrentPedimento()) at some point?

If this is still a problem, you may need to explain more that the failure is,
or show more of the code/context...


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.