1
votes

I trying to do the relationship between Category and Publication classes (OneToMany) and I need to delete all the Publications that belong to an specific Category. I don't know if I missing some annotations but this is my error when I execute the query:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute update query org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852) javax.servlet.http.HttpServlet.service(HttpServlet.java:618) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) javax.servlet.http.HttpServlet.service(HttpServlet.java:725) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

org.hibernate.exception.ConstraintViolationException: could not execute update query.

org.postgresql.util.PSQLException: ERROR: update or delete on table "category" violates foreign key constraint "fkf5bea17d42c4af41" on table "publication" Detail: Key (id)=(190) is still referenced from table "publication".

My source code is as follow:

Category class:

@Entity
@Table(name = "category")
public class Category {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String name;
    private String description;

    @OneToMany(mappedBy="category", orphanRemoval=true)
    private List<Publication> publications;

    // Getters and Setters
}

Publication class:

@Entity
@Table(name = "Publication")
public class Publication {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String title;
    private String content;

    @ManyToOne
    @JoinColumn(name = "id_category")
    private Category category;

    // Getters and Setters
}

Query to delete categories:

@Override
public void deleteCategory(int id) {
    sessionFactory.getCurrentSession()
     .createQuery("DELETE FROM Category WHERE id="+id).executeUpdate();
}

Thanks in advance people.

2
post the complete stacktrace of the exception. - Chaitanya
@Chaitanya, thanks for answer man, please check my question again. - wilson

2 Answers

0
votes

You need to cascade the delete operation to the publications:

@OneToMany(mappedBy="category", orphanRemoval=true, cascade = CascadeType.ALL)
private List<Publication> publications;

Edit:

Since adding the cascade did not solve your problem and you are using Hibernate, it might help that you add the Hibernate-specific annotation for cascading. In this case, you won't need the JPA parameter from above:

@OneToMany(mappedBy="category", orphanRemoval=true)
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.ALL)
private List<Publication> publications;

This breaks the portability to a different object-relational-mapping framework, because it is no longer pure JPA - but it had helped me in some cases in the past.

0
votes

Even if you put the cascade, it will not work. I see from your question that you would like to delete all the Publications which are of a certain category then your query should change to the following. (you were trying to delete Categories earlier)

delete from Publication p where p.category.id = :categoryId;

then if you would like to delete the Categories also. (Now you are safe to delete the categories because there are no references in the Publication table.

delete from Category c where c.id = :categoryId;