Note that there are some explanatory texts on larger screens.

plurals
  1. POThe mystery of Java EE 6 annotations inheritance
    primarykey
    data
    text
    <p>I'm using inheritance with EJB in a few scenarios, sometimes with annotations in the super class like this generic entityDAO:</p> <pre><code>public class JpaDAO&lt;T&gt;{ protected Class&lt;T&gt; entityClass; @PersistenceContext(unitName="CarrierPortalPU") protected EntityManager em; protected CriteriaBuilder cb; @PostConstruct private void init() { cb = em.getCriteriaBuilder(); } public JpaDAO(Class&lt;T&gt; type) { entityClass = type; } @TransactionAttribute(TransactionAttributeType.REQUIRED) public void create(T entity) { em.persist(entity); } @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public T find(Object id) { return em.find(entityClass, id); } @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public List&lt;T&gt; findAll(){ CriteriaQuery&lt;T&gt; cq = cb.createQuery(entityClass); Root&lt;T&gt; entity = cq.from(entityClass); cq.select(entity); return em.createQuery(cq).getResultList(); } @TransactionAttribute(TransactionAttributeType.REQUIRED) public void remove(T entity) { em.remove(em.merge(entity)); } @TransactionAttribute(TransactionAttributeType.REQUIRED) public T edit(T entity) { return em.merge(entity); } } </code></pre> <p>With an example subclass implemented like this:</p> <pre><code>@Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public class DepartmentDAO extends JpaDAO&lt;Department&gt; { public DepartmentDAO() { super(Department.class); } public Department findByName(String name){ CriteriaQuery&lt;Department&gt; cq = cb.createQuery(Department.class); Root&lt;Department&gt; department = cq.from(Department.class); cq.where(cb.equal(department.get(Department_.name), name)); cq.select(department); try{ return em.createQuery(cq).getSingleResult(); }catch(Exception e){ return null; } } } </code></pre> <p>I recently read that java annotations are NOT inherited <a href="http://fusionsoft-online.com/articles-java-annotations.php" rel="noreferrer">(source)</a>. This should cause my JpaDAO to throw a null pointer exception when accessing its entitymanager or its criteriabuilder (since both @PersistanceContext and @PostConstruct would be ignored), however this it not the case. Can someone clarify how this really works? I am abit worried about what happens to my @TransactionAttributes in the superclass, can I trust a REQUIRED to actually use transactions when called from the subclass, when the subclass has NOT_SUPPORTED as class default?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload