Generic solution to Hibernate's CriteriaQuery I've created a little class which helps me to generalize many query cases. I have a DbService and couple methods in it. Can you please take a look at it and share some criticism? The usage of that tool is really easy to me and I do wanna know how to make it safer and optimized.
 public class DbServiceImpl implements DbService {
 private final EntityManager entityManager;

 @Inject
 DbServiceImpl(EntityManager entityManager) {
    this.entityManager = entityManager;
 }   

 @Override
 public &lt;T, V&gt; List&lt;V&gt; createQuery(Query&lt;T, V&gt; query) {
    try {
        if (query != null &amp;&amp; query.entityType.getAnnotation(Entity.class) != null) {
            CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

            CriteriaQuery&lt;V&gt; criteriaQuery = criteriaBuilder.createQuery(query.returnType);

            query.root = criteriaQuery.from(query.entityType);
            query.builder = criteriaBuilder;

            Selection&lt;? extends V&gt; selection = query.select(); //invokes on the call.
            Predicate predicate = query.where(); 

            if (predicate != null) {
                criteriaQuery.where(predicate); //null of a simple select without where clause.
            }

            criteriaQuery.select(selection); //null is allowed, it will just select * from the entity

            return entityManager.createQuery(criteriaQuery).getResultList();
        }
      } catch (Exception exception) {
        exception.printStackTrace();
      }
    return new ArrayList&lt;&gt;(); //a simple check of .isEmpty() covers that case.
   }

 @SuppressWarnings(&quot;unchecked&quot;) 
 public abstract static class Query&lt;T, V&gt; {
    private final Class&lt;T&gt; entityType;
    private final Class&lt;V&gt; returnType;

    //accessible only by outer class's methods.
    private Root&lt;T&gt; root; 
    private CriteriaBuilder builder; 

    public Query() {
        this.entityType=(Class&lt;T&gt;) ((ParameterizedType) getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0];
        this.returnType=(Class&lt;V&gt;) ((ParameterizedType) getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0];
    }

    protected abstract Selection&lt;? extends V&gt; select();

    protected abstract Predicate where();

    protected Root&lt;T&gt; root() {
        return root;
    }

    protected CriteriaBuilder builder() {
        return builder;
    }
  }
}

And here are some use cases. Passed types are as follows - entity &amp; return (User,Long)
  List&lt;Long&gt; data = dbService.createQuery(new DbServiceImpl.Query&lt;User, Long&gt;() {
        @Override
        protected Selection&lt;? extends Long&gt; select() {
            return builder().count(root());
        }
        @Override
        protected Predicate where() {
            return root();
        }
    });

This allows me to find the count of a table so understandable, without a line of pure HQL or any SQL code. One more example:
  List&lt;User&gt; users = dbService.createQuery(new DbServiceImpl.Query&lt;User, User&gt;() {
        @Override
        protected Selection&lt;? extends User&gt; select() {
            return root();
        }

        @Override
        protected Predicate where() {
            return builder().equal(root().get(&quot;username&quot;), username);
        }
    });

One with a join:
    List&lt;Recipe&gt; recipeList= dbService.createQuery(new DbServiceImpl.Query&lt;Recipe, Recipe&gt;() {
        @Override
        protected Selection&lt;? extends Recipe&gt; select() {
            return null;
        }

        @Override
        protected Predicate where() {
            root().fetch(&quot;author&quot;, JoinType.LEFT);
            return builder().equal(root().get(&quot;author&quot;).get(&quot;id&quot;),user.getId());
        }
    });

The code structure remains the same but the query is different. The access modifiers I used allowed me to encapsulate the behaviour on the use cases. What do you think? How broken or useful is that?
Welcome to Code Review, I have seen your code and I have seen you encapsulated your createQuery method like below:
try { body method } catch (Exception exception) { exception.printStackTrace(); }

You are basically catching all NullPointerException cases and other runtime exceptions you can meet within your method with the Exception class, this is a sign something should be redesigned to avoid these situations.
About the paradigm you are following , you are implementing a personal version of the repository pattern, referring to structures like the jpa repository present for example in the spring framework. You can check while you are creating different queries worrying about the construction of a valid query (and this is the reason why you had to encapsulate your method in the try catch construct), it should be better using an already built class to do the same things with much less effort.