09 April 2014
You might want to implement some generic template type methods for your domain model superclass in order to avoid duplicating boilerplate code for several classes, one example is the findAll method, in certain scenarios you might want to search for all entities of a given Class:
<span class="nd">@PersistenceContext</span>
<span class="kd">protected</span> <span class="n">EntityManager</span> <span class="n">entityManager</span><span class="o">;</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="n">List</span><span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="nf">findAll</span><span class="o">(</span><span class="n">Class</span><span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="n">clazz</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">ServiceException</span> <span class="o">{</span>
<span class="n">CriteriaBuilder</span> <span class="n">cb</span> <span class="o">=</span> <span class="n">getEntityManager</span><span class="o">().</span><span class="na">getCriteriaBuilder</span><span class="o">();</span>
<span class="n">CriteriaQuery</span><span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="n">cq</span> <span class="o">=</span> <span class="n">cb</span><span class="o">.</span><span class="na">createQuery</span><span class="o">(</span><span class="n">clazz</span><span class="o">);</span>
<span class="n">Root</span><span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="n">rootEntry</span> <span class="o">=</span> <span class="n">cq</span><span class="o">.</span><span class="na">from</span><span class="o">(</span><span class="n">clazz</span><span class="o">);</span>
<span class="n">CriteriaQuery</span><span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="n">all</span> <span class="o">=</span> <span class="n">cq</span><span class="o">.</span><span class="na">select</span><span class="o">(</span><span class="n">rootEntry</span><span class="o">);</span>
<span class="n">TypedQuery</span><span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="n">allQuery</span> <span class="o">=</span> <span class="n">getEntityManager</span><span class="o">().</span><span class="na">createQuery</span><span class="o">(</span><span class="n">all</span><span class="o">);</span>
<span class="k">return</span> <span class="n">allQuery</span><span class="o">.</span><span class="na">getResultList</span><span class="o">();</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="n">List</span><span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="nf">findAllQuery</span><span class="o">(</span><span class="n">Class</span><span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="n">clazz</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">ServiceException</span> <span class="o">{</span>
<span class="n">Query</span> <span class="n">query</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">entityManager</span><span class="o">.</span><span class="na">createQuery</span><span class="o">(</span>
<span class="s">"select o from "</span> <span class="o">+</span> <span class="n">clazz</span><span class="o">.</span><span class="na">getSimpleName</span><span class="o">()</span> <span class="o">+</span> <span class="s">" o"</span><span class="o">);</span>
<span class="k">return</span> <span class="o">(</span><span class="n">List</span><span class="o"><</span><span class="n">T</span><span class="o">>)</span> <span class="n">query</span><span class="o">.</span><span class="na">getResultList</span><span class="o">();</span>
<span class="o">}</span>
// ... more code
Where DomainEntity is a generic domain entity superclass and ID a generic Id type that can be serializable, like String, Integer and others.
Here I listed two ways of getting the list, first using CriteriaBuilder and second using JPA Query Language, I personally prefer the first one since it only handles object instances.