Re: Generate "on delete cascade" in DDL?

104 views
Skip to first unread message

James Roper

unread,
Nov 12, 2012, 7:00:15 PM11/12/12
to eb...@googlegroups.com
Well, ebean should do the cascading deletes for you manually, that's why it doesn't generate the ddl.  But, the ddl generation is just a temporary thing for helping you get started quickly in a project.  Once it no longer satisfies your needs, you should maintain it manually, at that point Play won't generate it for you anymore.

On Tuesday, 13 November 2012 05:19:51 UTC+11, Stefan wrote:
Hello all,

I am modelling the following:
One NewsArticle has multiple Comments, each Comment belongs to exactly one NewsArticle.
In SQL formulation: There is a foreign key from the Comment table to the primary key in the NewsArticle table.

I successfully modelled this using Ebean (please see code below), but I am yet missing one thing: When a NewsArticle is deleted, all associated Comments shall be deleted. In SQL this would be "on delete cascade". How do I get Ebean to generate "on delete cascade" in DDL? (jdbc mysql). Currently Ebean generates "on delete restrict" instead of the intended "on delete cascade".

My code as of now is:

NewsArticle:
@Entity
public class NewsArticle extends Model {
   
    @Id   
    public Integer news_id;
   
    @Column(nullable=false)
    public String title;
   
    @Column(nullable=false)
    public String text;
   
    @OneToMany(cascade=CascadeType.REMOVE)
    @Column(nullable=false)
    public List<Comment> comments;   
}

 Comment:
@Entity
public class Comment extends Model {
   
    @OneToOne(cascade=CascadeType.REMOVE)
    @Column(nullable=false)
    public NewsArticle news;
   
    @Id
    public Integer comment_id;
   
    @Column(nullable=false)
    public String text;
}
This code generates the tables and the foreign key constraint for the Comments without any problem, but it generates "on delete restrict" instead of "on update cascade"? ): How to tell Ebean to generate "on delete cascade"?

Thanks for any hint :-)

James Roper

unread,
Nov 12, 2012, 7:01:11 PM11/12/12
to eb...@googlegroups.com
Sorry, I was in the wrong forum, thought this was the Play forum :(

I need to find that on switch for my brain...

Stefan

unread,
Nov 13, 2012, 2:40:22 PM11/13/12
to eb...@googlegroups.com
I see nothing wrong on your post ;-)
And thanks for your information, still have to test this, but sounds good :-)
Reply all
Reply to author
Forward
0 new messages