-->
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: Unable to delete a map of entities
PostPosted: Wed May 26, 2010 12:10 pm 
Newbie

Joined: Wed May 26, 2010 11:49 am
Posts: 2
Hi!

I've found that it is unable to delete a map of entities.
This code is just an example. My goal - is to get a clear database before tests run so I'am sorting the table mappings in the dependency order and clearing it one-by-one. I've found that having following class-mappings structure doesn't allow my to clear the db.
Or there are some other ways to clear db?

The error I got is: "ERROR JDBCExceptionReporter:101 - The DELETE statement conflicted with the REFERENCE constraint "FKC5E135EA36BECBD3". The conflict occurred in database "Tests", table "dbo.Foo_bars", column 'id'."

Does anybody know how to solve this issue? I'd like to delete a number of instances of Foo.

This error occurs then I'am trying to delete an instance of Foo with elements in the bars map.
Code:
      Map<String, String> bars = new HashMap<String, String>();
      bars.put("key1", "value1");
      
      Foo foo = new Animal();
      foo.setWeighings(bars);
      
      session.beginTransaction();
      session.save(foo);
      session.getTransaction().commit();
      
      session.beginTransaction();
      session.createQuery("delete from Foo").executeUpdate(); // Here comes exception
      session.getTransaction().commit();


Here is a piece of code:

The mapping:
Code:
<hibernate-mapping>
   <class name="com.tests.Foo" table="Foo">
      <id name="id" type="java.lang.String" >
         <column name="id"  length="36"/>
         <generator class="assigned" />
      </id>
      <map name="bars" cascade="all-delete-orphan">
         <key column="id" />
         <index column="key" type="string" />
         <element column="value" type="string"/>
      </map>
   </class>
</hibernate-mapping>


The Foo class:
Code:
public class Foo {
   string id;
   Map<String, String> bars;

   public Foo(String id, Map<String, String> bars) {
      this.id = id;
      this.bars = bars;
   }

   public String getId() {
      return id;
   }

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

   public Map<String, String> getBars() {
      return bars;
   }

   public void setBars(Map<String, String> bars) {
      this.bars = bars;
   }
}


Thanks in advice,
Dmitry.


Top
 Profile  
 
 Post subject: Re: Unable to delete a map of entities
PostPosted: Wed May 26, 2010 12:54 pm 
Newbie

Joined: Mon Feb 02, 2009 5:31 pm
Posts: 8
session.createQuery("delete from Foo").executeUpdate(); // Here comes exception


is Foo the name of your table or it is Foo_bar?


Top
 Profile  
 
 Post subject: Re: Unable to delete a map of entities
PostPosted: Thu May 27, 2010 8:16 am 
Newbie

Joined: Wed May 26, 2010 11:49 am
Posts: 2
Database contais two tables - the one is Foo, and the second one - Foo_bars.
First one is for Foo's fields, and the second one - for bars map.


Top
 Profile  
 
 Post subject: Re: Unable to delete a map of entities
PostPosted: Thu May 27, 2010 8:44 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
As far as I know cascading is not supported by Hibernate when using HQL DELETE statements. There is an old JIRA issue about this: http://opensource.atlassian.com/project ... se/HHH-695

A database-level ON DELETE CASCADE may be your best option unless you want to use SQL/JDBC or delete each Foo one-by-one with Session.delete().


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.