Looking for an HQL builder (Hibernate Query Language)

后端 未结 11 907
-上瘾入骨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:16

    It looks like you want to use the Criteria query API built into Hibernate. To do your above query it would look like this:

    List stocks = session.createCriteria(Stock.class)
        .add(Property.forName("id").eq(id))
        .list();
    

    If you don't have access to the Hibernate Session yet, you can used 'DetachedCriteria' like so:

    DetachedCriteria criteria = DetachedCriteria.forClass(Stock.class) 
        .add(Property.forName("id").eq(id));
    

    If you wanted to get all Stock that have a Bonus with a specific ID you could do the following:

    DetachedCriteria criteria = DetachedCriteria.forClass(Stock.class)
         .createCriteria("Stock")
              .add(Property.forName("id").eq(id)));
    

    For more infromation check out Criteria Queries from the Hibernate docs

提交回复
热议问题