Looking for an HQL builder (Hibernate Query Language)

后端 未结 11 909
-上瘾入骨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条回答
  •  -上瘾入骨i
    2021-02-04 17:16

    Now are also available the standard JPA Type Safe query and an less standard but also good Object Query

    Examples:

    JPA Type Safe

    EntityManager em = ...
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery c = qb.createQuery(Stock.class);
    Root = c.from(Stock.class);
    Predicate condition = qb.eq(p.get(Stock_.id), id);
    c.where(condition);
    TypedQuery q = em.createQuery(c); 
    List result = q.getResultList();
    

    Object Query

    EntityManager em = ...
    ObjectQuery query = new GenericObjectQuery(Stock.class);
    Stock toSearch = query.target();
    query.eq(toSearch.getId(),id);
    List res = (List)JPAObjectQuery.execute(query, em);
    

提交回复
热议问题