Looking for an HQL builder (Hibernate Query Language)

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

    @Sébastien Rocca-Serra

    select stock
    from com.something.Stock as stock, com.something.Bonus as bonus
    where stock.bonus.id = bonus.id
    

    That's just a join. Hibernate does it automatically, if and only if you've got the mapping between Stock and Bonus setup and if bonus is a property of Stock. Criteria.list() will return Stock objects and you just call stock.getBonus().

    Note, if you want to do anything like

    select stock
    from com.something.Stock as stock
    where stock.bonus.value > 1000000
    

    You need to use Criteria.createAlias(). It'd be something like

    session.createCriteria(Stock.class).createAlias("bonus", "b")
       .add(Restrictions.gt("b.value", 1000000)).list()
    

提交回复
热议问题