I'm using Demoiselle in my application, but it does not come out very well in one respect. In an association between two or more entities, deleting throws an exception. With the exception handling available in Demoiselle, I've created a method to catch this exception, but we're not getting it.
Follow the example code.
@Entity
public class Bookmark implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String description;
@Column
private String link;
And the Client class:
@Entity
public class Cliente implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String name;
@ManyToOne
@JoinColumn(name = "bookmark_id", referencedColumnName = "id", nullable = false)
private Bookmark bookmark;
Through this, we can see that the Client has a many-to-one association with Bookmark. So when registering a customer I should add a bookmark to it. The problem occurs when you delete a bookmar that has reference on a client. It returns me the following error.
Caused by: javax.persistence.RollbackException: Error while committing the transaction at org.hibernate.ejb.TransactionImpl.commit (TransactionImpl.java:92) at br.gov.frameworkdemoiselle.transaction.JPATransaction.commit (JPATransaction.java:121) ... 60 more Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement at org.hibernate.ejb.AbstractEntityManagerImpl.convert (AbstractEntityManagerImpl.java:1387) at org.hibernate.ejb.AbstractEntityManagerImpl.convert (AbstractEntityManagerImpl.java:1310) at org.hibernate.ejb.TransactionImpl.commit (TransactionImpl.java:80) ... 61 more Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Can not delete or update parent row: a foreign key constraint fails (% with%.% of%, CONSTRAINT
db_estacionamento
FOREIGN KEY (cliente
) REFERENCESFK_ht5q4yqv47muisi25vq14yw2u
(bookmark_id
))
To try to handle this error, in the BookmarkEditMB class I created a method to catch this exception. But specifically this case, you can not capture it.
@ViewController
@PreviousView("/bookmark_list.xhtml")
public class BookmarkEditMB extends AbstractEditPageBean<Bookmark, Long> {
@ExceptionHandler
public void tratador(ConstraintViolationException cause) {
messageContext.add("Estou tentando tratar a exceção aqui.", SeverityType.WARN);
}
}
I have tried to capture all the exception types that occur when deleting, but none of them are captured by the method. I've tried catching other types of exception and it's captured perfectly. In this specific case (deleting) I can not capture it.
On this Demoiselle manual page explain how to use @ExceptionHandler: link
Below is a link to download an example application:
Thanks in advance for your help.