IS NOT NULL始终返回true J​​PA查询

时间:2018-02-23 10:40:35

标签: java spring postgresql spring-data-jpa jpql

我有两个实体用户帐户。两者都有一对一的映射。 这是实体类: 用户

@Entity
@Table(name = 'docutools_users')
public class DocutoolsUser {
   @Id
   @Type(type = "pg-uuid")
   UUID id = UUID.randomUUID();

   @OneToOne(mappedBy = "user", cascade = ALL)
   Account account;}

帐户

@Entity
@Table(name = "accounts")
public class Account {

  @Id
  @Type(type = "pg-uuid")
  private UUID id = UUID.randomUUID();
  @Column(nullable = false)
  private LocalDate activated = LocalDate.now();
  @OneToOne
  private DocutoolsUser user;
}

查询

@Query("SELECT u FROM DocutoolsUser u WHERE u.account IS NOT NULL")
Page<DocutoolsUser> findByAccountNotNull()

我正在使用JPA存储库。 表达 u.account IS NOT NULL 始终返回 true ,即使用户中没有帐户。

由于

确切查询在这里

 @Query("""SELECT u FROM DocutoolsUser u WHERE u.organisation = :org AND UPPER(CONCAT(u.name.firstName, ' ', u.name.lastName)) LIKE :search AND ((CASE WHEN ( (u.id = u.organisation.owner.id AND u.account IS NULL) OR ( u.account IS NOT NULL AND (u.account.subscription.until IS NULL or u.account.subscription.until > :currentDate)) ) THEN true ELSE false END) IS :isLicensed )""")
Page<DocutoolsUser> findByOrganisationAndLicense(@Param('org') Organisation organisation, @Param('search') String search, @Param('currentDate') LocalDate currentDate, @Param('isLicensed') Boolean isLicensed, Pageable pageable)

1 个答案:

答案 0 :(得分:1)

你可以在没有@Query的情况下使用 JpaRepository IsNotNull

来做到这一点
@Repository
public interface DocutoolsUserRepository  extends JpaRepository<DocutoolsUser, Long> {

// where Account not null
 public List<DocutoolsUser> findByAccountIsNotNull();

}

了解更多信息:https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.html

对于整个查询,您可以组合许多jpaRepository服务