【发布时间】:2016-12-17 03:00:21
【问题描述】:
我想知道是否存在一种方法来告诉 Hibernate 仅在满足条件时才在 @OneToMany 后面执行查询。 这可能是一个非常简单的例子:
@Entity
public class MyEntity {
...
private Long id;
private boolean condition;
private List<AnotherEntity> anotherEntity;
@Id
@Column(name = "ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Basic
@Column(name = "CONDITION")
public Boolean getCondition() {
return isContentChanged;
}
public void setIsContentChanged(Boolean condition) {
this.condition = condition;
}
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "SECOND_ID")
**@Conditional(clause = "CONDITION = true")**
public List<AnotherEntity> getAnotherEntity() {
return anotherEntity;
}
public void setAnotherEntity(List<AnotherEntity> anotherEntity) {
this.anotherEntity = anotherEntity;
}
}
@Entity
public class AnotherEntity {
private id
private secondId
...
}
所以我想要的是: - 如果@Conditional注解中的条件满足,Hibernate执行@OneToMany注解相关的查询 - 如果@Conditional注解中的条件不满足,Hibernate对@OneToMany注解什么都不做
我不知道它是否已经存在于 Hibernate 的某个地方。否则可能是一个很棒的新功能。
【问题讨论】:
标签: java hibernate annotations conditional-statements one-to-many