-->
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.  [ 3 posts ] 
Author Message
 Post subject: Java Map on a base class fails -foreign key constraint fails
PostPosted: Mon Sep 12, 2005 8:52 am 
Newbie

Joined: Wed Sep 07, 2005 8:22 am
Posts: 14
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


Top
 Profile  
 
 Post subject: Update...
PostPosted: Tue Sep 13, 2005 3:37 am 
Newbie

Joined: Wed Sep 07, 2005 8:22 am
Posts: 14
This problem is due to the Attributes table having two foriegn keys.

One for SubClassA & one for SubClassB.

Also I had placed a Long identify field on the two subclasses.
According to section "5.2. Implementing inheritance", the identifier property is inherited from the superclass.

(The design has now changed & does not require a attribtes map, I will figure this out though & post a complete answer when I have one)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 9:17 am 
Newbie

Joined: Wed Sep 07, 2005 8:22 am
Posts: 14
I was using ...


@hibernate.class table="subClassA"

on the sub classes, when I should of been using...

@hibernate.joined-subclass table="subClassA"


ah, the simple things.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.