Example usage for javax.persistence EntityManager clear

List of usage examples for javax.persistence EntityManager clear

Introduction

In this page you can find the example usage for javax.persistence EntityManager clear.

Prototype

public void clear();

Source Link

Document

Clear the persistence context, causing all managed entities to become detached.

Usage

From source file:ejb.bean.UsuarioDAOJPAImplBean.java

/**Mtodo para a remoo de usurio.
 * @author Richel Sensineli// w  w  w.  jav  a2 s . co m
 * @param id int - ID do usurio
 * @throws UsuarioNaoEncontradoException - usurio no encontrado
 */
@Override
public void removeUsuario(final int id) throws UsuarioNaoEncontradoException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU");
    EntityManager em = emf.createEntityManager();

    Usuario u = em.find(UsuarioImpl.class, id);

    em.getTransaction().begin();
    if (u == null) {
        throw new UsuarioNaoEncontradoException("usuario no encontrado");
    } else {
        em.remove(u);
        em.getTransaction().commit();
    }
    em.clear();
    em.close();
    emf.close();

}

From source file:ejb.bean.UsuarioDAOJPAImplBean.java

/**Mtodo para a criao de usurio
 * @author Richel Sensineli\/*from ww  w.  j  a v  a  2 s. com*/
 * @param nome String - Nome do usurio
 * @param sobrenome String - Nome do usurio
 * @return Usuario usuario - Objeto Usurio
 */
@Override
public Usuario criaUsuario(final String nome, final String sobrenome) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU");
    EntityManager em = emf.createEntityManager();

    UsuarioImpl user = new UsuarioImpl();
    user.setNome(nome);
    user.setSobrenome(sobrenome);

    em.getTransaction().begin();
    try {
        em.persist(user);
        em.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        em.getTransaction().rollback();
    }

    em.clear();
    em.close();
    emf.close();

    return user;
}

From source file:ejb.bean.UsuarioDAOJPAImplBean.java

/**Mtodo para a atualizao do usurio
 * @author Richel Sensineli/* w w w. j a  va 2 s  .  c o m*/
 * @param id int - ID do usurio
 * @param nome String - Nome do usurio
 * @param sobrenome String - Nome do usurio
 */
@Override
public void updateUsuario(final int id, final String nome, final String sobrenome)
        throws UsuarioNaoEncontradoException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU");
    EntityManager em = emf.createEntityManager();

    UsuarioImpl user = em.find(UsuarioImpl.class, id);
    user.setNome(nome);
    user.setSobrenome(sobrenome);

    em.getTransaction().begin();
    try {
        em.merge(user);
        em.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        em.getTransaction().rollback();
    }

    em.clear();
    em.close();
    emf.close();
}

From source file:in.bookmylab.jpa.JpaDAO.java

public List<StaticData> getStaticData() {
    EntityManager em = emf.createEntityManager();
    List<StaticData> data = null;
    try {//from   w  ww .ja v a2 s.  c o m
        data = em.createNamedQuery("StaticData.findAll", StaticData.class).getResultList();
    } finally {
        em.clear();
        em.close();
    }
    return data;
}

From source file:in.bookmylab.jpa.JpaDAO.java

public List<ResourceBooking> getSlotsByDate(Date dt) {
    List<ResourceBooking> list = null;
    EntityManager em = emf.createEntityManager();
    try {//from   w w  w .j a  v  a  2s.  c  o  m
        Query q = em.createNamedQuery("ResourceBooking.findSlotsByDate");
        list = q.setParameter("bookingDate", dt).getResultList();
    } finally {
        em.clear();
        em.close();
    }
    return list;
}

From source file:org.drools.semantics.lang.dl.DL_9_CompilationTest.java

private void persist(Object o, EntityManager em) {
    em.getTransaction().begin();
    em.persist(o);
    em.getTransaction().commit();
    em.clear();
}

From source file:in.bookmylab.jpa.JpaDAO.java

public User getUserByLogin(String email) {
    User u = null;/*www . java 2s  .c om*/
    EntityManager em = emf.createEntityManager();
    try {
        Query q = em.createNamedQuery("User.findByEmail");
        List<User> l = q.setParameter("email", email).getResultList();
        if (l.size() == 1) {
            u = l.get(0);
        }
    } finally {
        em.clear();
        em.close();
    }
    return u;
}

