0

entityManager.remove() VS JPQL DELETE Query

I know that one side of relation is owner and the other side, marked with mapped by, is non-owner.

Relation is saved in Database with persistence of owner and removed with removal of it. I know that removal of non-owner side will not erase relation. see this link

So far so good.

In my experience, removal on non-owner with JPQL query, removes relation too. It's not clear for me why!? Is there any persuasive explanation?

Code snippets below show my test cases with concept:

Non-Owner

@Entity
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String title;

    @ManyToMany(mappedBy = "books")
    private Set<Author> authors;
}

Owner:

@Entity
public class Author {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToMany
    private Set<Book> books;
}

Test 1

public void persistingRelationOwnerPersistsRelationSuccessfully(){
    Book book = new Book("nonOwner");
    entityManager.persist(book);

    Author author = new Author("owner", new HashSet<>(Arrays.asList(book)));
    entityManager.persist(author);
    entityManager.flush();
}

Log:

Hibernate: insert into book (title, id) values (?, ?)
Hibernate: insert into author (name, id) values (?, ?)
Hibernate: insert into author_books (authors_id, books_id) values (?, ?)

Test 2

public void removingRelationOwnerRemovesRelationSuccessfully(){
    //persist book and author

    entityManager.remove(author);
    entityManager.flush();
}

Log:

Hibernate: insert into book (title, id) values (?, ?)
Hibernate: insert into author (name, id) values (?, ?)
Hibernate: insert into author_books (authors_id, books_id) values (?, ?)
Hibernate: delete from author_books where authors_id=?
Hibernate: delete from author where id=?

Test 3

public void removingNonOwnerWillThrowException(){
    //persist book and author

    entityManager.remove(book);
    entityManager.flush();
}

Log: (As Expected)

Hibernate: insert into book (title, id) values (?, ?)
Hibernate: insert into author (name, id) values (?, ?)
Hibernate: insert into author_books (authors_id, books_id) values (?, ?)
Hibernate: delete from book where id=?
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement

Test 4

public void removingNonOwnerWithQueryWillRemoveRelation(){
    //persist book and author

    Query query = entityManager.createQuery("delete from Book b where b.title = 'nonOwner'");
    System.out.println("affectedRows = " + query.executeUpdate());
}

Log: (Unexpected behavior)

Hibernate: insert into book (title, id) values (?, ?)
Hibernate: insert into author (name, id) values (?, ?)
Hibernate: insert into author_books (authors_id, books_id) values (?, ?)
Hibernate: delete from author_books where (books_id) in (select id from book where title='nonOwner')
Hibernate: delete from book where title='nonOwner'
affectedRows = 1

1 Answers1

1

This might be a bug because this should depend on the cascading setting of the Book#authors association which might be ignored for a delete DML statement. You should log an issue with a reproducer https://hibernate.atlassian.net/

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58