cascade delete in hibernate

Getting this excpetion when i try to delete the entities
error HibernateException  in deleting the records Could not execute JDBC batch update
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
        at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
        at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:179)
        at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:72)
        at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:67)
        at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:148)
        at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:809)
        at org.hibernate.action.CollectionRemoveAction.execute(CollectionRemoveAction.java:22)
        at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:138)
        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:678)
        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:309)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
        at com.uprr.app.eco.DeleteOldData.deleteOldData(DeleteOldData.java:163)
        at com.uprr.app.eco.DeleteOldData.main(DeleteOldData.java:190)
Caused by: java.sql.BatchUpdateException: ORA-01407: cannot update ("ECO"."ECO_HZRD_EMGY_RESP"."STCC_CODE") to NULL

        at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:367)
        at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:8726)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1722)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:172)




my mapping files


 
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
        Auto-generated mapping file from
        the hibernate.org cfg2hbm engine
-->
    <class name="com.uprr.app.eco.persistenceDelete.EcoCmdyDelete" table="ECO_CMDY" schema="ECO">
        <id name="stccCode" type="string">
            <column name="STCC_CODE" length="7" />
            <generator class="assigned" />
        </id>      
        <property name="hzrdInd" type="string">
            <column name="HZRD_IND" length="1" />
        </property>
        <property name="effDate" type="date">
            <column name="EFF_DATE" length="7" />
        </property>
        <property name="cmdyAbrv" type="string">
            <column name="CMDY_ABRV" length="6" />
        </property>
        <property name="intlHmzdCode" type="string">
            <column name="INTL_HMZD_CODE" length="6" />
        </property>
        <property name="svcRqmtCode1" type="string">
            <column name="SVC_RQMT_CODE_1" length="1" />
        </property>
        <property name="svcRqmtCode2" type="string">
            <column name="SVC_RQMT_CODE_2" length="1" />
        </property>
        <property name="svcRqmtCode3" type="string">
            <column name="SVC_RQMT_CODE_3" length="1" />
        </property>
        <property name="wgtTolrFctr" type="integer">
            <column name="WGT_TOLR_FCTR" precision="1" scale="0" />
        </property>
        <property name="cmdyBaseNbr" type="short">
            <column name="CMDY_BASE_NBR" precision="3" scale="0" />
        </property>
        <property name="unnaNbr" type="string">
            <column name="UNNA_NBR" length="6" />
        </property>
        <property name="crtnDt" type="date">
            <column name="CRTN_DT" length="7" not-null="true" />
        </property>
        <property name="crtnUserId" type="string">
            <column name="CRTN_USER_ID" length="8" not-null="true" />
        </property>
        <property name="lastUptdDt" type="timestamp">
            <column name="LAST_UPTD_DT" length="7" not-null="true" />
        </property>
        <property name="lastUptdUserId" type="string">
            <column name="LAST_UPTD_USER_ID" length="8" not-null="true" />
        </property>
        <property name="cmdyDesc" type="string">
            <column name="CMDY_DESC" length="720" />
        </property>
       <!-- <property name="prodClassStcc" type="string">
         <column name="PROD_CLAS_STCC_CODE" length="7" />
      </property> -->
       
        <set name="ecoHzrdEmgyResps" cascade="delete-orphan" >
            <key>
                <column name="STCC_CODE" length="7" not-null="true" />
            </key>
            <one-to-many class="com.uprr.app.eco.persistenceDelete.EcoHzrdEmgyRespDelete" />
        </set>
       
        <set name="ecoCommodities" cascade="delete-orphan">
               <key>
                   <column name="PROD_CLAS_STCC_CODE" length="7"  />
               </key>
               <one-to-many class="com.uprr.app.eco.persistenceDelete.EcoCmdyDelete" />
        </set>
       
        <set name="ecoHzmtDtls" cascade="delete-orphan">
            <key>
                <column name="STCC_CODE" length="7" not-null="true" />
            </key>
            <one-to-many class="com.uprr.app.eco.persistenceDelete.EcoHzmtDtlDelete" />
        </set>
        <set name="ecoHzmtNameDescs" cascade="delete-orphan" >
            <key>
                <column name="STCC_CODE" length="7" not-null="true" />
            </key>
            <one-to-many class="com.uprr.app.eco.persistenceDelete.EcoHzmtNameDescDelete" />
        </set>
    </class>
