java - Is there a way to reduce the amount of boiler-plate code associated with a CriteriaQuery (in JPA 2.0)? -


That I have a unit named Named Entity, which has just an ID and a string field called "name" (assuming the unique obstacle in it is true). Here's what NamedEntityManager can look like:

  Public class NamedEntityManager {// Instruments using your Framework EntityManager entityManager; // DB Public Interpreting all existing institutions & lt; NamedEntity & gt; Type NamedEntity from QueryAll () {CriteriaBuilder Builder = entityManager.getCriteriaBuilder (); CriteriaQuery & LT; NamedEntity & gt; Query = builder.createQuery (NamedEntity.class); Return unit Magerager.createQuery (query) .getResultList (); } / / Retrieve the specific unit named a specific entity by using the public NamedEntity queryByName (string name) {CriteriaBuilder Builder = entityManager.getCriteriaBuilder (); CriteriaQuery & LT; NamedEntity & gt; Query = builder.createQuery (NamedEntity.class); The root of the & lt; NamedEntity & gt; Root = query.from (NamedEntity.class); Query = query.where (get root.lt; namedEntity & gt; ("name"). (Name) in); // Strike retention unit / attempt to hold block left manager. Query (query) .getSingleResult (); }}  

Is there a way to process code to avoid copying / pasting the same line of code in each query method? Perhaps in some way can a Critical Object be reused?

I was looking for something like this, you can see (LGPL is licensed) Which can keep JPAs in the backend.

I'm still reading this, but with their examples, it looks very clean.

  HQLQuery q = new HibernateQuery session); Cuchet cat = new q cat ("cat"); // query type list & lt; Cat & gt; Cats = q.from (cat). Where (cat.name.between ("A", "B")). List (cat);  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -