我想检查Woman类中是否存在字段。该字段是其自己的(丈夫)类,“女人”和“丈夫”都是存储在数据库中的实体。未在Woman类的构造函数中初始化Husband类。在程序的稍后时间,在我操纵了Woman类的某些实例之后,我想检查Woman类的哪些条目仍然没有Husband类实例(并进行初始化)。我需要使用JpaQueries API做到这一点。基本上,我想编写规范“ WithNoHusband”来浏览数据库。
到目前为止,我已经尝试过
(root,query,cb) -> {
Join<Woman, Husband> husbandJoin = root.join(husband);
return cb.equal(husbandJoin.isNull(), true);
};
在我的JpaWomanSpecificationAdapter类和
@Value
class WithNoHusband implements WomanSpecification {
@Override
public boolean isSatisfiedBy(Woman object) {
return object.getHusband() == null;
}
}
在我的WomanSpecification类别中没有运气。
当我想检查女人是否有汽车时,我所要做的就是
private org.springframework.data.jpa.domain.Specification<Woman> toJpaSpecification(WithNoCar specification) {
(root,query,cb) -> cb.isEmpty(root.get(car));
}
和
@Value
class WithNoCar implements WomanSpecification {
@Override
public boolean isSatisfiedBy(Woman object) {
return object.getCar().isEmpty();
}
}
在各个类别中。
这是三个类的基本结构
public class Woman{
@Id
Integer id;
@Column
String name;
@OneToOne(mappedBy = "maintenanceImplementation", cascade = ALL, orphanRemoval = true)
@Getter
Husband husband;
@oneToMany(mappedBy = "maintenanceImplementation", cascade = ALL, orphanRemoval = true)
@Getter
List<Car> car;
@Column
@Enumerated(STRING)
@Getter
Type typeOfWoman;
public Woman(Integer id, String name){
this.is=id;
this.name=name;
this.car = new ArrayList<>();
}
}
public class Husband {
@Id
Integer Id;
@OneToOne
@JoinColumn(name = "WomanId", nullable = false)
@Getter
@Setter(PACKAGE)
private Woman Woman;
@Column
String name;
}
public class Car {
@Id
Integer Id;
@ManyToOne
@JoinColumn(name = "WomanId", nullable = false)
@Getter
@Setter(PACKAGE)
private Woman Woman;
@Column
String brand;
}
我希望对丈夫来说,规范 withNoHusband 将对isNull条件返回true。
当然,在Woman的构造函数中,汽车已被初始化为一个空列表,因此可以简单地查询该列表是否为空(无需拼写 join子句),但是在联接之后子句适用于丈夫,查询选择所有没有汽车且没有丈夫的妇女的结果仍然是
unexpected AST node: is near line 1, column 485 [select generatedAlias0
from Woman as generatedAlias0, Woman as generatedAlias1 inner join
generatedAlias1.husband as generatedAlias2 where ( (
generatedAlias0.status=:param0 ) and ( generatedAlias0.cars is empty ) ) and
( generatedAlias2 is null=:param1 )]
?
PS:我不需要在WOMAN的构造函数中初始化丈夫,因为(到目前为止)丈夫的存在是基于typeOfWoman的,因此,如果typeOfWoman是“寡妇”,则不需要初始化丈夫。
答案 0 :(得分:0)
FEI,我犯的错误是创建一个INNER JOIN子句(在Criteria API中默认为)(root,query,cb)-> {Join maleJoin = root.join(husband,left);返回cb.equal(husbandJoin.isNull(),true); };做到了...