我可以将@Where 注释与@ManytoOne 关联一起使用吗?

Can I use @Where annotation along with @ManytoOne association?

EER Diagram

我不是 Spring、JPA、Hibernate 或 MySql 方面的专家。 但是,我将 all 用于支持 RESTful 调用的 Web 服务。 我正在使用 Spring 构建商店管理应用程序后端。 此时我的实体是 StoreModel、StoreUserModel、StoreUserRoleModel 和 StoreUserAuthModel。

我已经设置了双向关系(OneToMany 和 ManyToOne) StoreModel - StoreUserAuthModel, StoreUserMode - StoreUserAuthModel 和 StoreUserRoleMode - StoreUserAuthModel。

我不想要外键约束,尽管 StoreUserAuthModel 中有外键字段 storeid、roleid 和 userid。

现在四张表都有isdeleted列,实现软删除。 我懒得获取关联。但是,每当我查询关联时,我都不想要软删除值。

我想知道是否可以在 StoreUserAuthModel 实体中使用 @Where 注释和 @ManyToOne 注释?

这个问题与 How to use @Where in Hibernate 不同,因为我的问题是 ManyToOne 注释,而我在 OneToMany

中使用了 where 注释
@Entity
@Table(name = "store")
public class StoreModel {

    @NotBlank
    private String name;

    @NotBlank
    private String address;

    @NotBlank
    private String city;

    @NotBlank
    private String phone;


    @JsonIgnore
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "storeid", foreignKey = @ForeignKey(name="none", value = ConstraintMode.NO_CONSTRAINT ))
    @Where(clause="isdeleted = 0")
    private List<StoreUserAuthModel> authList = new ArrayList<StoreUserAuthModel>();


    ...
}

@Entity
@Table(name = "storerole")
public class StoreRoleModel {

    @NotBlank
    private String name;

    @NotBlank
    private Integer rolehierarchy;

    @JsonIgnore
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "roleid", foreignKey = @ForeignKey(name="none", value = ConstraintMode.NO_CONSTRAINT ))
    @Where(clause="isdeleted = 0")
    private List<StoreUserAuthModel> authList = new ArrayList<StoreUserAuthModel>();

    ...


}

@Entity
@Table(name = "storeuser")
public class StoreUserModel{

    @NotBlank
    @Column(unique = true)
    private String username;

    @Email
    @Column(unique = true)
    private String useremail;

    @JsonIgnore
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "userid", foreignKey = @ForeignKey(name="none", value = ConstraintMode.NO_CONSTRAINT ))
    @Where(clause="isdeleted = 0")
    List<StoreUserAuthModel> userAuthList = new ArrayList<StoreUserAuthModel>();

    ...

}

@Entity
@Table(name = "storeuserauth", 
        uniqueConstraints = @UniqueConstraint(columnNames = {"storeid", "roleid", "userid"}))
public class StoreUserAuthModel {

    @NotNull
    Long storeid;

    @NotNull
    Long roleid;

    @NotNull
    Long userid;

    // Using @where to filter out the soft deleted storeuser
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="userid", foreignKey = @ForeignKey(name="none",  value = ConstraintMode.NO_CONSTRAINT ),insertable = false, updatable = false )
    @Where(clause="isdeleted = 0")
    private StoreUserModel storeuser;

    // Using @where to filter out the soft deleted store
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="storeid", foreignKey = @ForeignKey(name="none",  value = ConstraintMode.NO_CONSTRAINT ),insertable = false, updatable = false )
    @Where(clause="isdeleted = 0")
    private StoreModel store;

    // Using @where to filter out the soft deleted role
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="roleid", foreignKey = @ForeignKey(name="none",  value = ConstraintMode.NO_CONSTRAINT ),insertable = false, updatable = false )
    @Where(clause="isdeleted = 0")
    private StoreRoleModel role;

    ...


}


// In the controller, Following code shows how I plan to use

    Optional<StoreUserModel> aUser = storeUserRepository.findByUseremailAndIsdeleted(zUserMail), 0);
    if(aUser.isPresent()) {
        // The user was found!!!

        // Testing...
        // Getting the User Auth List (that will filter out the soft deleted auths)
        List<StoreUserAuthModel> authList = aUser.get().getUserAuthList();
        for(StoreUserAuthModel auth :authList) {
            StoreModel store = auth.getStore();
            // here both soft deleted store as well as normal stores are shown.
            // ie where clause  on  store relation is not working!!

            logger.debug("Store is "+store.getName());
        }


    }

...

现在所有与id匹配的商店行都在列表中。 预期结果也应适用 where 子句

我打开了 hibernate 5.3.9 的日志记录 触发 select 查询时没有 where 子句

@Where 注释对 ToOne 关系没有影响。但不是将 @Where 添加到引用中,而是可以在实体上使用 @Where:

@Where(clause="isdeleted = 0")
@Entity
@Table(name = "storerole")
public class StoreRoleModel {

这样,Hibernate 将不会加载 StoreRoleModel 的已删除实体。