tags:

views:

299

answers:

2

Hi

In my project i m using using hibernate and oracle as DB.

I am having two tables with foreign key relationship.

In hibernate I m have one-to-many relationship.

my one-to-many code

set name="classname" cascade="all,all-delete-orphan

one-to-many ..... on-delete="cascade"

set

But whenever i detele i am getting constraint violation error... ora:222 error cannot delete it has child table I have cascade="all" but y still the error comes...

Same error while inserting too. I am having primarykey as ID attribute which is sequence. So in foreignkey ID attribute its not inserting and getting null cannot be inserted error .. Have any one experienced these errors ?

+1  A: 

Try:

 cascade="all,delete-orphan"

or:

 cascade="all-delete-orphan"

instead of:

 cascade="all,all-delete-orphan"

Also, make sure you mark one side of the association with:

inverse="true"

if the association is bi-directional.

If that doesn't solve your issue, please try to clean up your explanation a little and provide more information (like the full mapping files and Java classes for the two objects involved).

RMorrisey
A: 

Hi i am still getting the error. here is my code

table 1

public class ManualDDAData implements Serializable {

private Long ddaDealNumber;

private String clientGCI;

private Date dealDate;

private Long dealSize;

private String productCd;

private Long reasonId;

private String currencyCd;

private String currencyNm;

private String dealDesc;

private String dealComment;

private String reasonName;

private String modifiedBy;

private Date modifiedDt;

private Set dealAdvisors;

/**
 * @return Returns the clientGCI.
 */
public String getClientGCI() {
    return clientGCI;
}

/**
 * @param clientGCI The clientGCI to set.
 */
public void setClientGCI(String clientGCI) {
    this.clientGCI = clientGCI;
}

/**
 * @return Returns the dealDate.
 */
public Date getDealDate() {
    return dealDate;
}

/**
 * @param dealDate The dealDate to set.
 */
public void setDealDate(Date dealDate) {
    this.dealDate = dealDate;
}

/**
 * @return Returns the dealDesc.
 */
public String getDealDesc() {
    return dealDesc;
}

/**
 * @param dealDesc The dealDesc to set.
 */
public void setDealDesc(String dealDesc) {
    this.dealDesc = dealDesc;
}

/**
 * @return Returns the dealSize.
 */
public Long getDealSize() {
    return dealSize;
}

/**
 * @param dealSize The dealSize to set.
 */
public void setDealSize(Long dealSize) {
    this.dealSize = dealSize;
}

/**
 * @return Returns the productCd.
 */
public String getProductCd() {
    return productCd;
}

/**
 * @param productCd The productCd to set.
 */
public void setProductCd(String productCd) {
    this.productCd = productCd;
}

/**
 * @return Returns the reasonId.
 */
public Long getReasonId() {
    return reasonId;
}

/**
 * @param reasonId The reasonId to set.
 */
public void setReasonId(Long reasonId) {
    this.reasonId = reasonId;
}

/**
 * @return Returns the reasonName.
 */
public String getReasonName() {
    return reasonName;
}

/**
 * @param reasonName The reasonName to set.
 */
public void setReasonName(String reasonName) {
    this.reasonName = reasonName;
}

/**
 * @param modifiedBy The modifiedBy to set.
 */
public void setModifiedBy(String modifiedBy) {
    this.modifiedBy = modifiedBy;
}

/**
 * @param modifiedDt The modifiedDt to set.
 */
public void setModifiedDt(Date modifiedDt) {
    this.modifiedDt = modifiedDt;
}

/**
 * @return Returns the modifiedBy.
 */
public String getModifiedBy() {
    return modifiedBy;
}

hbm file :

    <id name="ddaDealNumber" column="DDA_DEAL_NO" type="java.lang.Long">
         <generator class="sequence">
            <param name="sequence">BANKNET.DDA_DEAL_ID_SEQ</param>
        </generator>
    </id>

    <property name="clientGCI" type="java.lang.String" update="true"
        insert="true" access="property" column="GCI" />

    <property name="dealDate" type="java.util.Date"
        update="true" insert="true" access="property" column="DEAL_DATE" />

    <property name="dealSize" type="java.lang.Long" update="true"
        insert="true" access="property" column="DEAL_SIZE" />

    <property name="productCd" type="java.lang.String" update="true"
        insert="true" access="property" column="PRODUCT_CD" />

    <property name="reasonId" type="java.lang.Long" update="true"
        insert="true" access="property" column="REASON_ID" />

    <property name="reasonName" type="java.lang.String" update="false"
        insert="false" access="property"  >
        <formula>(SELECT ddar.REASON FROM BANKNET.DDA_REASONS ddar WHERE ddar.ID = REASON_ID)</formula> 
    </property>

    <property name="currencyCd" type="java.lang.String" update="true"
        insert="true" access="property" column="CURRENCY_CD" />

    <property name="currencyNm" type="java.lang.String" update="false"
        insert="false" access="property" >
        <formula>(SELECT curd.CY_NM FROM IBK_DW.CURRENCY_DIM curd WHERE curd.CY_CD = CURRENCY_CD)</formula> 
    </property>