</hibernate-mapping>




<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
        Auto-generated mapping file from
        the hibernate.org cfg2hbm engine
-->
    <class name="com.uprr.app.eco.persistenceDelete.EcoHzrdEmgyRespDelete" table="ECO_HZRD_EMGY_RESP" schema="ECO">
        <composite-id name="id" class="com.uprr.app.eco.persistenceDelete.EcoHzrdEmgyRespId">
            <key-property name="stccCode" type="string">
                <column name="STCC_CODE" length="7" />
            </key-property>
            <key-property name="lineNbr" type="int">
                <column name="LINE_NBR" precision="2" scale="0" />
            </key-property>
        </composite-id>
        <many-to-one name="ecoCmdy" class="com.uprr.app.eco.persistenceDelete.EcoCmdyDelete" update="false" insert="false" fetch="select">
            <column name="STCC_CODE" length="7" not-null="true" />
        </many-to-one>
        <property name="crtnDt" type="date">
            <column name="CRTN_DT" length="7" not-null="true" />
        </property>
        <property name="crtnUserId" type="string">
            <column name="CRTN_USER_ID" length="8" not-null="true" />
        </property>
        <property name="lastUptdDt" type="date">
            <column name="LAST_UPTD_DT" length="7" not-null="true" />
        </property>
        <property name="lastUptdUserId" type="string">
            <column name="LAST_UPTD_USER_ID" length="8" not-null="true" />
        </property>
        <property name="emgyRespText" type="string">
            <column name="EMGY_RESP_TEXT" length="80" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
        Auto-generated mapping file from
        the hibernate.org cfg2hbm engine
-->
    <class name="com.uprr.app.eco.persistenceDelete.EcoHzmtNameDescDelete" table="ECO_HZMT_NAME_DESC"  schema="ECO">
        <composite-id name="id" class="com.uprr.app.eco.persistenceDelete.EcoHzmtNameDescId">
            <key-property name="stccCode" type="string">
                <column name="STCC_CODE" length="7" />
            </key-property>
            <key-property name="regAuthCode" type="string">
                <column name="REG_AUTH_CODE" length="1" />
            </key-property>
            <key-property name="nameDescType"  type="string">
                <column name="NAME_DESC_TYPE" length="4" />
            </key-property>
            <key-property name="grpNbr" type="byte">
                <column name="GRP_NBR" precision="2" scale="0" />
            </key-property>
            <key-property name="lineNbr" type="byte">
                <column name="LINE_NBR" precision="2" scale="0" />
            </key-property>
        </composite-id>
        <many-to-one name="ecoHzmtDescLine" class="com.uprr.app.eco.persistenceDelete.EcoHzmtDescLineDelete" fetch="select">
            <column name="NAME_DESC_ID" precision="10" scale="0" not-null="true" />
        </many-to-one>
        <many-to-one name="ecoCmdy" class="com.uprr.app.eco.persistenceDelete.EcoCmdyDelete" update="false" insert="false" fetch="select">
            <column name="STCC_CODE" length="7" not-null="true" />
        </many-to-one>
        <property name="primInd"  type="string">
            <column name="PRIM_IND" length="1" />
        </property>
        <property name="crtnDt" type="date">
            <column name="CRTN_DT" length="7" not-null="true" />
        </property>
        <property name="crtnUserId" type="string">
            <column name="CRTN_USER_ID" length="8" not-null="true" />
        </property>
        <property name="lastUptdDt" type="date">
            <column name="LAST_UPTD_DT" length="7" not-null="true" />
        </property>
        <property name="lastUptdUserId" type="string">
            <column name="LAST_UPTD_USER_ID" length="8" not-null="true" />
        </property>
    </class>
</hibernate-mapping>



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
        Auto-generated mapping file from
        the hibernate.org cfg2hbm engine
