List of usage examples for javax.persistence EntityManager persist
public void persist(Object entity);
From source file:org.eclipse.smila.binarystorage.persistence.jpa.JPABinaryPersistence.java
/** * Stores the given BinaryStorageDao, updating an existing one or creating a new one. * * @param dao//ww w. j a va 2 s .c o m * the BinaryStorageDao to store * @throws BinaryStorageException * if any error occurs */ // TODO: don't know if this synchronize is good, was needed to pass the JUNit test TestConcurrentBSSAccessJPA private synchronized void store(final BinaryStorageDao dao) throws BinaryStorageException { _lock.readLock().lock(); try { final EntityManager em = createEntityManager(); final EntityTransaction transaction = em.getTransaction(); try { transaction.begin(); if (findBinaryStorageDao(em, dao.getId()) == null) { em.persist(dao); } else { em.merge(dao); } transaction.commit(); if (_log.isTraceEnabled()) { _log.trace("stored content of id:" + dao.getId()); } } catch (final Exception e) { if (transaction.isActive()) { transaction.rollback(); } throw new BinaryStorageException(e, "error storing record id: " + dao.getId()); } finally { closeEntityManager(em); } } finally { _lock.readLock().unlock(); } }
From source file:com.chiralbehaviors.CoRE.event.status.StatusCode.java
@Override public void link(Relationship r, StatusCode child, Agency updatedBy, Agency inverseSoftware, EntityManager em) { assert r != null : "Relationship cannot be null"; assert child != null; assert updatedBy != null; assert em != null; StatusCodeNetwork link = new StatusCodeNetwork(this, r, child, updatedBy); em.persist(link); StatusCodeNetwork inverse = new StatusCodeNetwork(child, r.getInverse(), this, inverseSoftware); em.persist(inverse);//w w w .j a v a 2 s .c o m }
From source file:nl.b3p.kaartenbalie.core.server.accounting.AccountManager.java
/** * //from www . j a v a 2s. com * @param transactionClass * @param description * @return * @throws java.lang.Exception */ public Transaction prepareTransaction(int type, String description) throws Exception { if (!isEnableAccounting()) { return null; } log.debug("Getting entity manager ......"); EntityManager em = MyEMFDatabase.getEntityManager(MyEMFDatabase.MAIN_EM); Account account = (Account) em.find(Account.class, organizationId); Transaction transaction = new Transaction(); transaction.setType(type); transaction.setStatus(Transaction.PENDING); transaction.setAccount(account); transaction.setDescription(description); em.persist(transaction); em.flush(); return transaction; }
From source file:uk.ac.horizon.ug.locationbasedgame.author.CRUDServlet.java
/** Create on POST. * E.g. curl -d '{...}' http://localhost:8888/author/configuration/ * @param req/*from w w w . j a v a2 s . c om*/ * @param resp * @throws ServletException * @throws IOException */ private void doCreate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub try { Object o = parseObject(req); Key key = validateCreate(o); // try adding EntityManager em = EMF.get().createEntityManager(); EntityTransaction et = em.getTransaction(); try { et.begin(); if (em.find(getObjectClass(), key) != null) throw new RequestException(HttpServletResponse.SC_CONFLICT, "object already exists (" + key + ")"); em.persist(o); et.commit(); logger.info("Added " + o); } finally { if (et.isActive()) et.rollback(); em.close(); } resp.setCharacterEncoding(ENCODING); resp.setContentType(JSON_MIME_TYPE); Writer w = new OutputStreamWriter(resp.getOutputStream(), ENCODING); JSONWriter jw = new JSONWriter(w); listObject(jw, o); w.close(); } catch (RequestException e) { resp.sendError(e.getErrorCode(), e.getMessage()); } catch (Exception e) { logger.log(Level.WARNING, "Getting object of type " + getObjectClass(), e); resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); return; } }
From source file:ec.edu.chyc.manejopersonal.controller.PersonaJpaController.java
public void create(Persona persona/*, Contratado contratado, Tesista tesista, Pasante pasante*/) throws Exception { EntityManager em = null; try {//from w w w . j a v a2 s . c om em = getEntityManager(); em.getTransaction().begin(); em.persist(persona); /* if (contratado != null) { contratado.setId(persona.getId()); em.persist(contratado); } if (tesista != null) { tesista.setId(persona.getId()); em.persist(tesista); } if (pasante != null) { pasante.setId(persona.getId()); em.persist(pasante); } */ for (PersonaTitulo personaTitulo : persona.getPersonaTitulosCollection()) { personaTitulo.setPersona(persona); Titulo titulo = personaTitulo.getTitulo(); if (titulo.getId() == null || titulo.getId() < 0) { titulo.setId(null); em.persist(titulo); } if (personaTitulo.getUniversidad().getId() == null || personaTitulo.getUniversidad().getId() < 0) { personaTitulo.getUniversidad().setId(null); em.persist(personaTitulo.getUniversidad()); } if (personaTitulo.getId() == null || personaTitulo.getId() < 0) { personaTitulo.setId(null); em.persist(personaTitulo); } } /*for (PersonaFirma personaFirma : persona.getPersonaFirmasCollection()) { personaFirma.setPersona(persona); Firma firma = personaFirma.getFirma(); if (firma.getId() == null || firma.getId() < 0) { firma.setId(null); em.persist(firma); } if (personaFirma.getId() == null || personaFirma.getId() < 0) { personaFirma.setId(null); em.persist(personaFirma); } }*/ guardarPersonaFirma(em, persona); em.getTransaction().commit(); } finally { if (em != null) { em.close(); } } }
From source file:in.bookmylab.jpa.JpaDAO.java
public User saveUser(User user, boolean savePassword) { EntityManager em = emf.createEntityManager(); try {// ww w . j a v a2s . c o m user.password = Utils.hashPassword(user.password); em.getTransaction().begin(); if (user.userId == null) { grantDefaultRoleToUser(em, user); em.persist(user); } else { user = em.merge(user); if (savePassword) { Query q = em.createNamedQuery("User.setPassword"); q.setParameter("password", user.password); q.setParameter("userId", user.userId); q.executeUpdate(); } } em.getTransaction().commit(); } finally { em.clear(); em.close(); } return user; }
From source file:org.eclipse.smila.recordstorage.impl.RecordStorageImpl.java
/** * {@inheritDoc}/*from www . j ava2 s.co m*/ */ @Override public void storeRecord(final Record record) throws RecordStorageException { if (record == null) { throw new RecordStorageException("parameter record is null"); } _lock.readLock().lock(); try { final EntityManager em = createEntityManager(); final EntityTransaction transaction = em.getTransaction(); try { final RecordDao dao = new RecordDao(record); transaction.begin(); if (findRecordDao(em, record.getId()) == null) { em.persist(dao); } else { em.merge(dao); } transaction.commit(); if (_log.isTraceEnabled()) { _log.trace("stored record Id:" + record.getId()); } } catch (final Exception e) { if (transaction.isActive()) { transaction.rollback(); } throw new RecordStorageException(e, "error storing record id: " + record.getId()); } finally { closeEntityManager(em); } } finally { _lock.readLock().unlock(); } }
From source file:uk.ac.edukapp.service.UserAccountService.java
public Useraccount registerNewUser(String username, String email, String password, String realname) throws Exception { EntityManager em = getEntityManagerFactory().createEntityManager(); em.getTransaction().begin();//from w w w. ja v a2 s . co m Useraccount user = new Useraccount(); user.setUsername(username); user.setEmail(email); UUID token = UUID.randomUUID(); String salt = token.toString(); String hashedPassword = MD5Util.md5Hex(salt + password); user.setPassword(hashedPassword); user.setSalt(salt); user.setToken("03"); em.persist(user); log.info("User created with id: " + user.getId()); Accountinfo info = new Accountinfo(); info.setId(user.getId()); info.setRealname(realname); info.setJoined(new Timestamp(new Date().getTime())); em.persist(info); //LtiProvider lti = new LtiProvider(user); //em.persist(lti); em.getTransaction().commit(); em.close(); ActivityService as = new ActivityService(ctx); as.addUserActivity(user.getId(), "joined", 0); return user; }
From source file:org.apache.falcon.jdbc.MonitoringJdbcStateStore.java
public void putSLAAlertInstance(String feedName, String cluster, Date nominalTime, Boolean isSLALowMissed, Boolean isSLAHighMissed) { EntityManager entityManager = getEntityManager(); FeedSLAAlertBean feedSLAAlertBean = new FeedSLAAlertBean(); feedSLAAlertBean.setFeedName(feedName); feedSLAAlertBean.setClusterName(cluster); feedSLAAlertBean.setNominalTime(nominalTime); feedSLAAlertBean.setIsSLALowMissed(isSLALowMissed); feedSLAAlertBean.setIsSLAHighMissed(isSLAHighMissed); try {//from ww w.j av a 2 s. com beginTransaction(entityManager); entityManager.persist(feedSLAAlertBean); } finally { commitAndCloseTransaction(entityManager); } }
From source file:com.chiralbehaviors.CoRE.network.Relationship.java
@Override public void link(Relationship r, Relationship child, Agency updatedBy, Agency inverseSoftware, EntityManager em) { assert r != null : "Relationship cannot be null"; assert child != null; assert updatedBy != null; assert em != null; RelationshipNetwork link = new RelationshipNetwork(this, r, child, updatedBy); em.persist(link); RelationshipNetwork inverse = new RelationshipNetwork(child, r.getInverse(), this, inverseSoftware); em.persist(inverse);//from w w w . j a v a2s. c o m }