gpt4 book ai didi

inheritance - Java EE 6 注解继承之谜

转载 作者:行者123 更新时间:2023-12-04 02:57:07 26 4
gpt4 key购买 nike

我在一些场景中将继承与 EJB 一起使用,有时在父类(super class)中使用注释,例如这个通用 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);
}

}

使用这样实现的示例子类:
@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;
}
}
}

我最近读到 java 注释不是继承的 (source) .这应该会导致我的 JpaDAO 在访问其实体管理器或其标准构建器时抛出空指针异常(因为 @PersistanceContext 和 @PostConstruct 都将被忽略),但事实并非如此。有人可以澄清这实际上是如何工作的吗?我有点担心父类(super class)中的 @TransactionAttributes 会发生什么,当子类将 NOT_SUPPORTED 作为类默认值时,我可以信任 REQUIRED 从子类调用时实际使用事务吗?

最佳答案

Java 注释不是继承的,但 JavaEE 规范更改了规则以允许这些属性按预期工作。请参阅通用注释 1.1 规范。 2.1 节甚至以@TransactionAttribute 为例。 EJB 3.1 第 13.3.7.1 节还明确说明了 @TransactionAttribute 的规则:

If the bean class has superclasses, the following additional rules apply.

  • transaction attribute specified on a superclass S applies to the business methods defined by S. If a class-level transaction attribute is not specified on S, it is equivalent to specification of TransactionAttribute(REQUIRED) on S.
  • A transaction attribute may be specified on a business method M defined by class S to override for method M the transaction attribute value explicitly or implicitly specified on the class S.
  • If a method M of class S overrides a business method defined by a superclass of S, the transaction attribute of M is determined by the above rules as applied to class S.

简而言之,对于大多数 JavaEE 注释,除非子类覆盖该方法,否则方法级注释适用于该方法,并且类级注释仅适用于该类中定义的所有方法。该规则不适用于“组件定义”类级别的注释,例如 @Stateless(请参阅 EJB 3.1 规范第 4.9.2.1 节)

关于inheritance - Java EE 6 注解继承之谜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5536583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com