我不是 Spring、JPA、Hibernate 或 MySql 方面的专家。 不过,我将所有这些都用于支持 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 注释,而我已将 where 注释与 OneToMany 一起使用
@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 的日志记录 触发选择查询时没有 where 子句
最佳答案
@Where 注释对 ToOne 关系没有影响。但是,您可以在实体上使用 @Where,而不是将 @Where 添加到引用中:
@Where(clause="isdeleted = 0")
@Entity
@Table(name = "storerole")
public class StoreRoleModel {
这样 Hibernate 就不会加载 StoreRoleModel 中已删除的实体。
关于mysql - 我可以将 @Where 注释与 @ManytoOne 关联一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56770986/