From source file:org.SRV.testServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    double value1 = Double.parseDouble(request.getParameter("val1"));
    double value2 = Double.parseDouble(request.getParameter("val2"));

    UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    String username = user.getUsername();
    //com.seostella_springsecuritybasics_war_1.0.0-BUILD-SNAPSHOTPU
    entityManagerFactory = Persistence.createEntityManagerFactory("org.SRV_war_1.0.0-BUILD-SNAPSHOTPU");

    double result = 0;
    switch (request.getParameter("operation").charAt(0)) {
    case '+':
        result = value1 + value2;/*from  ww  w  . j a va 2  s . c o m*/
        break;
    case '-':
        result = value1 - value2;
        break;
    case '*':
        result = value1 * value2;
        break;
    case '/':
        result = value1 / value2;
        break;
    default:
        break;
    }

    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    Date date = new Date();
    Essence essence = new Essence();
    essence.setLogin(username);
    essence.setDate(date);
    essence.setValue1(Double.toString(value1));
    essence.setValue2(Double.toString(value2));
    essence.setOperation(request.getParameter("operation"));
    essence.setResult(Double.toString(result));
    entityManager.persist(essence);
    entityManager.getTransaction().commit();
    entityManager.clear();
    entityManager.close();
    entityManagerFactory.close();
    JSONObject obj;
    obj = new JSONObject();
    obj.put("result", result);
    //response.getOutputStream().print(obj.toString());
    response.getWriter().write(obj.toString());
}

From source file:org.eclipse.jubula.client.core.persistence.ProjectPM.java

/**
 * Load an instance of ProjectPO into the readonly session. The read
 * instance is db identical to the key supplied.
 * used for open project/*from   w  w  w .  j av  a  2s  .c  om*/
 * 
 * @param project
 *            Look for this instance in the db.
 * @throws PMReadException
 *             if loading from db failed
 * 
 */
public static void loadProjectInROSession(IProjectPO project) throws PMReadException {

    GeneralStorage.getInstance().reset();
    EntityManager s = GeneralStorage.getInstance().getMasterSession();
    s.clear(); // get rid of all session (cached) data
    try {
        preloadData(s, project);

        IProjectPO p = s.find(NodeMaker.getProjectPOClass(), project.getId());
        GeneralStorage.getInstance().setProject(p);
        ParamNameBP.getInstance().initMap();
        ComponentNamesBP.getInstance().init();
    } catch (PersistenceException e) {
        GeneralStorage.getInstance().setProject(null);
        OperationCanceledException cancel = checkForCancel(e);
        if (cancel != null) {
            throw cancel;
        }
        String msg = Messages.CantReadProjectFromDatabase + StringConstants.DOT;
        log.error(Messages.UnexpectedPersistenceErrorIgnored + StringConstants.DOT, e);
        throw new PMReadException(msg + e.getMessage(), MessageIDs.E_CANT_READ_PROJECT);
    } catch (PMException e) {
        String msg = Messages.CouldNotReadParamNamesFromDB + StringConstants.DOT;
        log.error(msg, e);
        throw new PMReadException(msg + e.getMessage(), MessageIDs.E_CANT_READ_PROJECT);
    } catch (JBException e) {
        GeneralStorage.getInstance().setProject(null);
        String msg = Messages.CantReadProjectFromDatabase + StringConstants.DOT;
        log.error(Messages.UnexpectedPersistenceErrorIgnored, e);
        throw new PMReadException(msg + e.getMessage(), MessageIDs.E_CANT_READ_PROJECT);
    }
}

From source file:org.emau.icmvc.ganimed.epix.core.internal.PersonPreprocessedCache.java

/**
 * Initialize cache//from  ww w  . j  a v  a  2s  .com
 *
 * @param em
 * @param matchingConfiguration
 */
public void init(EntityManager em, MatchingConfiguration matchingConfiguration) {
    synchronized (emSynchronizerDummy) {
        personPreprocessedPreprocessor = new CommonPreprocessor<T>();
        personPreprocessedPreprocessor.setMatchingConfiguration(matchingConfiguration);

        List<PersonPreprocessed> personPreprocessedList = getAllPersonPreprocessed(em);
        for (PersonPreprocessed personPreprocessed : personPreprocessedList) {
            ppCache.put(personPreprocessed.getPersonId(), personPreprocessed);
        }
        logger.info("ppCache initialized with: " + ppCache.size() + " entries");
        em.clear();
        initialised = true;
    }
}