简体   繁体   中英

How to add condition for List type in TypedQuery

I have entity with following attributes:

@Table(name = "MY_TABLE")
public class MyTable {
  ......
  @OneToMany(mappedBy = "mytable", cascade = ALL, orphanRemoval = true)
  private List<MyAnotherTable> otherTableValues = new ArrayList<MyAnotherTable>();
  ......
}

Now I am trying to write HSQL as

TypedQuery<Share> q =
            getEntityManager().createQuery("SELECT MyTable FROM MyTable AS mytable WHERE " +
                    "mytable.someField=:firstParam AND mytable.secondField IS NOT NULL AND " +
                   // "AND mytable.otherTableValues"
                   , Share.class);
    q.setParameter(firstParam, firstVal);
    return q.getResultList();

So not sure how to put consition on otherTableValues as it is list type. Any suggestion?

You have to join the collection:

SELECT DISTINCT mytable
FROM MyTable AS mytable join mytable.otherTableValues AS otv
WHERE mytable.someField = :firstParam
  AND mytable.secondField IS NOT NULL
  AND otv.someOtherField = :someOtherField

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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