Search code examples
querydsl

How to delete with QueryDSL when there is no Q-class?


I have the following:

@Entity
public class Step {
...
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "InfoType")
@MapKeyEnumerated(EnumType.STRING)
@CollectionTable(name = "Info", joinColumns = @JoinColumn(name = "StepId"))
@Column(name = "Value", length = 100)
private Map<InfoType, String> infos = new HashMap<>();
...
}

The InfoType class is just an enum.

In the DB that translates to a table, Step and another table Info. The Info table has a foreign key to Step.

What I want to do is to delete Step records via a query. First I would need to delete from the Info table because of the FK, but there is no generated Q-class for Info. Is there a way to write such queries with QueryDSL ?


Solution

  • The @ElementCollection annotation operations are always cascaded. When you remove Step entity, the operation is cascaded to the @ElementCollection.

    However, cascading operations is only effective when the operation is done via EntityManager. If you are trying to bulk delete the entities via custom query the operation will not be cascaded.

    Possible solution would be to fetch all Step entities you want to delete and call the delete method for each of them.