Mike
Mike

Reputation: 1097

How to use delete cascade?

I was using Matt Raible example for the Blog app http://gist.asciidoctor.org/?github-mraible/jhipster4-demo//README.adoc and when I tried to delete a blog without deleting first all the entries of that blog, it give me an Internal Server Error (DataIntegrityViolationException).

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["FK_ENTRY_BLOG_ID: PUBLIC.ENTRY FOREIGN KEY(BLOG_ID) REFERENCES PUBLIC.BLOG(ID) (2801)"; SQL statement:
delete from blog where id=? [23503-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:278)
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)

And I wonder if there is any way to obtain delete cascade when using the JDL-Import of JDLStudio.jh definition file and, if not possible, if anyone knows how to best accomplish it. As always, any example would be great!

Upvotes: 0

Views: 1217

Answers (1)

Jon Ruddell
Jon Ruddell

Reputation: 6362

This is not possible to declare in JDL. If you want to cascade delete, add that attribute to the relationship annotation. For example:

@OneToMany(mappedBy = "blog", cascade = CascadeType.REMOVE)
private Set<Article> articles = new HashSet<>();

Upvotes: 5

Related Questions