Problem: Java Map on a base class fails with ...
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails
Moving the map object (inc methods) to concreate class and it works. Additions to Map get saved in the database.
I dont understand why it doesnt work when its on a base class, anyone?
Hibernate version: 3
Mapping documents:
Mapping generated is the same for when map is declared on either base class or the concreate class.
Mapping class...
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.ConcreateClass"
table="ConcreateClass" >
<id name="id" column="id" type="java.lang.Long" >
<generator class="native"> </generator>
</id>
<property
name="miscField"
type="java.lang.String"
update="true"
insert="true"
column="miscField"
length="50"
/>
<map name="attrs" table="dm_attributes" lazy="true" sort="unsorted" cascade="all" >
<key column="documentId" />
<index column="map_key" type="string" />
<element column="map_value" type="string" not-null="true" unique="false"/>
</map>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session sessionGET = HibernateUtil.currentSession();
Transaction txGET = sessionGET.beginTransaction();
request =(ConcreateClass) sessionGET.load(ConcreateClass.class, theID);
request.addAttr("key1", "value1");
request.setMiscField("update");
sessionGET.saveOrUpdate(request);
txGET.commit();
HibernateUtil.closeSession();
Full stack trace of any exception that occurs:Code:
(org.ConcreateClassTest)org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at org.ConcreateClassTest.testHibernate1(ConcreateClassTest.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.ConcreateClassTest.main(ConcreateClassTest.java:30)
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:642)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
Name and version of the database you are using: MySQL 4.1
Code:Code:
private Map attrs = new HashMap();
/**
* @hibernate.map lazy="true" table="dm_attributes" cascade="all"
* @hibernate.collection-index column="map_key" type="string"
* @hibernate.collection-key column="documentId" type="string"
* @hibernate.collection-element type="string" column="map_value" not-null="true"
*/
public Map getAttrs() {
return attrs;
}
public void setAttrs(Map attrs) {
this.attrs = attrs;
}
public void addAttr(String key, String value) {
if (attrs == null) {
attrs = new HashMap();
}
attrs.put(key, value);
}
public Object getAtt(String key) {
if (attrs == null) {
return null;
}
return attrs.get(key);
}
Thanks for your input.
Jeff