Reputation: 36689
I have some logically deleted records (ie active=false
) which are causing problems with my @ManyToOne
mapping since more than one result is being returned by the join column.
I need to only include records where active=true
which I thought I could achieve by:
@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "site_id", referencedColumnName = "site_id", insertable = false, updatable = false)
@WhereJoinTable(clause = "active=true")
private Site site;
However, it seems like the WhereJoinTable
is not being used by hibernate (perhaps its only valid for OneToMany
?) since the active=true
does not show up in the generated SQL (logs) and the problem persists.
Is it possible to include a where clause for the join of a ManyToOne
and how?
Upvotes: 8
Views: 10579
Reputation: 1785
The solution that worked for me is add @where annotation in the top of the class, see the next example:
@Where(clause = "state_.idCity=0")
@SuppressWarnings("serial")
@Entity
@Table(name="city", schema="catalog")
@SequenceGenerator(name = "default_gen", sequenceName = "IDSERIEINVALIDO", allocationSize = 1)
public class Citye implements Serializable{
@Id
private Long id;
}
Upvotes: 0
Reputation: 42114
@WhereJoinTable is not supported with @ManyToOne. There is bug HHH-4335 about subject open since five years. I am not aware about any workaround, except using view (in the case of read-only access) as mentioned in bug report.
Upvotes: 4