We're updating the issue view to help you get more done. 

DML batch delete not working if entity has @ElementCollection mapping

Description

If an entity has an @ElementCollection mapping, sql is never fired to delete from the mapping table and a foreign key constraint exception is raised when attempting to delete using HQL DML.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @Entity @Table(name="employee") public class Employee { @Id long id; String name; @ElementCollection @CollectionTable(name="employee_roles", joinColumns = @JoinColumn(name="employee_id")) @Column(name="role_id") private Set<EmployeeRole> roles; } public enum EmployeeRole { Role1, Role2, Role3, Role4 }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 create table employee (id integer not null auto_increment, name varchar(255), primary key (id)) engine=InnoDB; create table employee_roles (employee_id integer not null, role_id integer) engine=InnoDB; alter table employee_roles add constraint FKivyn2pkkba72p28ahjxv1t0ai foreign key (employee_id) references employee (id); insert into employee values (1, 'Bill'); insert into employee values (2, 'Joe'); insert into employee values (3, 'Bob'); insert into employee_roles values (1, 0); insert into employee_roles values (1, 1); insert into employee_roles values (2, 1); insert into employee_roles values (2, 2); insert into employee_roles values (3, 2); insert into employee_roles values (3, 3);
1 2 3 4 Query deleteQuery = em.createQuery("delete Employee e where e.id = :id"); deleteQuery.setParameter("id", 1L); deleteQuery.executeUpdate();

Output:

1 2 3 Hibernate: delete from employee where id=? 2019-01-18 14:59:02,891 WARN [SqlExceptionHelper] (main) SQL Error: 1451, SQLState: 23000 2019-01-18 14:59:02,891 ERROR [SqlExceptionHelper] (main) Cannot delete or update a parent row: a foreign key constraint fails (`proddb`.`employee_roles`, CONSTRAINT `FKivyn2pkkba72p28ahjxv1t0ai` FOREIGN KEY (`employee_id`) REFERENCES `employee` (`id`))

I was expecting to see:

1 2 delete from employee_roles where employee_id=? delete from employee where id=?

Environment

None

Status

Assignee

Unassigned

Reporter

Scott Werner

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.3.7
5.4.0

Priority

Major