-->
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.  [ 1 post ] 
Author Message
 Post subject: Many-to-one owner to cascade on deletion of owned entity
PostPosted: Wed Sep 16, 2009 11:48 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi there,

my task is to delete an entity B that holds a many-to-one reference to an entity A as soon as the referenced entity A is deleted -- basically a cascade delete in the DB. The association is unidirectional and entity A does not "know" about entity B.

Java code:
Code:
public abstract class RootEntity {
  private Long id;
  ...
  public Long getId() { ... }
}

public class EntityA extends RootEntity {
  ...
}

public class Entity B extends RootEntity {
  protected EntityA;
  ...
  public EntityA getEntityA() { ... }
}


Hibernate mapping:
Code:
<hibernate-mapping ...>
<class name="RootEntity" abstract="true" ...>
    <id name="id" type="long">
      <generator class="native" />
    </id>
    ...

    <subclass name="EntityA" extends="RootEntity">
      <join table="entitya">
        <key column="id" foreign-key="FK_A_Root" />
        ...
      </join>
    </subclass>

  <subclass name="EntityB" extends="RootEntity">
      <join table="entityb">
        <key column="id" foreign-key="FK_B_Root" />
        ...
        <many-to-one name="entitya" column="entitya_id"
          class="EntityA" foreign-key="FK_B_A"
          not-null="true" update="false" />
      </join>
    </subclass>
    ...
</class>
</hibernate-mapping>

The relationship is not unique, there may be several instances of EntityB referencing the same EntityA.

I want to achieve that the foreign key FK_B_A is marked ON DELETE CASCADE. How do I do that?

BTW: It is not intended to make EntityB known to EntityA by adding a reference or a collection of references to it.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.