문제

I want to create a query that query only rows that have an empty list.

The list in my model :

public static final Finder<Long, BankDebit> find = new Finder<>(Long.class, BankDebit.class);

@ManyToMany(cascade = CascadeType.ALL)
public List<Mandate> mandates;

The function that do the query :

public static ExpressionList<BankDebit> findFilter(sepaId, mandat, day ....) {
        ExpressionList<BankDebit> exp = find
                .fetch("creditor")
                .fetch("debitor")
                .fetch("mandates")
                .where()
                .eq("sepa.id", sepaId);

        if (day > 0) {
            dateMax = dateMax.withDayOfMonth(day);
            exp.eq("executionDate", dateMax.toDate());
        }    

        if (!mandat.isEmpty())
            exp.eq("mandates.id", 0); // here is the problem
    return exp
}

I want to query only the BankDebit that have an empty list of mandates. I tried to do it with .isNull("mandates"), .isNull("mandates.id"), .lt("mandates.id", 1), .eq("mandates.id", null) and a lot more, nothing ever worked...

I don't understund how I'm supposed to do. Do a rawSql would be very painful (I didnt paste the whole code of the function)

I tried a lot of things and reached many 4th page on google (never a good sign). I just ran out of ideas.

도움이 되었습니까?

해결책

Huh, you were faster actually I wanted to suggest you similar solution, probably lighter as doesn't require object mapping:

List<Integer> idsWithoutMandates = new ArrayList<>();
        List<SqlRow> rowList = Ebean.createSqlQuery("SELECT debits.id id " +
                "FROM bank_debit AS debits " +
                "LEFT JOIN bank_debit_mandate AS jointable ON (debits.id = jointable.bank_debit_id) " +
                "WHERE (jointable.mandate_id IS NULL OR jointable.mandate_id = 0)").findList();

for (SqlRow sqlRow : rowList) idsWithoutMandates.add(sqlRow.getInteger("id"));

List<BankDebit> debitsWithoutMandates = BankDebit.find.where().in("id", idsWithoutMandates).findList();

다른 팁

I found out that although .isNull() doesn't work, .isNotNull() did work. So I made a little ugly modification to use the existing ones to find the others...

if (!mandat.isEmpty()) {
    List<BankDebit> tmp = find.fetch("mandates").where().eq("sepa.id", sepaId).isNotNull("mandates.id").findList();
    List<Long> ids = Lists.newArrayList();
    for (BankDebit bd : tmp) {
        ids.add(bd.id);
    }
    exp.not(Expr.in("id", ids));
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top