List of usage examples for javax.persistence EntityManager find
public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode);
From source file:fr.mby.opa.picsimpl.dao.DbAlbumDao.java
@Override public void deleteAlbum(final Album album) throws AlbumNotFoundException { Assert.notNull(album, "No Album supplied !"); Assert.notNull(album.getId(), "Id should be set for delete !"); new TxCallback(this.getEmf()) { @Override/*from ww w . j av a 2 s . co m*/ protected void executeInTransaction(final EntityManager em) { final Album managedAlbum = em.find(Album.class, album.getId(), LockModeType.WRITE); if (managedAlbum == null) { throw new PictureNotFoundException(); } em.remove(managedAlbum); } }; }
From source file:fr.mby.opa.picsimpl.dao.DbAlbumDao.java
@Override public Album updateAlbum(final Album album) throws AlbumNotFoundException { Assert.notNull(album, "No Album supplied !"); Assert.notNull(album.getId(), "Id should be set for update !"); final TxCallbackReturn<Album> txCallback = new TxCallbackReturn<Album>(this.getEmf()) { @Override//from ww w . j a va2 s .c om protected Album executeInTransaction(final EntityManager em) { final Album managedAlbum = em.find(Album.class, album.getId(), LockModeType.WRITE); if (managedAlbum == null) { throw new AlbumNotFoundException(); } Album updatedAlbum = null; if (!managedAlbum.getLocked()) { // Update album if not locked ! managedAlbum.setName(album.getName()); managedAlbum.setDescription(album.getDescription()); updatedAlbum = em.merge(managedAlbum); } return updatedAlbum; } }; return txCallback.getReturnedValue(); }
From source file:fr.mby.opa.picsimpl.dao.DbPictureDao.java
@Override public void deletePicture(final Picture picture) throws PictureNotFoundException { Assert.notNull(picture, "No Picture supplied !"); Assert.notNull(picture.getId(), "Id should be set for delete !"); new TxCallback(this.getEmf()) { @Override/* w w w . j a v a 2 s . c o m*/ protected void executeInTransaction(final EntityManager em) { final Picture managedPicture = em.find(Picture.class, picture.getId(), LockModeType.WRITE); if (managedPicture == null) { throw new PictureNotFoundException(); } em.remove(picture); em.lock(picture, LockModeType.NONE); } }; }
From source file:fr.mby.opa.picsimpl.dao.DbProposalDao.java
@Override public ProposalBag createBag(final ProposalBag bag, final long branchId) { Assert.notNull(bag, "No ProposalBag supplied !"); Assert.isNull(bag.getId(), "Id should not be set for creation !"); new TxCallback(this.getEmf()) { @Override/*from w ww. ja v a 2 s . c om*/ // @SuppressWarnings("unchecked") protected void executeInTransaction(final EntityManager em) { // Retrieve branch final ProposalBranch branch = em.find(ProposalBranch.class, branchId, LockModeType.PESSIMISTIC_WRITE); if (branch == null) { throw new ProposalBranchNotFoundException(); } // Retrieve base bag (parent bag) and lock the row // final Query findParentQuery = em.createNamedQuery(ProposalBag.FIND_LAST_BRANCH_BAG); // findParentQuery.setParameter("branchId", branchId); // findParentQuery.setLockMode(LockModeType.PESSIMISTIC_WRITE); // Persist bag with its parent // final ProposalBag parentBag = Iterables.getFirst(findParentQuery.getResultList(), null); // bag.setBaseProposal(parentBag); // em.persist(bag); // Persist bag with its parent bag.setBaseProposal(branch.getHead()); em.persist(bag); // Update branch head pointer branch.setHead(bag); em.merge(branch); } }; return bag; }
From source file:com.enioka.jqm.tools.JobManagerHandler.java
/** * Update the {@link com.enioka.jqm.jpamodel.History} with the given progress data. * /*from w w w. java 2s . c om*/ * @param msg * @throws JqmKillException */ private void sendProgress(Integer msg) { EntityManager em = Helpers.getNewEm(); try { em.getTransaction().begin(); this.ji = em.find(JobInstance.class, this.ji.getId(), LockModeType.PESSIMISTIC_WRITE); ji.setProgress(msg); em.getTransaction().commit(); } finally { em.close(); } }
From source file:net.kamhon.ieagle.dao.JpaDao.java
@Override public T getReadonly(final Class<T> clazz, final Serializable serializablekey) { return getJpaTemplate().execute(new JpaCallback<T>() { @Override//w w w . j a v a 2 s . c om public T doInJpa(EntityManager em) throws PersistenceException { return em.find(clazz, serializablekey, LockModeType.OPTIMISTIC); } }); }
From source file:net.kamhon.ieagle.dao.JpaDao.java
@Override public T getForUpdate(final Class<T> clazz, final Serializable serializablekey) { return getJpaTemplate().execute(new JpaCallback<T>() { @Override/*from ww w. j a v a 2 s .c o m*/ public T doInJpa(EntityManager em) throws PersistenceException { return em.find(clazz, serializablekey, LockModeType.PESSIMISTIC_WRITE); } }); }
From source file:com.edugility.substantia.substance.TestCasePerson.java
@Test public void testingJPA() throws Exception { final EntityManagerFactory emf = Persistence.createEntityManagerFactory("test"); assertNotNull(emf);/*from ww w. ja va 2 s . co m*/ final EntityManager em = emf.createEntityManager(); assertNotNull(em); final EntityTransaction et = em.getTransaction(); assertNotNull(et); et.begin(); final Person p = new Person(); em.persist(p); em.flush(); assertFalse(p.isTransient()); assertTrue(p.isVersioned()); assertEquals(Long.valueOf(1L), p.getId()); assertEquals(Integer.valueOf(1), p.getVersion()); et.commit(); et.begin(); final Person p2 = em.find(Person.class, 1L, LockModeType.OPTIMISTIC_FORCE_INCREMENT); assertNotNull(p2); assertFalse(p2.isTransient()); assertTrue(p2.isVersioned()); assertEquals(Long.valueOf(1L), p2.getId()); assertEquals(Integer.valueOf(1), p2.getVersion()); et.commit(); et.begin(); final Person p3 = em.getReference(Person.class, 1L); assertNotNull(p3); assertFalse(p3.isTransient()); assertTrue(p3.isVersioned()); assertEquals(Long.valueOf(1L), p3.getId()); assertEquals(Integer.valueOf(2), p3.getVersion()); et.commit(); et.begin(); assertTrue(em.contains(p)); em.remove(p); et.commit(); em.close(); emf.close(); }
From source file:com.enioka.jqm.tools.JqmEngine.java
synchronized void checkEngineEnd() { jqmlogger.trace("Checking if engine should end with the latest poller"); for (QueuePoller poller : pollers.values()) { if (poller.isRunning()) { jqmlogger.trace("At least the poller on queue " + poller.getQueue().getName() + " is still running and prevents shutdown"); return; }/*from w ww . j av a 2 s. c o m*/ } if (hasEnded) { return; } jqmlogger.trace("The engine should end with the latest poller"); hasEnded = true; // If here, all pollers are down. Stop Jetty too this.server.stop(); // Also stop the internal poller this.intPoller.stop(); // Reset the stop counter - we may want to restart one day EntityManager em = null; try { em = Helpers.getNewEm(); em.getTransaction().begin(); this.node = em.find(Node.class, this.node.getId(), LockModeType.PESSIMISTIC_WRITE); this.node.setStop(false); this.node.setLastSeenAlive(null); em.getTransaction().commit(); } catch (Exception e) { // Shutdown exception is ignored (happens during tests) } finally { Helpers.closeQuietly(em); } // JMX if (loadJmxBeans) { try { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); mbs.unregisterMBean(name); jqmlogger.trace("unregistered bean " + name); } catch (Exception e) { jqmlogger.error("Could not unregister engine JMX bean", e); } } // Note: if present, the JMX listener is not stopped as it is JVM-global, like the JNDI context // Done this.ended.release(); jqmlogger.info("JQM engine has stopped"); }
From source file:org.eclipse.jubula.client.core.persistence.ProjectPM.java
/** * delete a project/*w w w .ja va 2 s .c om*/ * * @param proj * project to delete * @param isActProject * flag to label the actual project * @throws PMAlreadyLockedException * if project is already locked in db * @throws PMDirtyVersionException * if project to delete is modified in the meantime * @throws JBException * if a session cannot closed * @throws PMExtProjDeletedException * if a project (but not the current) was deleted by another * user * @throws ProjectDeletedException * if the current project was deleted by another user * @throws InterruptedException * if the operation was canceled */ public static void deleteProject(IProjectPO proj, boolean isActProject) throws PMDirtyVersionException, PMAlreadyLockedException, PMExtProjDeletedException, ProjectDeletedException, JBException, InterruptedException { Validate.notNull(proj, "Project to delete is null"); //$NON-NLS-1$ EntityManager deleteSess = null; IProjectPO p = null; final Long projId = proj.getId(); try { if (isActProject) { EntityManager s = GeneralStorage.getInstance().getMasterSession(); IProjectPO currProj = s.find(NodeMaker.getProjectPOClass(), projId, LockModeType.READ); if (currProj == null) { throw new ProjectDeletedException(Messages.ProjectWasDeleted, MessageIDs.E_CURRENT_PROJ_DEL); } } } catch (PersistenceException e) { handleDBExceptionForMasterSession(proj, e); } try { deleteSess = Persistor.instance().openSession(); EntityTransaction tx = Persistor.instance().getTransaction(deleteSess); p = deleteSess.find(NodeMaker.getProjectPOClass(), projId); if (p == null) { if (isActProject) { throw new ProjectDeletedException("Current Project was deleted", //$NON-NLS-1$ MessageIDs.E_CURRENT_PROJ_DEL); } throw new PMExtProjDeletedException(Messages.ProjectWasDeleted + StringConstants.DOT, MessageIDs.E_DELETED_OBJECT); } Persistor.instance().lockPO(deleteSess, p); deleteProjectIndependentDBObjects(deleteSess, p); // FIXME zeb Workaround for EclipseLink deleting the objects in the // wrong order. Test Cases that reference Test Data Cubes // were being deleted *after* the Test Data Cubes // themselves. List<ISpecPersistable> specObjList = new ArrayList<ISpecPersistable>( p.getSpecObjCont().getSpecObjList()); List<IExecPersistable> execObjList = new ArrayList<IExecPersistable>( p.getExecObjCont().getExecObjList()); for (ISpecPersistable po : specObjList) { PersistenceUtil.removeChildNodes(po, deleteSess); p.getSpecObjCont().removeSpecObject(po); Persistor.instance().deletePO(deleteSess, po); } for (IExecPersistable po : execObjList) { PersistenceUtil.removeChildNodes(po, deleteSess); p.getExecObjCont().removeExecObject(po); Persistor.instance().deletePO(deleteSess, po); } deleteSess.flush(); // FIXME zeb end workaround Persistor.instance().deletePO(deleteSess, p); CompNamePM.deleteCompNames(deleteSess, projId); Persistor.instance().commitTransaction(deleteSess, tx); tx = null; } catch (PersistenceException e) { handleDBExceptionForAnySession(p, e, deleteSess); } finally { Persistor.instance().dropSession(deleteSess); } ProjectNameBP.getInstance().checkAndDeleteName(proj.getGuid()); }