如何在spring data jpa中连接2个表时自动添加where子句



上下文:有两个1:1关系的表,想要在表连接上添加where子句,尝试通过添加@WhereJoinTable(clause = "addressType = 'STORE_ADDRESS'")来实现这一点注释,但在我的情况下不起作用

@Entity
@Table(name = "A")
public class A extends BaseEntity {
@Column(name = "name")
@Size(max = 500)
private String name;
@Column(name = "created_by", updatable = false)
private String createdBy;
@Column(name = "updated_by")
private String updatedBy;
@OneToOne(mappedBy = "store", fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
@JsonManagedReference
@WhereJoinTable(clause = "addressType = 'STORE_ADDRESS'")
private Address address;
public Store() {
}
}

@Entity
@Table(name = "address")
public class Address extends BaseEntity {
@OneToOne
@JoinColumn(name = "id")
@JsonBackReference
private A store;
@Column(name = "address")
private String address;
@Column(name = "address_type")
@Enumerated(EnumType.STRING)
private StoreAddressType addressType;
public StoreAddress() {
}
}

使用关联表时使用@WhereJoinTable注释。对于1:1关系,不需要关联表。

@Where注释应用于目标实体。

这就是为什么你应该尝试使用@Where注释而不是@WhereJoinTable注释。你可以这样做:

import org.hibernate.annotations.Where;
@Entity
@Table(name = "A")
public class A extends BaseEntity {
@Column(name = "name")
@Size(max = 500)
private String name;
@Column(name = "created_by", updatable = false)
private String createdBy;
@Column(name = "updated_by")
private String updatedBy;
@OneToOne(mappedBy = "store", fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
@JsonManagedReference
@Where(clause = "addressType = 'STORE_ADDRESS'")
private Address address;
public Store() {
}
}

相关内容

  • 没有找到相关文章