-->
    <class name="com.uprr.app.eco.persistenceDelete.EcoHzmtDescLineDelete" table="ECO_HZMT_DESC_LINE" schema="ECO">
        <id name="nameDescId" type="long">
            <column name="NAME_DESC_ID" precision="10" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="hzmtNameDesc" type="string">
            <column name="HZMT_NAME_DESC" length="25" not-null="true" />
        </property>
        <property name="crtnDt" type="date">
            <column name="CRTN_DT" length="7" not-null="true" />
        </property>
        <property name="crtnUserId" type="string">
            <column name="CRTN_USER_ID" length="8" not-null="true" />
        </property>
        <set name="ecoHzmtNameDescs" inverse="true">
            <key>
                <column name="NAME_DESC_ID" precision="10" scale="0" not-null="true" />
            </key>
            <one-to-many class="com.uprr.app.eco.persistenceDelete.EcoHzmtNameDescDelete" />
        </set>
    </class>
</hibernate-mapping>

HarikrishnaYaminiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
> Caused by: java.sql.BatchUpdateException: ORA-01407: cannot update ("ECO"."ECO_HZRD_EMGY_RESP"."STCC_CODE") to NULL

whats your schema look like?
0
HarikrishnaYaminiAuthor Commented:
why is it  trying to update. I am trying to delete
0
objectsCommented:
A delete could result in change to foreign keys oin other tables
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

x4uCommented:
all keys in columns that refernce to the deleted objects need to be set to null. To get rid of the exception you can in this section:
...
        <set name="ecoHzrdEmgyResps" cascade="delete-orphan" >
            <key>
                <column name="STCC_CODE" length="7" not-null="true" />
            </key>
            <one-to-many class="com.uprr.app.eco.persistenceDelete.EcoHzrdEmgyRespDelete" />
        </set>
...
change the column attribute not-null="true" to not-null="false"

This will allow the deletion which is otherwise forbidden by the not-null attribute.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HarikrishnaYaminiAuthor Commented:
how can i fix the issue

CREATE TABLE ECO_CMDY
(
  STCC_CODE            VARCHAR2(7)              NOT NULL,
  HZRD_IND             VARCHAR2(1),
  PROD_CLAS_STCC_CODE  VARCHAR2(7),
  EFF_DATE             DATE,
  CMDY_ABRV            VARCHAR2(6),
  INTL_HMZD_CODE       VARCHAR2(6),
  SVC_RQMT_CODE_1      VARCHAR2(1),
  SVC_RQMT_CODE_2      VARCHAR2(1),
  SVC_RQMT_CODE_3      VARCHAR2(1),
  WGT_TOLR_FCTR        NUMBER(1),
  CMDY_BASE_NBR        NUMBER(3),
  UNNA_NBR             VARCHAR2(6),
  CRTN_DT              DATE                     NOT NULL,
  CRTN_USER_ID         VARCHAR2(8)              NOT NULL,
  LAST_UPTD_DT         DATE                     NOT NULL,
  LAST_UPTD_USER_ID    VARCHAR2(8)              NOT NULL,
  CMDY_DESC            VARCHAR2(720)
)
TABLESPACE ECOTS101
PCTUSED    85
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
LOGGING
NOCACHE
NOPARALLEL;


CREATE UNIQUE INDEX ECO_CMDY_PK ON ECO_CMDY
(STCC_CODE)
LOGGING
TABLESPACE ECOXS001
PCTFREE    5
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


CREATE INDEX ECO_CMDY_X1 ON ECO_CMDY
(PROD_CLAS_STCC_CODE)
LOGGING
TABLESPACE ECOXS001
PCTFREE    5
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


CREATE INDEX ECO_CMDY_X2 ON ECO_CMDY
(UNNA_NBR)
LOGGING
TABLESPACE ECOXS001
PCTFREE    5
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


CREATE INDEX ECO_CMDY_X3 ON ECO_CMDY
(CMDY_DESC)
LOGGING
TABLESPACE ECOXS001
PCTFREE    5
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


CREATE INDEX ECO_CMDY_X4 ON ECO_CMDY
(CMDY_ABRV)
LOGGING
TABLESPACE ECOXS001
PCTFREE    5
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


CREATE PUBLIC SYNONYM ECO_CMDY FOR ECO_CMDY;


ALTER TABLE ECO_CMDY ADD (
  CONSTRAINT ECO_CMDY_PK PRIMARY KEY (STCC_CODE)
    USING INDEX
    TABLESPACE ECOXS001
    PCTFREE    5
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128K
                NEXT             128K
                MINEXTENTS       1
                MAXEXTENTS       2147483645
                PCTINCREASE      0
                FREELISTS        1
                FREELIST GROUPS  1
               ));