    <property name="dealDesc" type="java.lang.String" update="true"
        insert="true" access="property" column="DEAL_DESC" />

    <property name="dealComment" type="java.lang.String" update="true"
        insert="true" access="property" column="DEAL_COMMENT" />

    <property name="modifiedBy" type="java.lang.String"
        update="true" insert="true" access="property" column="MODIFIED_BY" />

    <property name="modifiedDt" type="java.util.Date" update="true"
        insert="true" access="property" column="MODIFIED_DATE" />

   <!-- set name="dealAdvisors" inverse="true" cascade="all,all-delete-orphan" fetch="select" lazy="false">
        <key  column="DDA_DEAL_NO" on-delete="cascade" />
        <one-to-many class="com.bofa.crme.dda.domain.DealAdvisor" />
    </set-->

    <set name="dealAdvisors" inverse="true" cascade="all,delete-orphan" lazy="false">
        <key  column="DDA_DEAL_NO" not-null="true" on-delete="cascade"/>
        <one-to-many class="com.bofa.crme.dda.domain.DealAdvisor" />
    </set>


</class>

table 2:

public class DealAdvisor implements Serializable {

private Long ddaDealNumber;

private String dealAdvisorNm;

private String modifiedBy;

private Date modifiedDt;

private ManualDDAData ddaDeal;

public DealAdvisor() {
}

public String toString() {
    return (new ToStringBuilder(this)).append("ddaDealNumber", getDdaDealNumber())
            .toString();
}

/**
 * @return Returns the modifiedBy.
 */
public String getModifiedBy() {
    return modifiedBy;
}

/**
 * @param modifiedBy The modifiedBy to set.
 */
public void setModifiedBy(String modifiedBy) {
    this.modifiedBy = modifiedBy;
}

/**
 * @return Returns the modifiedDt.
 */
public Date getModifiedDt() {
    return modifiedDt;
}

/**
 * @param modifiedDt The modifiedDt to set.
 */
public void setModifiedDt(Date modifiedDt) {
    this.modifiedDt = modifiedDt;
}

/**
 * @return Returns the ddaDealNumber.
 */
public Long getDdaDealNumber() {
    return ddaDealNumber;
}

/**
 * @param ddaDealNumber The ddaDealNumber to set.
 */
public void setDdaDealNumber(Long ddaDealNumber) {
    this.ddaDealNumber = ddaDealNumber;
}

/**
 * @return Returns the dealAdvisorNm.
 */
public String getDealAdvisorNm() {
    return dealAdvisorNm;
}

/**
 * @param dealAdvisorNm The dealAdvisorNm to set.
 */
public void setDealAdvisorNm(String dealAdvisorNm) {
    this.dealAdvisorNm = dealAdvisorNm;
}

/**
 * @return Returns the ddaDeal.
 */
public ManualDDAData getDdaDeal() {
    return ddaDeal;
}

/**
 * @param ddaDeal The ddaDeal to set.
 */
public void setDdaDeal(ManualDDAData ddaDeal) {
    this.ddaDeal = ddaDeal;
}

}

hbm file :

        <key-property name="ddaDealNumber" column="DDA_DEAL_NO" type="java.lang.Long"/>
        <key-property name="dealAdvisorNm" column="ADVISOR_NM" type="java.lang.String"/>

        <!-- key-many-to-one name="ddaDealNumber" class="com.bofa.crme.dda.domain.ManualDDAData" column="DDA_DEAL_NO"/-->

    </composite-id>

    <property name="modifiedBy" column="MODIFIED_BY" type="java.lang.String" update="true" insert="true" access="property" />
    <property name="modifiedDt" column="MODIFIED_DATE" type="java.util.Date" update="true"  insert="true" access="property"/>
</class>

error for delete :

[ERROR] 2010-02-19 09:09:06 (JDBCExceptionReporter.java:logExceptions:78) ORA-02292: integrity constraint (BANKNET.FK_DDA_DEAL_ADVISOR) violated - child record found

[ERROR] 2010-02-19 09:09:06 (AbstractFlushingEventListener.java:performExecutions:301) Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: could not delete: [com.bofa.crme.dda.domain.ManualDDAData#10061]

is this because there is no on delete cascade constraint given in oracle ??

for insert i am getting this error

[ERROR] 2010-02-19 09:10:51 (JDBCExceptionReporter.java:logExceptions:78) ORA-01400: cannot insert NULL into ("BANKNET"."DDA_DEAL_ADVISOR"."DDA_DEAL_NO")

[ERROR] 2010-02-19 09:10:51 (AbstractFlushingEventListener.java:performExecutions:301) Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: could not insert: [com.bofa.crme.dda.domain.DealAdvisor]

any clues ?

Senthilnathan
For the insert: verify that DdaDealNumber property is set on the java side at the point where you are going to save the record. It looks like you have null for that property. For the delete, it's telling you that you don't have a hibernate mapping for the "ddaDeal" property on your DealAdvisor object
RMorrisey