Persist命令在事务结束前执行

时间:2014-12-24 10:59:40

标签: hibernate java-ee ejb-3.1 jta

问题在于:

我有使用EJB 3.1的Generic Dao实现和接口,扩展了 具体实现和业务接口:

public abstract class GenericDaoImpl<T extends Rewritable<T>, PkType extends Serializable> implements GenericDao<T, PkType> {

@PersistenceContext(unitName = "tsm")
protected EntityManager em;

private final Class<T> clazz;

public GenericDaoImpl(final Class<T> clazz) {
    this.clazz = clazz;
}

public void create(final T entity) {
    em.persist(entity);
}

public T find(final PkType pk) {
    return (T) em.find(clazz, pk);
}

public void update(final PkType id, final T entity) {
    T oldEntity = em.find(clazz, id);
    oldEntity.overrideFrom(entity);
    em.merge(oldEntity);
}

public void update(final T entity) {
    em.merge(entity);
}

public void deleteById (final PkType id) {
    T entity = find(id);
    if (entity == null) {
        throw new TSMServiceException("Couldn't find " + entity.getClass().getSimpleName() + " with id: " + id);
    }
    em.remove(entity);
}

public void delete (final T entity) {
    em.remove(entity);
}


/**
 *
 * @return returns true by default if entity has no specific relations
 */
public boolean hasNoRelations(PkType id) {
    return true;
}

}

示例实现是:

@Stateless

公共类CardProfileDaoImpl扩展GenericDaoImpl实现CardProfileDao {

public CardProfileDaoImpl() {
    super(CardProfile.class);
}

//specific methods for current entity

@Override
public boolean hasNoRelations(Long id) {
    return em.createNamedQuery("CHECK_CARD_PROFILE_HAS_NO_RELATIONS")
            .setParameter("id", id)
            .setMaxResults(1)
            .getResultList()
            .isEmpty();
}

}

示例业务接口:

@Local

公共接口CardProfileDao扩展了GenericDao {

// specific methods for this entity

}

但是当我在CMT事务中调用扩展方法试图保持新实体时,它会在CMT事务提交之前被刷新。更重要的是,它在事务回滚后保留在数据库中。 据我所知,EJB容器的继承并没有公开从另一个类扩展的业务接口。因此它无法加入对CMT交易的持久调用。所以,问题是:在CMT事务中有没有办法从GenericDaoImpl执行方法?

0 个答案:

没有答案