ALTER TABLE ECO_CMDY ADD (
  CONSTRAINT ECO_CMDY_F1 FOREIGN KEY (PROD_CLAS_STCC_CODE)
    REFERENCES ECO_CMDY (STCC_CODE));


CREATE TABLE ECO_HZRD_EMGY_RESP
(
  STCC_CODE          VARCHAR2(7)                NOT NULL,
  LINE_NBR           NUMBER(2)                  NOT NULL,
  CRTN_DT            DATE                       NOT NULL,
  CRTN_USER_ID       VARCHAR2(8)                NOT NULL,
  LAST_UPTD_DT       DATE                       NOT NULL,
  LAST_UPTD_USER_ID  VARCHAR2(8)                NOT NULL,
  EMGY_RESP_TEXT     VARCHAR2(80)               NOT NULL
)
TABLESPACE ECOTS101
PCTUSED    85
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
LOGGING
NOCACHE
NOPARALLEL;


CREATE UNIQUE INDEX ECO_HZRD_EMGY_RESP_PK ON ECO_HZRD_EMGY_RESP
(STCC_CODE, LINE_NBR)
LOGGING
TABLESPACE ECOXS101
PCTFREE    5
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


CREATE PUBLIC SYNONYM ECO_HZRD_EMGY_RESP FOR ECO_HZRD_EMGY_RESP;


ALTER TABLE ECO_HZRD_EMGY_RESP ADD (
  CONSTRAINT ECO_HZRD_EMGY_RESP_PK PRIMARY KEY (STCC_CODE, LINE_NBR)
    USING INDEX
    TABLESPACE ECOXS101
    PCTFREE    5
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          4M
                NEXT             4M
                MINEXTENTS       1
                MAXEXTENTS       2147483645
                PCTINCREASE      0
                FREELISTS        1
                FREELIST GROUPS  1
               ));


ALTER TABLE ECO_HZRD_EMGY_RESP ADD (
  CONSTRAINT ECO_HZRD_EMGY_RESP_F1 FOREIGN KEY (STCC_CODE)
    REFERENCES ECO_CMDY (STCC_CODE));






0
x4uCommented:
While changing the not-null attribute would avoid the exception there might be a good reason for the column to be defined to not allow NULL values to ensure a valid object graph. So it would probably a more appropriate solution to find the associated (referring) EcoHzmtNameDescId objetcs and delete them before you delete the referenced object.
0
x4uCommented:
By the way, you have several cascade="delete-orphan" attributes in your mapping, but as far as I know there is no "delete-orphan" option for cascade only a "all-delete-orphan". If you correct this you should get the referenced objects deleted automatically with the object you tried to delete and this could fix your problem.
0
HarikrishnaYaminiAuthor Commented:
I tried the all-delete-orphan, it didn't help. Getting the same exception
0
HarikrishnaYaminiAuthor Commented:
It is not deleting the entities from the database.
0
HarikrishnaYaminiAuthor Commented:
I figured out.
0
HarikrishnaYaminiAuthor Commented:
Hi x4u,

You comment was little helpful in soving the problem. I was able to solve the problem by setting inverse="true" .And the soloution is somewhat related to your comment, the way hibernate updates the FK  values before deleting. I want to give you a partial credit of 200 points.  Don't know how to spit the 500 points
0
x4uCommented:
It's great that you found a solution for this. Sorry that I couldn't be more helpful, I haven't used hibernate that often yet.

As far as I know it is not possible to split the points without giving the rest to someone else (i.e. to objects who mentioned the foreign keys first). You could also just give it a lower grade with all the points or ask in Community Support to get some of your points refunded because you found and posted the answer yourself.
http://www.experts-exchange.com/Community_Support/
0
sbugganaCommented:
Hi, I am facing different error, the above discussion thread is more towards when deleteing the records from referenced tables, but in my case when I am trying to create the tables (that are having references) using Hibernate merge() method.

I am copy pasting the error stack trace below, can you extend your help.


Caused by: java.sql.BatchUpdateException: ORA-01407: cannot update ("CRMSTG"."OCC_FIF_CLIENT_OWN_FINANCIALS"."OCC_ACCOUNT_PLAN_MASTER_GR_ID") to NULL

    at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
    at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10657)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1592)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.