Hibernate child cascade delete child???

i am facing a problem on the cascade delete of a parent object, i try to explain the problem by example.
Parent object C
Child object D,E,F
i want to delete the parent object "delete(c) ".
before the parent can be deleted the child D,E and F has to be deleted.
My relations are defined with cascade="all-delete-orphan" in the parent
object. In my child no cascade is defined.
When i check the logging on deletion of the object i see that hibernate
tries to update reference in the child to the parent by setting the value to
null. But since the the reference is obligated (NOT NULL in database)
i get a not null constraint violation.
My first question is why does hibernate not delete the child record instead of updating the reference (since my child object is remove
from my collection in the parent object)?
Could some one tell me what i am doing wrong? or does there not exist a solution.
thnx

First, is that a typo? The docs for 3.1 that I'm looking at say "all,delete-orphan", but you have "all-delete-orphan".
Second, I thought (and it's been a long time since I've needed to do this) that you defined the cascading in the child. I could be wrong though.
What kind of relationship is this? The docs say that delete-orphan only applies to one-to-many.
You might want to go through the pertinent sections of these if you haven't already:
http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html
http://www.hibernate.org/hib_docs/v3/reference/en/html/collections.html
[url http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html#objectstate-transitive]http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html#objectstate-transitive

Similar Messages

  • Cascade delete in hibernate 3

    i am having a problem deleting child object
    here is my code
    my hibernate files are generated with xdoclet
    I want to remove an object AFVALSTOF with all it's childs. the relationship is bi-directional. parent object is AFVALSTOF and as example child is OVERBRENGER. the relationship is obligated (not-null="true")
    By viewing the hibernate logging i can see that the key field of the child is update to null first but this is an obligated field.
    Can anyone help me on how to define my relationship so i can execute the cascade delete?
    This is a part of my parent object (AFVALSTOF) which causes
    problems
    /     private Set overbrenger = new HashSet( );
         * @hibernate.set tablename="overbrenger" lazy="true" inverse="false"
         * cascade="all-delete-orphan"
         * @hibernate.collection-key column="ovbr_afvs_id"
         * @hibernate.collection-one-to-many class="be.milieuinfo.imjv.database.model.afval.Overbrenger"
         * @return Set
         public Set getOverbrenger() {
              return overbrenger;
         * @param overbrenger
         * Overbrenger
         public void setOverbrenger(Set overbrenger) {
              this.overbrenger = overbrenger;
    THIS IS THE CHILD (OVERBRENGER) which has a relationship to the PARENT called AFVALSTOF
    private Afvalstof afvalstof ;
    * @hibernate.many-to-one column="ovbr_afvs_id" class=
         * "be.milieuinfo.imjv.database.model.afval.Afvalstof"
         * not-null="true" inverse="true" cascade="all"
    * @return Afvalstof
    public Afvalstof getAfvalstof() {
         return afvalstof ;
    * @param afvalstof
    public void setAfvalstof(Afvalstof afvalstof) {
         this.afvalstof = afvalstof ;
    thnx
    Stefaan

    <?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>
      <class name="com.orly_otero.patterns.VO.ArticleVO" table="tblArticles">
        <id name="idArticle" type="long">
          <column name="id_Article" not-null="true"/>
          <generator class="native"/>
        </id>
        <property name="title" type="string">
          <column name="Title" length="200" not-null="true"/>
        </property>
        <property name="abstracT" type="string">
          <column name="Abstract" length="2000" not-null="true"/>
        </property>
        <property name="keywords" type="string">
          <column name="Keywords" length="200" not-null="true"/>
        </property>
        <property name="type" type="string">
          <column name="type" length="1" not-null="true"/>
        </property>
        <property name="position" type="integer">
          <column name="position" not-null="false"/>
        </property>
        <property  name="isBriefContribution" type="boolean">
          <column name="isBriefContribution" not-null="false"/>
        </property>
        <many-to-one name="issueVO" column="id_Issue" class="com.orly_otero.patterns.VO.IssueVO" not-null="true"/>
        <set name="authorsVO" table="tlbArticlesAuthors" lazy="true" cascade="save-update">
          <key column="id_Article"/>
          <many-to-many class="com.orly_otero.patterns.VO.AuthorVO" column="id_Author"/>
        </set>
        <set name="sectionsVO" inverse="true" lazy="true" cascade="all" order-by="position ASC">
          <key column="id_Article" not-null="true"/>
          <one-to-many class="com.orly_otero.patterns.VO.SectionVO"/>
        </set>
      </class>
    </hibernate-mapping>

  • Problem with cascade delete and remove bean

    I am working with two entity beans that map to two tables that have a foreign key relationship. Table B has a foreign key to A and in the database that foreign key is set for cascaded updates and cascaded deletes.
    The problem occurs when the the sytem first tries to remove bean 1 (mapped to table A) and then remove bean 2 (mapped to B) where bean 2 is associated with bean 1 with a foreign key relationship. The first remove works but then when it tries to remove bean 2 it throws a very ugly "CORBA TRANSACTION_ROLLEDBACK 9998" Maybe exception. My guess is that the reason is because bean 2's reocrd in the database was deleted when bean 1 was removed but the 'bean object' was not removed from the container.
    When I go into our Application Server and look at how it see's the tables, it show the wrong relationship. It shows a restrict delete and a restrict update realationship.
    My question is, am I wrong to think that this is a application server problem or a configuration problem? It seems to me that attempting to remove a non-existant record should not cause an error. It won't cause any SQL exceptions. Is this a flawed viewpoint? As a work around I made sure that the dependent records are deleted first but it kind of defeats the point of cascaded deletes.
    We have a limited number of support calls, should I use one or am I at fault here?

    If the database removes the record from the second
    table, why is the system trying to remove it once
    again? You should try to remove an entity from a
    single place, should it be the database or the
    application. Don't try to remove it twice.
    Regards,
    DimitarI could do this but it is a huge pain in my ass. The problem is that you might want to remove the dependent bean without removing it's parent. The object structure is a little questionable, I'll admit that. It is, as they say, the eleventh hour and I can't really change that now.
    The way this work is that the server gets a list of objects marked either as new, modified, or deleted. It then relates those changes back to the database.
    In this case we have two lists(which makes me realize where the class structure sucks.) In order to do what you suggest I would have to get all the deleted parent objects and then search all the deleted child objects for ones that have the parent's key in them.
    It would be prefferable to fix the class structure but again this is not an option.
    Anyone want to answer the question I asked?

  • Privately Owned and Cascade Delete in DB

    HI,
    What is the relationship between setting privately owned in Toplink vs creating a cascade delete in Oracle DB? If I've the cascade delete in DB then what is the advantage of setting the privately owned flag in toplink?
    Thanks

    If you only set the cascade delete option in the database, then TopLink will have no knowledge that the private object was deleted (or that it is private in general). This would mean that TopLink would not remove the private object from the cache or process it as have being deleted. In general this may not cause your app any direct issues if you never read the private object directly as it will eventually garbage collect from the cache.
    Making the relationship privately own in TopLink will also ensure the following:
    - If you replace the private object with another, or set it to null, TopLink will delete the old object on the update of the source. Cascade delete only works on deletion.
    - If the relationship is a 1-m and you remove an object from the collection it will be removed.
    - If you refresh the parent object, by default the child will also be refreshed.
    - If you merge the parent object, by default the child will also be merged.

  • Cascade delete in privatelyownedrelationships

    If a one-to-many mapping is defined as a privately owned relationship, when I delete a record from this collection, I get integrity constraint exception stating that
    'child record exits', in a privately owned collection, dont the child records get deleted automatically,
    We are removing a record from the collection and trying a commit thru UOW. thanks

    Looks like a bug in the way the cascade delete is being done, if we remove the constraints then the cascade delete works.

  • Cascade delete in many to many association

    Hi All,
        I have a usecase, there is an association with many to many cardinality  between 3 EO's. If i delete  any record in master, the corresponding childs all should delete.
        So appreciate if any alternatives on above usecase. Thanks in advance

    Hi,
    cascade delete is a function of the database. When you enable cascade delete for an entity operation then this only produces the proper SQL code for deleting a data object. If you have a many-to-many relation then this means you have multiple master and child records, which I think makes your "If i delete  any record in master, the corresponding childs all should delete" impossible.
    for what its worth, please read
    Creating a Business Domain Layer Using Entity Objects - 11g Release 2 (11.1.2.3.0)
    to learn how to parse and delete detail records  from an entity object
    http://docs.oracle.com/cd/E35521_01/web.111230/e16182/bcentities.htm#CEGJAFCF
    The last document is about posting orders (e.g. create a master record before you create detail records). However, with a few changes you can reverse this to perform a delete operation (just add this code to the entity impl classes of the entities so they delete their child objects
    Frank

  • Cascade delete in CMP 1:M relationship

    All,
    I have 2 CMP entity beans with 1:M relationship and cascade delete option setup. All functions of Add/Mod are working fine.
    Even cascade delete is getting trigged but not executing properly every time. Out of more than 100 records in the child table, only 99 are getting cascade deleted. I am deploying the beans using embedded OC4J which comes with JDeveloper.
    Is anyone aware of any such limitations or faced similar problem before ? Database tables don't have any integrity constraint setup.
    Thanks,
    Dhiraj

    The problem is that by making the AddrXRef privately owned it cannot be reparented. Once it is removed from it's parent's list (either one) it will be deleted. To achieve what you're trying to do you'll need to delete the old incorrect AddrXRef and replace it with a new correct one.
    From the TopLink Developer's Guide[1]:
    When you tell TopLink that a relationship is privately owned, you are specifying that:
    * If the source of a privately owned relationship is deleted, then delete the target.
    * If you remove the reference to a target from a source, then delete the target.
    --Shaun
    [1] http://www.oracle.com/technology/products/ias/toplink/doc/10131/main/_html/uowbas007.htm#i1134011

  • Let the database doing cascade deletion for privateOwned relationship

    Hi,
    I have two tables: parent, child. I mapped the parent to child as a one-many private owned relationship. The child table have a foreign key reference back to parent table. The constraint is like this
    alter table child add constraint FK_CONSTRAINT foreign key (parentid) reference parent (parentid) on delete cascade;
    I would the cascade deletion is done by database (as it is "on delete cascade" in the constraint) rather by the toplink. (This is exactly what you will do when use JDBC. another reason is that if I let the TopLink do the cascade deletion, I got a deadlock! If I remove the cascade deletion from topLink, I do not have the deadlock).
    Is there anyway I can turn off the cascade deletion from topLink, but at the same side keep the private owned relationship(because it is indeed privately owned)?
    Any help is really appreciated.
    jason

    Setting a no-op sql solves the cascade deletion problem. However, if I really want to remove a child from the parently, I ends up using the same no-op sql. I can not delete child independently.

  • Problem in cascade-delete

    I've using cascade delete for delete rows from two entity beans.......
    but my problem is that if I'm doing it from ejb side...i.e. setting "fk_constraint" to false, and setting cascade-delete in ejb-jar.xml (& not in database), then its working fine....
    but if I remove these tags from the ejb-jar and in the database, specify foreign key and ON DELETE CASCADE, then its not working....it is giving the error...
    "can't add or update a child row...foreign key constraint fails".....
    I've read somewhere that this cascade delete option should be specified either on the ejb side or on the db side..and not on both the sides......but in run-time only ejb side is working...
    Plz. help me out coz I want the deletion from db side...as thr are some other tables not used in ejb but dependant on the one used in the ejb side.

    I think that you must specify <cascade-delete/> tag in ejb-jar.xml ONLY. But not in database.

  • EF database first cascades delete despite cascade is restricted in db and edmx setting End1/2 OnDelete = "None"

    We are developing with Visual Studio 2013, Entity Framework 5, Oracle DB 11 via ODP.NET managed driver.
    The edmx is generated/updated from database (database first). We generate POCO object using standard T4.
    We have 2 tables with a master-detail foreign key relation, one to many. The foreign key in database does _not_ allow cascade deletes, so when you delete master row via SQL, you get error " ... child record found ... ".
    The problem now is:
    In Entity Framework, when i delete a record from the master table, then EF also deletes all corresponding detail records from the detail table! The restriction of the foreign key in database is ignored.
    In edmx diagram, the properies End1 OnDelete / End2 OnDelete are set to "None", so i expect that on delete of a master row, EF should throw an error if detail rows exists.
    How can i disable the cascading of the delete?
    Here is the FK definition in edmx:
            <Association Name="USR_USER_ROLES_ROLES_FK">
              <End Role="USR_ROLES" Type="Self.USR_ROLES" Multiplicity="1" />
              <End Role="USR_USER_ROLES" Type="Self.USR_USER_ROLES" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="USR_ROLES">
                  <PropertyRef Name="GES" />
                  <PropertyRef Name="PROJ" />
                  <PropertyRef Name="ROLE_NAME" />
                </Principal>
                <Dependent Role="USR_USER_ROLES">
                  <PropertyRef Name="GES" />
                  <PropertyRef Name="PROJ" />
                  <PropertyRef Name="ROLE_NAME" />
                </Dependent>
              </ReferentialConstraint>
            </Association> 
     The delete of master row is done by calling:
    ivDbCtx.USR_Roles.Local.Remove(role); // ivDbCtx is the DbContext, USR_Roles is the collection, role is the POCO object to be deleted.
    Thank you very much for your help!
    Udo

    Hello,
    For issues regarding ODP.NET managed driver, I would suggest you posting to oracle entity framework forum:
    https://community.oracle.com/communications
    There are oracle entity framework expects who will help you better.
    Thanks for your understanding.
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Cascade delete in container or database schema?

    I am creating a database schema which I want to map EJB CMP Entity Beans onto. There are a number of relationships in the schema where I want to cascade deletion if a parent record is deleted and a number of relationships where I want to prevent deletion of parent records if child records exist for the relationship.
    I am wondering whether it is better to implement this in the database schema (Oracle) or in the container (JBoss 3.2.2)? I believe it is possible to implement cascading delete in the container but I am not sure about preventing delete in the container? Is there a way I could do it programmatically? I want to use CMP though so I'm not sure there would be much scope for that.
    Are there any rules as to which approach is better? Will the container have a problem if I implement it in the database schema? Obviously the ejbRemove may throw an exception if I prevent deletion in a relationship but that is fine. I believe it is not a good idea to implement it in both the container and schema as there will be a conflict when they both try to delete records.
    Thanks.

    Container's cascade-deleting in jboss(tm)3.2.x working properly with any scope:
    This example with 1 scope:
        <relationships>
            <ejb-relation>
                <ejb-relation-name>Region-Town</ejb-relation-name>
                <ejb-relationship-role>
                    <ejb-relationship-role-name>region-has-towns</ejb-relationship-role-name>
                    <multiplicity>One</multiplicity>
                    <relationship-role-source>
                        <ejb-name>RegionBean</ejb-name>
                    </relationship-role-source>
                    <cmr-field>
                        <cmr-field-name>regionTowns</cmr-field-name>
                        <cmr-field-type>java.util.Set</cmr-field-type>
                    </cmr-field>
                </ejb-relationship-role>
                <ejb-relationship-role>
                    <ejb-relationship-role-name>town-belongs-to-region</ejb-relationship-role-name>
                    <multiplicity>Many</multiplicity> <cascade-delete/>
                    <relationship-role-source>
                        <ejb-name>TownBean</ejb-name>
                    </relationship-role-source>
                    <cmr-field>
                        <cmr-field-name>region</cmr-field-name>
                    </cmr-field>
                </ejb-relationship-role>
            </ejb-relation>
        </relationships>

  • Cascading Delete in BC4J-UIXml application

    Morning all,
    I have a problem with a UIXml application that I was wondering somebody could help me with. Since I am new to this technology, please excuse the simplistic nature of the question.
    I have a BC4J application working through UIX pages. In this application I have a form that deletes a record from the table. However, I am unable to determine how to perform an automatic cascading delete on the record since it has child records in another table.
    Please advise. I shall update this thread after a successful workaround in order to provide a reference for others who may encounter this problem.
    Thanks in advance for reading this post.
    Adeel

    This was resolved in the entity's association properties.

  • Virtual private database and cascade delete

    We can't secure rows deleted by cascade delete by vpd.
    User "A" have right to delete row "1" but don't have right to delete row "2".
    If user "A" delete row "1", database will delete also row "2" by cascade delete.
    Why it is possible to delete row "2"?

    Either of the two options (a policy that queries the base table or propagating the security columns to the child tables) should work.
    My preference from a data model standpoint would be to have a policy that queries the base table so that the security information can be maintained in exactly one place. But if your application is frequently querying the child tables without reference to the parent table, joining to the parent table, particularly if data volumes are such that an IN would be less than ideal, there may be performance issues to this approach.
    Copying the columns creates a potential data integrity problem-- if you change the security on a base table row, you may forget to change the security on all the child records. But that issue may not be significant if the security is essentially static once the rows are created. And it's definitely easier to tune.
    You may also want to create views that join the parent and child tables and grant users access to these views rather than to the child tables directly, which would allow you to have a single policy on the base table and get most of the benefits of the first option with less performance problems if data volumes make an IN less than ideal.
    Justin

  • Master detail form - Cascade Delete gives error

    I am using 3.0.9.8.2 and have made a master detail form which works fine except for cascade delete. Insert and update works well, but I have to delete the child rows first to be able to delete the parent.
    "Cascade Delete Detail Rows on Master Delete" is checked.
    Error situation 1 (where the master just has one child row (number of detail rows to be displayed = 2)):
    When trying master action=delete I get:
    Error: No conversion performed for type INTEGER, value . (WWC-49102)
    This happens whatever is chosen from the detail action drop down list during the attempted delete.
    Error situation 2 (where the master has two child rows (number of detail rows to be displayed = 2)):
    When trying master action=delete I get:
    Error: An unexpected error occurred: ORA-02292: integrity constraint (MILLS_PORTAL.TEX_ACCOUNTS_FK) violated - child record found (WWV-16016)
    This also happens whatever is chosen from the detail action drop down list.

    Sorry to say, but this is a bug which will get fixed in the forth coming releases.
    Till then, there are 2 ways to avoid this problem:-
    1> The change the foreign key in the detail table to have an ON DELETE CASCADE clause.
    2> (Though not desirable, but still as a workaround)- Drop the foreign key constraint from the detail table.

  • Cascade Deletes

    Could anyone please suggest how the following could be done programatically i.e without using database triggers?
    I have a Java form created using InfoSwing components where the Master-Detail relation is nested to several levels ( like Master-Detail-Detail ). When a user deletes a record in the master table by pressing the delete button in the navigation bar, the records the detail tables should also be deleted.
    Thanks.

    Try using the on delete cascade with your foreign key constraint
    and drop the trigger. Mutating table errors are normally caused
    by using row level triggers on constraining tables which change
    as a result of an insert, update, or delete of the master table.
    These triggers can not issue dml against the table that is
    "mutating" (changing), which is the primary table or child table
    if constraint is created using cascade. This is a general rule.
    See specific rules in oracle documentation if you are serious
    about using triggers.
    Tom Niderost (guest) wrote:
    : How do I perform a cascade delete in Oracle? I tried using
    : "Delete on Row" triggers to delete child data, but when the
    child
    : table had a "Delete on Row" trigger, I got a "Mutating Table"
    : error. Help!
    null

Maybe you are looking for