简体   繁体   English

当关系为 null 时,JPA 规范不起作用

[英]JPA specification doesn't work when relationship is null

I'm implementing a search based on two entities.我正在实现基于两个实体的搜索。

@Entity(name = "user")
public class UserEntity {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(updatable = false, nullable = false)
    private String id;
    @Column(unique = true, nullable = false)
    private String email;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "foo_id", referencedColumnName = "id")
    private FooEntity foo;
    private UserType type;
}

@Entity(name = "foo")
public class FooEntity {
    @Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    @Column(updatable = false, nullable = false)
    private String id;
    private String identifier;
}

Now I want to look for users based on email adres OR when the FooEntity relation exist based on the identifier in that relationship.现在我想根据 email adres 或者当 FooEntity 关系基于该关系中的标识符存在时查找用户。 AND based on the enum type in the UserEntity. AND 基于 UserEntity 中的枚举类型。

I tried with following query:我尝试了以下查询:

public static Specification<UserEntity> emailLike(String text) {
     return (root, query, criteriaBuilder) -> criteriaBuilder.like(root.get("email"), "%" + text + "%");
    }
    
public static Specification<UserEntity> patientIdentifier(String text) {
      return (root, query, criteriaBuilder) -> criteriaBuilder.like(root.join("foo").get("identifier"), "%" + text + "%");
    }
public static Specification<UserEntity> userType(UserType type) {
            return (root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("type"), type);
        }
    
final Specification<UserEntity> specification = Specification.where(
                UserSpecification.patientIdentifier(text)
                        .or(UserSpecification.emailLike(text))
        ).and(
                UserSpecification.userType(type)
        );
    
final Page<UserEntity> userEntities = userRepository.findAll(specification, pageable);

Now this query seems to work when the relationship with the FooEntity exists.现在,当与 FooEntity 的关系存在时,此查询似乎有效。 When this relationship is null nothing is returned even when the email and type matches.当此关系为 null 时,即使 email 和类型匹配,也不会返回任何内容。

Can someone help me out?有人可以帮我吗?

You shouldn't just join but left join or right join.你不应该只是加入,而是左加入或右加入。 A join is an inner join which means it only matches records which match the join criteria.联接是内部联接,这意味着它只匹配符合联接条件的记录。 Anything equals null in SQL is always false, so I'd either of the join sites is null, the record isn't in the result set.任何等于 SQL 中的 null 的东西总是假的,所以我希望任何一个连接站点都是 null,记录不在结果集中。 So a left join says, respond with all of the left site entities and match the right site only if present, if not right site is null.因此,左连接表示,响应所有左站点实体并仅在存在时匹配右站点,如果不存在,则右站点为 null。

So join should look like root.join("foo", JoinType.LEFT) (not tested)所以 join 应该看起来像root.join("foo", JoinType.LEFT) (未测试)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM