-->
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 doesn´t work
PostPosted: Thu Jul 20, 2006 5:11 am 
Newbie

Joined: Wed May 04, 2005 9:34 am
Posts: 16
Location: Germany
Hi,

i have a one-to-many relationship between the two tables IbnKommune and IbnKennzahlen2004. My problem is that i can´t delete a record of Kennzahlen2004. The mapping-files for the object IbnKommune is:

Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
   
   <hibernate-mapping>
     <class
       name="de.gebit.ibn.data.IbnKommune"
       lazy="false"
       table="kommunen">
         <id
             name="id"
          type="long"
          column="k_nr">
            <generator class="increment"/>
         </id>
         <property
          name="bezeichnung"
          column="k_bez"
          type="string"/>
       <property
         name="art"
          column="k_art"
          type="int"/>
         <property
          name="kurz"
          column="k_kurz"
          type="string"/>

      <set name="kennzahlen2004" inverse="true" lazy="false" cascade="delete" >
         <key column="k_kommune"/>
         <one-to-many class="de.gebit.ibn.data.IbnKennzahlen2004"/>
      </set>
     </class>
</hibernate-mapping>


and for the second object:

Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
   
   <hibernate-mapping>
     <class
       name="de.gebit.ibn.data.IbnKennzahlen2004"
       lazy="false"
       table="kennzahlen2004">
         <id
             name="id"
          type="long"
          column="k_nr">
            <generator class="increment"/>
         </id>
       <property
         name="soz1"
          column="k_soz1"
          type="double"/>
       <property
         name="soz1_1"
          column="k_soz1_1"
          type="double"/>

                ...

      <many-to-one
         name="kommune"
         column="k_kommune"
         class="de.gebit.ibn.data.IbnKommune"
         not-null="false"/>
     </class>
</hibernate-mapping>


Here are the important snippets from the classes:
First IbnKommune:

Code:
public class IbnKommune implements Serializable {
   private static final long serialVersionUID = 1L;
   private Long id;
   private String bezeichnung;
   private String kurz;
   private int art;
   private Set kennzahlen2004 = new HashSet();

   public IbnKommune() {}
      
   public Long getId() {
      return id;
   }


   public Set getKennzahlen2004() {
      return kennzahlen2004;
   }

   public void setKennzahlen2004(Set kennzahlen2004) {
      this.kennzahlen2004=kennzahlen2004;
   }


   public void addKennzahlen2004 (IbnKennzahlen2004 newKennzahlen)
      throws IbnHibernateException {
      if (newKennzahlen==null)
         throw new IbnHibernateException("Ungültiges Argument beim " +
               "Versuch einen Datensatz mit Kennzahlen aus dem Jahr 2005 bei einer " +
               "Kommune einzutragen.");
      if (newKennzahlen.getKommune() != null)
         newKennzahlen.getKommune().getDaten().remove(newKennzahlen);
      newKennzahlen.setKommune(this);
      daten.add(newKennzahlen);
   }
}


and the class IbnKennzahlen2004

Code:
public class IbnKennzahlen2004 implements Serializable{
   private static final long serialVersionUID = 1L;
   private Long id;
   private IbnKommune kommune;
        ...

   public IbnKennzahlen2004() {};
   
   public IbnKennzahlen2004(IbnKommune kom) {
      this.kommune=kom;
   };

   public Long getId() {
      return id;
   }

   public IbnKommune getKommune() {
      return kommune;
   }

   public void setId(Long id) {
      this.id=id;
   }

   public void setKommune(IbnKommune kommune) {
      this.kommune=kommune;
   }

     ...
}


And here is how i try to delete several records of IbnKennzahlen2004:

Code:
      Session s = HibernateUtil.getSession();
      
// retrieves a collection of IbnKennzahlen2004-objects
      List kenn=GetKennzahlengruppe(jahr);
      
      try {
         for (int i=0;i<=kenn.size()-1;i++){
            HibernateUtil.beginTransaction();

            IbnKennzahlen2004 kenn2004=(IbnKennzahlen2004)kenn.get(i);
            if (kenn2004.getKommune() != null)
                  kenn2004.getKommune().getKennzahlen2004().remove(kenn2004);
            kenn2004.setKommune(null);
            s.delete(kenn2004);
            
            HibernateUtil.commitTransaction();
         }
      } catch (HibernateException ex) {
         HibernateUtil.rollbackTransaction();
         throw new IbnHibernateException(ex, "Fehler beim Löschen aller " +
               "Kennzahlen aus der Datenbank aufgetreten.");
      }finally{
         HibernateUtil.closeSession();
      }
   }


I don´t get an error, but the record is not deleted.

You can make a desperate programmer very happy, if you can give me a hint on what´s wrong here. Thank you very much.

Christoph

Hibernate version: 3.1
Name and version of the database: MySQL5[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 11:28 am 
Newbie

Joined: Thu Jul 27, 2006 10:02 am
Posts: 17
Did you find a solution? I Have exactly the same problem.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 24, 2006 7:38 pm 
Newbie

Joined: Sun Sep 24, 2006 7:33 pm
Posts: 2
I have this problem too. It occurs on a many-to-one mapping via a join-table. I just can't get a record deleted.

Does someone already have a solution???


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 4:45 am 
Newbie

Joined: Thu Jul 27, 2006 10:02 am
Posts: 17
Hi, I found the solution to my problem.

In my situation I was using HashSet and it was causing problems. This is how HashSet works in java.

If this is your object when you put it to the HashSet

Code:
String id = null;
String name = "hello"


and this is your hashCode() method

Code:
public int hashCode() {
  int result = 17;
  if (id!= null)
    result = 37 * result + id.hashCode();
  if (name != null)
    result = 37 * result + name.hashCode();      
  return result;
}


If you setup hibernate to generate id's automaticly for you, this is the same object after you persist it

Code:
String id = "1"; //or something like it
String name = "hello"


As you see the hashCode() value will be different now.

So if you're trying to delete this new object (with id) from the original HashSet you will not be able to do it.

If you look at HashSet implementation it creates the key on insert and even if you manipulate object that is in the HashSet later, the key will still be the one generated on insert.

In my situation I had to load the object from hibernate again with a new instance of HashSet (the one with object containting id's) and then it worked.

Hope it helps.


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.