如何在使用冬眠标准的内部选择中使用主要标准值?

时间:2018-07-20 09:26:12

标签: java sql hibernate kotlin

我有这种模型:

@Entity
public class Client {

    @Column(name = "first_name", nullable = true, length = 100)
    private String firstName = "";

    @Column(name = "last_name", nullable = true, length = 100)
    private String lastName = "";

    @NotAudited
    @OneToMany(mappedBy = "client", fetch = FetchType.LAZY)
    @OrderBy("id desc")
    private final List<Loan> loans;

    /* getters and setters for these fields */

}

@Entity
public class Loan {

    @ManyToOne(optional = false)
    @JoinColumn(name = "client_id")
    private Client client;

    @Column(name = "close_date")
    private LocalDate closedDate;

    /* getters and setters for these fields */

}

@Entity
public class LoanApplication {

    @ManyToOne(optional = false)
    @JoinColumn(name = "client_id")
    private Client client;

    @Column(name = "confirmation_date")
    private LocalDateTime confirmationDate;

    /* getters and setters for these fields */

}

我需要创建查询以过滤这些实体。试图使用休眠条件来实现它,但无法在分离的条件中使用主要条件字段。所以我想生成这样的东西:

SELECT client
FROM Client client
WHERE
    client.name = :name AND
    exists(
            SELECT loanApplication.id
            FROM LoanApplication loanApplication
            WHERE loanApplication.client = client AND loanApplication.confirmationDate BETWEEN :startDate AND :endDate
    ) AND NOT
    exists(
            SELECT loanApplication.id
            FROM LoanApplication loanApplication
            WHERE loanApplication.client = client AND loanApplication.confirmationDate > :endDate
    ) AND
    exists(
            SELECT loan.id
            FROM Loan loan
            WHERE loan.client = client
    ) AND NOT
    exists(
            SELECT loan.id
            FROM Loan loan
            WHERE loan.client = client AND loan.closedDate > :closeDate
    )
ORDER BY client.id DESC

我无法使用休眠条件重现这种查询。可能吗

最近能够到达的位置是这样的:

val subCriteria1 = DetachedCriteria.forClass(LoanApplication::class.java, "loanApplication")
    .createAlias("loanApplication.client", "client")
    .setFetchMode("client.id", FetchMode.JOIN)
    .add(Restrictions.between("confirmationDate", LocalDateTime.parse("2017-01-01"), onDateTime))
    .setProjection(Projections.id())

val subCriteria2 = DetachedCriteria.forClass(LoanApplication::class.java, "loanApplication")
    .createAlias("loanApplication.client", "client")
    .setFetchMode("client.id", FetchMode.JOIN)
    .add(Restrictions.gt("confirmationDate", onDateTime.minusWeeks(closePeriodInWeeks)))
    .setProjection(Projections.id())

val subCriteria3 = DetachedCriteria.forClass(Client::class.java, "client")
    .createAlias("client.loans", "loan")
    .setProjection(Projections.id())

val subCriteria4 = DetachedCriteria.forClass(Client::class.java, "client")
    .createAlias("client.loans", "loan")
    .add(Restrictions.gt("loan.closedDate", onDateTime))
    .setProjection(Projections.id())

val criteria = criteria()
    .setFirstResult(startRow).setMaxResults(batchSize)
    .addOrder(Order.desc("id"))
    .add(Subqueries.exists(subCriteria1))
    .add(Subqueries.notExists(subCriteria2))
    .add(Subqueries.exists(subCriteria3))
    .add(Subqueries.notExists(subCriteria4))
    .add(Restrictions.eq("name", "John"))

0 个答案:

没有答案