eer图
我不是春季,JPA,Hibernate或MySQL的专家。但是,我将全部用于支持RESTFUL呼叫的Web服务。我正在使用Spring建造商店管理应用程序后端。此时我的实体是Storemodel,StoreUserModel,StoreUserleleModel和StoreSerauthModel。
我之间有建立双向关系(Onetomany and MoantoOne(Storemodel -StoreSerauthmodel,StoreUserMode- storeuserauthmodel和StoreUserrolemode -StoreSerauthmodel。
我不想要外键约束,尽管在storeuserauthmodel中有外键字段storeId,ralesId和userId。
现在,所有四个表都列为列列以实现软删除。我懒惰地提出关联。但是,每当我查询关联时,我都不想要软填充的值。
我想知道我是否可以使用 @where注释以及@manytoone的注释,
这个问题与Hibernate中的 @Where的使用方式不同,因为我的问题是在许多方面注释,而我已经使用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匹配的商店行都在列表中。预期的结果应适用于条款太
我打开登录以供冬眠5.3.9当它触发选择查询
@Where注释对人际关系没有影响。但是,您可以在实体上使用 @Where,而不是将 @位添加到参考:
@Where(clause="isdeleted = 0")
@Entity
@Table(name = "storerole")
public class StoreRoleModel {
这样,Hibernate将不会删除StoreroLemodel的实体。