Generate "on delete cascade" using Ebean
Asked Answered
E

3

7

I am using Ebean and I need to end up getting "on delete cascade" as DDL (data definition language) - which annotation does that? I tried

@OneToMany(cascade=CascadeType.REMOVE)

but that gives "on delete restrict" / doesn't change the default "on delete restrict"?

Ergocalciferol answered 12/11, 2012 at 16:12 Comment(0)
A
1

Try this

@OneToMany(mappedBy = "parent", cascade = javax.persistence.CascadeType.REMOVE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
Atthia answered 15/1, 2013 at 9:7 Comment(0)
S
0

You need to use the @OnDelete annotation on the parent class in order for Hibernate to generate it in the DDL (@OnDelete(action = OnDeleteAction.CASCADE))

For example:

@Entity
class Parent {
   /* id and some attributes */

  @OneToMany(mappedBy = "parent", cascade = {CascadeType.REMOVE})
  @ForeignKey(name = "FK_CHILD_PARENT")
  @OnDelete(action = OnDeleteAction.CASCADE)
  List<Child> children;
}

@Entity
class Child {
   /* id and some attributes */

   Parent parent;
}
Suction answered 15/1, 2013 at 9:13 Comment(0)
C
0

Although you have asked for "on delete cascade" as DDL, my answer covers software level. EBean does not seem to generate DB level cascades. Instead, it handles the cascading operation in software. I think that is a good alternative.

See this from(avaje-ebeanorm-4.6.2.jar:com/avaje/ebeaninternal/server/persist/DefaultPersister)

 /**
 * Delete the bean.
 * <p>
 * Note that preDelete fires before the deletion of children.
 * </p>
 */
private void delete(PersistRequestBean<?> request) {

    DeleteUnloadedForeignKeys unloadedForeignKeys = null;

    if (request.isPersistCascade()) {
        // delete children first ... register the
        // bean to handle bi-directional cascading
        request.registerDeleteBean();
        deleteAssocMany(request);
        request.unregisterDeleteBean();
    ...

You can see that if the request is a cascade, then first its children are deleted (probably this is somehow recursive).

In my postgre DB, the automatically generated foreign key constraints do not contain any cascades, but still the delete operations are cascaded.

Carbonari answered 4/3, 2016 at 22:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.