Use JPA 2.1 EntityGraph to determine the fields that will be received in the request. Therefore, if you have a class MyClass and you want to receive separate fields dynamically, something like this might be enough
EntityGraph<MyClass> eg = em.createEntityGraph(MyClass.class); eg.addAttributeNodes("id"); eg.addAttributeNodes("name"); eg.addAttributeNodes("relation"); Query q = em.createQuery("SELECT b FROM MyClass b"); q.setHint("javax.persistence.fetchgraph", eg); List<MyClass> results = q.getResultList();
source share