The mysterious veil of Java EE 6 annotation inheritance

I use EJB inheritance in several schemes, and sometimes use annotations in superclasses, such as this general entitydao:

public class JpaDAO<T>{
    protected Class<T> entityClass;

    @PersistenceContext(unitName="CarrierPortalPU")
    protected EntityManager em;
    protected CriteriaBuilder cb;

    @postconstruct
    private void init() {
        cb = em.getCriteriaBuilder();
    }

    public JpaDAO(Class<T> 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<T> findAll(){
        CriteriaQuery<T> cq = cb.createQuery(entityClass);
        Root<T> 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);
    }

}

Use the example class implemented as follows:

@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class DepartmentDAO extends JpaDAO<Department> {

    public DepartmentDAO() {
        super(Department.class);
    }

    public Department findByName(String name){
        CriteriaQuery<Department> cq = cb.createQuery(Department.class);
        Root<Department> 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;
        }
    }
}

I recently read that Java annotations are not sources This should cause my jpadao to throw a null pointer exception when accessing its body manager or its standard object (because both @ persistancecontext and @ postconstruct will be ignored), but it is not Can anyone clarify whether this is really effective? I am very worried about what happens to @ transactionattributes in the superclass when the subclass has not_ When supported is the default value of the class, can a required transaction be trusted to actually use the transaction called from the subclass?

Solution

Java annotations are not inherited, but the Java EE specification changes the rules to allow these properties to work as expected See common note 1.1 specifications Section 2.1 even takes @ transactionattribute as an example EJB 3.1 section 13.3 Section 7.1 also specifies the rules of @ transactionattribute:

In short, for most Java EE annotations, method level annotations apply to the method unless the subclass overrides the method, and class level annotations apply only to all methods defined in the class This rule does not apply to "component definition" class level annotations, such as @ stateless (see EJB 3.1 specification section 4.9.2.1)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>