在spring-data-jpa项目中,我有两个实体处于一对多关系。
父实体-
@Entity
@Table(name = "code_group")
public class CodeGroup implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@NaturalId
@Column(nullable = false)
private String entityId;
@OneToMany
@JoinColumn(name = "codeGroupId", referencedColumnName = "entityId")
private List<SystemCode> systemCodes;
// .. getters setters
}
子实体-
@Entity
@Table(name = "system_code")
public class SystemCode implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@Column(nullable = false)
private String codeGroupId;
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private ActiveOrInactive status;
// getters and setters
}
status
列为枚举类型,它只能具有Active
或Inactive
值。
我现有的代码工作正常。它正在获取具有相关系统代码的代码组。我想用status='Active'
过滤系统代码。该怎么做?
答案 0 :(得分:1)
尝试这样:
@OneToMany
@JoinColumn(name = "codeGroupId", referencedColumnName = "entityId")
@Where(clause = "status= 'Active'")
private List<SystemCode> systemCodes;
答案 1 :(得分:1)
您可以在您的仓库中创建此类方法:
List<CodeGroup> getAllBySystemCodes_Status(ActiveOrInactive status);
default List<CodeGroup> getAllActive() {
return getAllBySystemCodes_Status(ActiveOrInactive.Active);
}
default List<CodeGroup> getAllInactive() {
return getAllBySystemCodes_Status(ActiveOrInactive.Inactive);
}