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.