JPA事务和回滚

时间:2014-08-01 12:30:37

标签: java jpa-2.0 ejb-3.0 jpa-2.1

hy all;

如果我有4个表A,B和C,D其中:

  • A和B之间有关系。
  • 表C与A,B和D无关,
  • 表D与A,B和C没有关系

我有4个本地接口用于ongly table

我有4个用于保存和更新的类实现接口......

现在我喜欢在A中保存新记录,但我必须保存在B中,因为它与A和C和D中的新记录有关系。

public class GestioneAnagraficaBean {
@EJB
private InterfaceA classA

@EJB
private InterfaceB classB

@EJB
private InterfaceC classC

@EJB
private InterfaceD classD


public void save(){

    try{

            classB.create(b);

            classA.create(a);

            classC.create(C);

            classD.create(d);

    }catch(Exception e){
        ...

    }

}

}

我的问题是,如果我在创建C中例外,我必须roolback创建B和A.

  • 可以先检查所有保存吗?
  • 你有一个想法,我可以做到。
  • 问题出现在@TransactionManagement(TransactionManagementType.BEAN)中,不是这个问题的关系
  • 我可以删除classB.create(b)因为我调用classA.create(a)并且当我保存A时自动保存B?

如果你帮助我,或者对新课程或界面的另一个想法,我很高兴

代码ClassA:

@TransactionManagement(TransactionManagementType.BEAN)
@Stateless
public class ClassA extends AbstractFacade<A> implements InterfaceA {

    @PersistenceContext
    private EntityManager em;

    @Resource
    private EJBContext    context;

    /**
     * @see it.bway.lavoraconnoi.facade.AbstractFacade#getEJBContext()
     * 
     * @author Bway s.r.l. - asghaier <br>
     * 
     *         Created on 18/giu/2014
     */
    @Override
    public EJBContext getEJBContext() {
        return this.context;
    }

    /**
     * <br>
     * <br>
     * 
     * @author Bway s.r.l. - asghaier <br>
     * 
     *         Created on 16/mag/2014 <br>
     * 
     * @param entityClass
     * 
     */
    public AdresseFacade() {
        super(ClassA.class);
    }


    @Override
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    /**
     * @see it.bway.lavoraconnoi.entities.model.AbstractFacade#getEntityManager()
     * 
     * @author Bway s.r.l. - asghaier <br>
     * 
     *         Created on 16/mag/2014
     */
    @Override
    public EntityManager getEntityManager() {
        this.em.getEntityManagerFactory().getCache().evictAll();
        return this.em;
    }

}

Code AbstractFacade:

public abstract class AbstractFacade<T> {

    /**
     * Classa in oggetto.
     */
    private final Class<T> entityClass;

    /**
     * Torna {@link EntityManager} <br>
     * <br>
     * 
     * @author Bway s.r.l. - asghaier
     * 
     *         Created on 16/mag/2014
     * 
     * @return {@link EntityManager}
     * 
     */
    public abstract EntityManager getEntityManager();

    /**
     * Torna {@link EJBContext} <br>
     * <br>
     * 
     * @author Bway s.r.l. - asghaier <br>
     * 
     *         Created on 18/giu/2014 <br>
     * 
     * @return {@link EJBContext}
     * 
     */
    public abstract EJBContext getEJBContext();

    /**
     * Constructore <br>
     * <br>
     * 
     * @author Bway s.r.l. - asghaier <br>
     * 
     *         Created on 16/mag/2014
     * 
     * @param entityClass classa in oggetto
     * 
     */
    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    /**
     * Salvataggio di un oggetto di {@link AbstractFacade#entityClass} <br>
     * <br>
     * 
     * @author Bway s.r.l. - asghaier<br>
     * 
     *         Created on 16/mag/2014
     * 
     * @param entity oggetto di {@link AbstractFacade#entityClass}
     * @throws LavoraConNoiException in caso di errori
     * 
     */
    public void create(T entity) throws LavoraConNoiException {

        UserTransaction utx = getEJBContext().getUserTransaction();

        try {

            utx.begin();

            getEntityManager().persist(entity);

            utx.commit();

        }
        catch (Throwable t) {

            try {
                utx.rollback();
            }
            catch (Throwable e) {
                throw new LavoraConNoiException(e);
            }

            throw new LavoraConNoiException(t);
        }
    }

