I have Two Entity User and Account. Both have one to one mapping. Here is the Entity Classes : User:
@Entity
@Table(name = 'docutools_users')
public class DocutoolsUser {
@Id
@Type(type = "pg-uuid")
UUID id = UUID.randomUUID();
@OneToOne(mappedBy = "user", cascade = ALL)
Account 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
@Query("SELECT u FROM DocutoolsUser u WHERE u.account IS NOT NULL")
Page<DocutoolsUser> findByAccountNotNull()
I am using JPA repositery. The expression u.account IS NOT NULL always return true even there is no account in user.
Thanks
Exact Query is here
@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)