0

hy all;

if i have 4 table A,B and C, D where:

  • A and B has a relationship between them.
  • table C not has relation with A and B and D,
  • table D not has relation with A, B and C

i have 4 interface local for ongly table

i have 4 class implement interface for save and update ...

now i like save new record in A but i must save in B because has a relationship with A and new record in C and 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){
        ...

    }

}

}

My problem if i take a exception in create C for example i must roolback create B and A.

  • its possible to check all save first i commit?
  • you have a idea for haw i can do it.
  • the problem is in @TransactionManagement(TransactionManagementType.BEAN) ot not hase relation for this problem
  • i can delete classB.create(b) beacause i call classA.create(a) and it automatical save B when i save A?

i have happy if you help me, or another idea for new class or interface

code 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 code:

@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);

}
sghaier ali
  • 433
  • 2
  • 15

0 Answers0