    /**
     * Merge di un oggetto di {@link AbstractFacade#entityClass} <br>
     * <br>
     * 
     * @author Bway s.r.l. - asghaier <br>
     * 
     *         Created on 16/mag/2014
     * 
     * @param entity {@link AbstractFacade#entityClass}
     * @throws LavoraConNoiException in caso di errori
     * 
     */
    public T merge(T entity) throws LavoraConNoiException {

        UserTransaction utx = getEJBContext().getUserTransaction();

        try {

            utx.begin();

            T entitymerge = getEntityManager().merge(entity);

            utx.commit();

            return entitymerge;
        }
        catch (Throwable t) {

            try {
                utx.rollback();
            }
            catch (Throwable e) {
                throw new LavoraConNoiException(e);
            }

            throw new LavoraConNoiException(t);
        }
    }

    /**
     * Rimuovere di un oggetto di {@link AbstractFacade#entityClass} <br>
     * <br>
     * 
     * @author Bway s.r.l. - asghaier
     * 
     *         Created on 16/mag/2014
     * 
     * @param id id del oggetto da rimuovere
     * @throws LavoraConNoiException in caso di errore
     * 
     */
    public void remove(Integer id) throws LavoraConNoiException {

        UserTransaction utx = null;

        try {

            if (id != null) {

                T toRemove = find(id);

                if (toRemove != null) {

                    utx = getEJBContext().getUserTransaction();

                    utx.begin();

                    getEntityManager().remove(toRemove);

                    utx.commit();
                }

            }

        }
        catch (Throwable t) {

            if (utx != null) {

                try {
                    utx.rollback();
                }
                catch (Throwable e) {
                    throw new LavoraConNoiException(e);
                }

            }

            throw new LavoraConNoiException(t);
        }
    }

    /**
     * Ricerca di oggetto di {@link AbstractFacade#entityClass} con id passato in parametro <br>
     * <br>
     * 
     * @author Bway s.r.l. - asghaier <br>
     * 
     *         Created on 16/mag/2014 <br>
     * 
     * @param id id del oggetto da ricercare
     * @return Entitie di {@link AbstractFacade#entityClass} o null se non è trovato
     * @throws LavoraConNoiException in caso d'errore
     * 
     */
    public T find(Object id) throws LavoraConNoiException {

        try {
            return getEntityManager().find(entityClass, id);
        }
        catch (Throwable t) {

            throw new LavoraConNoiException(t);
        }

    }

    /**
     * Ricerca di tutti i oggetti di {@link AbstractFacade#entityClass} <br>
     * <br>
     * 
     * @author Bway s.r.l. - asghaier <br>
     * 
     *         Created on 16/mag/2014 <br>
     * 
     * @return Lista del risultato della ricerca
     * @throws LavoraConNoiException in caso d'errore
     * 
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public List<T> findAll() throws LavoraConNoiException {

        try {

            CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();

            cq.select(cq.from(entityClass));

            return getEntityManager().createQuery(cq).getResultList();
        }
        catch (Throwable t) {

            throw new LavoraConNoiException(t);
        }

    }

    /**
     * Ritorna numero dei oggetti di {@link AbstractFacade#entityClass} <br>
     * <br>
     * 
     * @author Bway s.r.l. - asghaier <br>
     * 
     *         Created on 16/mag/2014 <br>
     * 
     * @return numero dei oggetti
     * @throws LavoraConNoiException in caso d'errore
     * 
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public int count() throws LavoraConNoiException {

        try {

            CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();

            Root<T> rt = cq.from(entityClass);

            cq.select(getEntityManager().getCriteriaBuilder().count(rt));

            Query q = getEntityManager().createQuery(cq);

            return ((Long) q.getSingleResult()).intValue();
        }
        catch (Throwable t) {

            throw new LavoraConNoiException(t);
        }

    }

}

InterfaceA代码:

@Local
public interface InterfaceA{

    public void create(A entity) throws LavoraConNoiException;

    public A merge(A entity) throws LavoraConNoiException;

    public void remove(Integer id) throws LavoraConNoiException;

    public A find(Object id) throws LavoraConNoiException;

    public List<A> findAll() throws LavoraConNoiException;

    public int count() throws LavoraConNoiException;

    public EntityManager getEntityManager();

    public void setEntityManager(EntityManager em);

}

0 个答案:

没有答案