JPA - Criteria API and EmbeddedId

前端 未结 3 1848
陌清茗
陌清茗 2020-12-05 13:24

I want to use criteria to make the following query. I have an Entity with EmbeddedId defined:

 @Entity
 @Table(name=\"TB_IN         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 14:02

    Try to copy and paste the metamodel classes into the same folder where your entities are saved (in NetBeans 8.2 they are automatically generated and have the same name of your entity but with an underscore at the end. Should be something like Interfase_ and InterfaseId_).

    Force the import of the metamodel classes Interfase_ and InterfaseId_ and refer to the desired field.

    CriteriaBuilder builder = em.getCriteriaBuilder();
    
    CriteriaQuery criteria = builder.createQuery(Interfase.class);
    Root interfaseRoot = criteria.from(Interfase.class);
    criteria.select(interfaseRoot);
    criteria.where(builder.equal(interfaseRoot.get(Interfase_.id).get(InterfaseId_.clase),"Referencia 111"));
    List interfases = em.createQuery(criteria).getResultList();
    

提交回复
热议问题