How do you perform soft delete with @EmbeddedId in Hibernate Java?

2019-06-18 hibernate jpa spring-data-jpa

I cannot perform a soft delete with an @EmbeddedId? The sql within the @SQLDelete is not being executed.

I have tried to hardcode value of the two id's to make up the embedded id.

I can get soft delete implementation work on entities with just @Id and not @EmbeddedId

When I run test with delete for entities with just @Id, I can see the sql being executed on the console but not with @EmbeddedId.

I think it just bypasses the @SQLDelete and uses the default delete sql from hibernate. I'm expecting the sql within the @SQLDelete being executed.

@EqualsAndHashCode(exclude = "serviceEmployeeIdentity", callSuper = true)
@Table(name = "service_employee")
@SQLDelete(sql = "UPDATE service_employee " +
    "SET deleted = true " +
    "WHERE service_employee.service_id = ?1 " +
    "AND service_employee.employee_b_number = ?2")
@Where(clause = "deleted = false")
public class ServiceEmployee extends Auditable implements Serializable {

    private ServiceEmployeeIdentity serviceEmployeeIdentity;

    @Column(name = "critical")
    private Boolean critical;

    @Column(name = "critical_rationale")
    private String criticalRationale;

public interface ServiceEmployeeRepository extends 
JpaRepository<ServiceEmployee, ServiceEmployeeIdentity> {}

// test
ServiceEmployee serviceEmployee = ServiceEmployeeBuilder.aServiceEmployee();

Note: serviceEmployee has embeddedId set serviceEmployeeService just calls the repository class


This would work just like it does if it were a single valued @Id annotated column. You simply need to specify multiple columns in the order of how they're represented in the embedded class, so use this in your @SQLDelete:

UPDATE service_employee
   SET deleted = true
 WHERE service_id = ?
   AND employee_b_number = ?

I tried and worked for me but the order is alphabetically of the columns of embeddedId

@SQLDelete(sql = "UPDATE (table_name) SET DELETED=true WHERE event_id = ? and payment_id=?")

If i use

@SQLDelete(sql = "UPDATE (table_name) SET DELETED=true WHERE payment_id = ? and event_id=?")

it will not work and not update the register needed.