Looking for an HQL builder (Hibernate Query Language)

后端 未结 11 903
-上瘾入骨i
-上瘾入骨i 2021-02-04 16:45

I\'m looking for a builder for HQL in Java. I want to get rid of things like:

StringBuilder builder = new StringBuilder()
    .append(\"select stock from \")
            


        
11条回答
  •  礼貌的吻别
    2021-02-04 17:36

    @Sébastien Rocca-Serra
    Now we're getting somewhere concrete. The sort of join you're trying to do isn't really possible through the Criteria API, but a sub-query should accomplish the same thing. First you create a DetachedCriteria for the bonus table, then use the IN operator for someValue.

    DetachedCriteria bonuses = DetachedCriteria.forClass(Bonus.class);
    List stocks = session.createCriteria(Stock.class)
        .add(Property.forName("someValue").in(bonuses)).list();
    

    This is equivalent to

    select stock
    from com.something.Stock as stock
    where stock.someValue in (select bonus.id from com.something.Bonus as bonus)
    

    The only downside would be if you have references to different tables in someValue and your ID's are not unique across all tables. But your query would suffer from the same flaw.

提交回复
热议问题