Hibernate JPA lazy OneToOne关系是在加载或查询实体时提取的,而实体不应';T为什么?
鉴于这两个实体具有一对一关系(a作为持有B实体id的拥有方): 我使用条件查询获取给定日期内的所有B实体:Hibernate JPA lazy OneToOne关系是在加载或查询实体时提取的,而实体不应';T为什么?,hibernate,jpa,spring-data,spring-data-jpa,jpa-criteria,Hibernate,Jpa,Spring Data,Spring Data Jpa,Jpa Criteria,鉴于这两个实体具有一对一关系(a作为持有B实体id的拥有方): 我使用条件查询获取给定日期内的所有B实体: public class BSpecification { public static Specification<BEntity> filter(BFilter filter) { return new Specification<BEntity>() { @Override public P
public class BSpecification {
public static Specification<BEntity> filter(BFilter filter) {
return new Specification<BEntity>() {
@Override
public Predicate toPredicate(Root<BEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<Predicate>();
if(filter.getDateFrom() != null){
predicates.add(cb.greaterThanOrEqualTo(cb.function("date", Date.class, root.get(BEntity_.someDate)),
filter.getDateFrom()));
}
if(filter.getDateTo() != null){
predicates.add(cb.lessThanOrEqualTo(cb.function("date", Date.class, root.get(BEntity_.someDate)),
filter.getDateTo()));
}
if(predicates.size()==0){
return null;
}else{
return cb.and(predicates.toArray(new Predicate[0]));
}
}
};
}
}
。。。还有另一种方法可以得到一对一的惰性关联实体:
select
a0_.id as id1_9_2_,
a0_.b_id as b14_9_2_,
from
a a0_
where
a0_.b_id=?
由于性能原因,我需要防止加载AENTITY,为什么这个条件会获取它们
更新:为了简化问题,我刚刚加载了一个benties
bRepository.findOne(bId)
,同样的情况也发生了。所以这不是关于标准,而是关于映射。有什么问题吗?这才是真正对我有用的,被接受的参考答案没有:
只需更新我的Bentity即可实现FieldHandler,仅此而已:
@Entity
@Table(name="B")
public class BEntity implements FieldHandled{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne(mappedBy = "b", cascade = CascadeType.ALL, optional = true,
fetch = FetchType.LAZY)
private AEntity a;
...
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
protected Date someDate;
public AEntity getA() {
if (fieldHandler != null) {
return (AEntity) fieldHandler.readObject(this, "a", a);
}
return notificacion;
}
public void setA(AEntity a) {
if (fieldHandler != null) {
this.a = (AEntity) fieldHandler.writeObject(this, "a", this.a,
a);
return;
}
this.a = a;
}
}
可能重复的
select
a0_.id as id1_9_2_,
a0_.b_id as b14_9_2_,
from
a a0_
where
a0_.b_id=?
@Entity
@Table(name="B")
public class BEntity implements FieldHandled{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne(mappedBy = "b", cascade = CascadeType.ALL, optional = true,
fetch = FetchType.LAZY)
private AEntity a;
...
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
protected Date someDate;
public AEntity getA() {
if (fieldHandler != null) {
return (AEntity) fieldHandler.readObject(this, "a", a);
}
return notificacion;
}
public void setA(AEntity a) {
if (fieldHandler != null) {
this.a = (AEntity) fieldHandler.writeObject(this, "a", this.a,
a);
return;
}
this.a = a;
}
}