I have a use case that involves composite ids to join multiple tables. I would like to use the @Where
annotation to exclude [soft] deleted entities. However, Hibernate is not adding it to the joins.
This is my entity spec:
@Entity
@Data
@Table(name = "_Who")
public class Who {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "who")
Set<WhoWhatWhere> storage;
}
@Entity
@Data
@Table(name = "_What")
public class What {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String thing;
}
@Entity
@Data
@Table(name = "_Where")
public class Where {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String place;
private Boolean deleted
}
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Entity
@NoArgsConstructor
@Table(name = "_WhoWhatWhere")
public class WhoWhatWhere {
public WhoWhatWhere(Who who, What what, Where where) {
this.who = who;
this.what = what;
this.where = where;
this.setId(new WhoWhatWhereId(who.getId(), what.getId(), where.getId()));
}
@EmbeddedId
WhoWhatWhereId id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "who_id", insertable = false, updatable = false)
private Who who;
@ManyToOne
@JoinColumn(name = "what_id", insertable = false, updatable = false)
private What what;
@ManyToOne
@JoinColumn(name = "where_id", insertable = false, updatable = false)
@Where(clause = "deleted = false")
private Where where;
}
@Embeddable
@NoArgsConstructor
public class WhoWhatWhereId implements Serializable {
public WhoWhatWhereId(Long whoId, Long whatId, Long whereId) {
this.whoId = whoId;
this.whatId = whatId;
this.whereId = whereId;
}
@Column(name = "who_id")
Long whoId;
@Column(name = "what_id")
Long whatId;
@Column(name = "where_id")
Long whereId;
}
I’d expect the @Where
to exclude any What
that has deleted=true
, however, when I select for all the Who
s, it returns all the linked objects regardless of that property value. Adding the annotation to the What
class definition does not work either because the entities are pulled in with a join, not queried directly. Also, the @WhereJoinTable
is not applicable here, because I’m not trying to filter on a column (property) found on the join table.
How can I achieve this functionality?