我在ejb3.0中有一些slsb,有很多方法,我是否需要使用事务属性来注释每个方法?

时间:2010-10-20 12:41:55

标签: jpa jboss java-ee ejb-3.0

课程解体:

@Stateless
@Local( { IMyLocalSLSB.class })
@Remote( { IMyRemoteSLSB.class }) })
public class mySLSB implements IMySLSB {

private EntityManager em;

现在用于插入/更新和删除db记录的一些方法是需要的事务注释,我认为它们不是jboss,而EJB3.0使用默认值来处理它们:

public void crud(MyEntity myEntity)  {
    em.merge(myEntity);// inserts or updates    
}

public void deleteInsert(MyEntity myEntity)  {

    MyEntity found = em.find(MyEntity.class, myEntity.getMyEntityPK());

    if (found == null) {
        em.merge(myEntity);
    } else {
        em.remove(found);
    }   
}

最后,这是返回记录/实体的最佳方式,但不会保留任何进一步的更改。我记录并更改数据然后显示(我可以放入DTO,但这似乎更整洁/更少的对象)

public MyEntity getAnEntity(integer id){

    MyEntity myEntity = em.find(MyEntity.class, id);
    org.hibernate.Session session = (Session) em.getDelegate();
    session.evict(myEntity);
    return myEntity;
}

总结是slsb中事务方法所需的注释,session.evict是阻止任何更改持久化的最佳方法吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

  

总结了SLSB中交易方法所需的注释

不,他们不需要。您可以在类级别应用@TransactionAttribute注释,以指定企业bean的所有业务方法的默认事务属性。如果不这样做,您的bean将默认为@TransactionAttribute(REQUIRED)(即所有方法都默认处理)。

  

并且session.evict是停止任何更改的最佳方法吗?