List of usage examples for javax.persistence EntityManager persist
public void persist(Object entity);
From source file:org.apache.roller.weblogger.business.jpa.JPAPersistenceStrategy.java
/** * Store object using an existing transaction. * @param obj the object to persist//from w w w. jav a2 s .co m * @return the object persisted * @throws org.apache.roller.weblogger.WebloggerException on any error */ public Object store(Object obj) throws WebloggerException { EntityManager em = getEntityManager(true); if (!em.contains(obj)) { // If entity is not managed we can assume it is new em.persist(obj); } return obj; }
From source file:org.apache.roller.planet.business.jpa.JPAPersistenceStrategy.java
/** * Store object using an existing transaction. * @param obj the object to persist//from ww w . j av a2s . co m * @return the object persisted * @throws org.apache.roller.planet.PlanetException on any error */ public Object store(Object obj) throws PlanetException { EntityManager em = getEntityManager(true); if (!em.contains(obj)) { // If entity is not managed we can assume it is new em.persist(obj); } return obj; }
From source file:com.openmeap.model.ModelTestUtils.java
static public void createModel(EntityManager em) { if (em == null) { em = createEntityManager();//from ww w . j av a2 s . co m } try { Map<String, Map<String, ? extends ModelEntity>> modelBeans = (Map<String, Map<String, ? extends ModelEntity>>) ModelTestUtils .newModelBeans().getBean("mockModel"); // we need to set all (except the Device.uuid) pk's to null, // so the entity manager doesn't flip out, thinking we've passed it // a detached entity for persistence. for (Map.Entry<String, Map<String, ? extends ModelEntity>> classes : modelBeans.entrySet()) for (ModelEntity member : classes.getValue().values()) if (!(member instanceof ApplicationInstallation)) member.setPk(null); em.getTransaction().begin(); for (String className : new String[] { "GlobalSettings", "Application", "ApplicationArchive", "ApplicationVersion", "Deployment", "ApplicationInstallation" }) { Map<String, ? extends ModelEntity> members = modelBeans.get(className); for (ModelEntity member : members.values()) { if (className.equals("Application")) { ((Application) member).setDeployments(null); } em.persist(member); em.flush(); } } em.getTransaction().commit(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:fr.mby.opa.picsimpl.dao.DbAlbumDao.java
@Override public Album createAlbum(final Album album) { Assert.notNull(album, "No Album supplied !"); Assert.isNull(album.getId(), "Id should not be set for creation !"); Assert.hasText(album.getName(), "No Album name supplied !"); new TxCallback(this.getEmf()) { @Override/*from ww w .j a v a 2s .c o m*/ protected void executeInTransaction(final EntityManager em) { final Timestamp creationTime = new Timestamp(System.currentTimeMillis()); // Persist album album.setCreationTime(creationTime); album.setLocked(false); em.persist(album); // Create initial bag final ProposalBag initialBag = new ProposalBag(); initialBag.setCommited(false); initialBag.setCreationTime(creationTime); initialBag.setName(IProposalDao.INITIAL_PROPOSAL_NAME); initialBag.setRevision("0"); em.persist(initialBag); // Create master branch final ProposalBranch masterBranch = new ProposalBranch(); masterBranch.setAlbum(album); masterBranch.setCreationTime(creationTime); masterBranch.setName(IProposalDao.MASTER_BRANCH_NAME); masterBranch.setHead(initialBag); final List<ProposalBag> bagList = new ArrayList<>(); bagList.add(initialBag); masterBranch.setBags(bagList); em.persist(masterBranch); } }; return album; }
From source file:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java
@Override public boolean create(OfferBid something) { EntityManager em = entityManagerFactory.createEntityManager(); boolean success = false; try {//from w ww . j a va2 s .c om em.getTransaction().begin(); em.persist(something); em.getTransaction().commit(); success = true; } catch (RuntimeException e) { Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e); success = false; } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } return success; }
From source file:cz.muni.fi.dndtroops.test.TroopDaoImplTest.java
@Test public void testUpdateGoldForTroop() { EntityManager entityManager = emf.createEntityManager(); entityManager.getTransaction().begin(); Troop troopC = new Troop(); troopC.setName("Testers"); troopC.setMoney(new BigDecimal("30")); entityManager.persist(troopC); entityManager.getTransaction().commit(); entityManager.close();/* w w w. j a v a 2s . co m*/ troopDao.updateGoldForTroop(troopC.getId(), new BigDecimal("5")); Troop t1 = troopDao.findTroopById(troopC.getId()); Assert.assertEquals(t1.getMoney().compareTo(new BigDecimal("5")), 0); }
From source file:com.doculibre.constellio.wicket.session.ConstellioSession.java
/** * Can't override Session#setLocale(Locale) classe Session. *//*from ww w . java2 s.c o m*/ public void changeLocale(Locale locale) { ConstellioUser user = (ConstellioUser) getUser(); if (user == null) { setLocale(locale); } else { EntityManager entityManager = ConstellioPersistenceContext.getCurrentEntityManager(); if (!entityManager.getTransaction().isActive()) { entityManager.getTransaction().begin(); } user.setLocale(locale); entityManager.persist(user); entityManager.getTransaction().commit(); } }
From source file:nl.b3p.kaartenbalie.struts.WMSUrlCreatorAction.java
public ActionForward getMapUrl(ActionMapping mapping, DynaValidatorForm dynaForm, HttpServletRequest request, HttpServletResponse response) throws Exception { if (!isTokenValid(request)) { prepareMethod(dynaForm, request, EDIT, LIST); addAlternateMessage(mapping, request, TOKEN_ERROR_KEY); return getAlternateForward(mapping, request); }//w w w . j a va 2 s. co m ActionErrors errors = dynaForm.validate(mapping, request); if (!errors.isEmpty()) { super.addMessages(request, errors); prepareMethod(dynaForm, request, EDIT, LIST); addAlternateMessage(mapping, request, VALIDATION_ERROR_KEY); return getAlternateForward(mapping, request); } //Create the String String[] layers = (String[]) dynaForm.get("sortedLayers"); String projectie = (String) dynaForm.get("selectedProjectie"); String bbox = (String) dynaForm.get("bbox"); Integer height = (Integer) dynaForm.get("height"); Integer width = (Integer) dynaForm.get("width"); String format = (String) dynaForm.get("selectedFormat"); String pUrl = (String) dynaForm.get("personalUrl"); if (layers.length == 0) { prepareMethod(dynaForm, request, LIST, LIST); addAlternateMessage(mapping, request, NO_LAYERS_SELECTED_ERROR_KEY); return getAlternateForward(mapping, request); } /* * Get the URL to start with */ User user = (User) request.getUserPrincipal(); if (user == null) { prepareMethod(dynaForm, request, LIST, LIST); addAlternateMessage(mapping, request, UNKNOWN_SES_USER_ERROR_KEY); return getAlternateForward(mapping, request); } StringBuffer layerString = new StringBuffer(); for (int i = 0; i < layers.length; i++) { layerString.append(layers[i]); layerString.append(","); } String layer = layerString.substring(0, layerString.lastIndexOf(",")); StringBuffer getMapUrl = new StringBuffer(user.getPersonalURL(request)); getMapUrl.append("?"); getMapUrl.append(OGCConstants.WMS_SERVICE); getMapUrl.append("="); getMapUrl.append(OGCConstants.WMS_SERVICE_WMS); getMapUrl.append("&"); getMapUrl.append(OGCConstants.WMS_VERSION); getMapUrl.append("="); getMapUrl.append(OGCConstants.WMS_VERSION_111); getMapUrl.append("&"); getMapUrl.append(OGCConstants.WMS_REQUEST); getMapUrl.append("="); getMapUrl.append(OGCConstants.WMS_REQUEST_GetMap); getMapUrl.append("&"); getMapUrl.append(OGCConstants.WMS_PARAM_LAYERS); getMapUrl.append("="); getMapUrl.append(layer); getMapUrl.append("&"); getMapUrl.append(OGCConstants.WMS_PARAM_BBOX); getMapUrl.append("="); getMapUrl.append(bbox); getMapUrl.append("&"); getMapUrl.append(OGCConstants.WMS_PARAM_SRS); getMapUrl.append("="); getMapUrl.append(projectie); getMapUrl.append("&"); getMapUrl.append(OGCConstants.WMS_PARAM_HEIGHT); getMapUrl.append("="); getMapUrl.append(height); getMapUrl.append("&"); getMapUrl.append(OGCConstants.WMS_PARAM_WIDTH); getMapUrl.append("="); getMapUrl.append(width); getMapUrl.append("&"); getMapUrl.append(OGCConstants.WMS_PARAM_FORMAT); getMapUrl.append("="); getMapUrl.append(format); getMapUrl.append("&"); getMapUrl.append(OGCConstants.WMS_PARAM_BGCOLOR); getMapUrl.append("="); getMapUrl.append("0xF0F0F0"); getMapUrl.append("&"); getMapUrl.append(OGCConstants.WMS_PARAM_EXCEPTIONS); getMapUrl.append("="); getMapUrl.append(OGCConstants.WMS_PARAM_EXCEPTION_INIMAGE); getMapUrl.append("&"); getMapUrl.append(OGCConstants.WMS_PARAM_STYLES); getMapUrl.append("="); user.setDefaultGetMap(getMapUrl.toString()); log.debug("Getting entity manager ......"); EntityManager em = getEntityManager(); if (user.getId() == null) { em.persist(user); } else { em.merge(user); } em.flush(); populateForm(getMapUrl.toString(), dynaForm, request); prepareMethod(dynaForm, request, LIST, EDIT); addDefaultMessage(mapping, request, ACKNOWLEDGE_MESSAGES); return getDefaultForward(mapping, request); }
From source file:cz.muni.fi.dndtroops.test.TroopDaoImplTest.java
@Test public void testAssignMissionToTroop() { EntityManager entityManager = emf.createEntityManager(); entityManager.getTransaction().begin(); Troop troopC = new Troop(); troopC.setName("Testers"); troopC.setMoney(new BigDecimal("30")); entityManager.persist(troopC); entityManager.getTransaction().commit(); entityManager.close();//from ww w . j av a2 s . co m troopDao.assignMissionToTroop(troopC.getId(), "mise C"); Troop t1 = troopDao.findTroopById(troopC.getId()); Assert.assertEquals(t1.getMission(), "mise C"); troopDao.assignMissionToTroop(troopC.getId(), null); Troop t2 = troopDao.findTroopById(troopC.getId()); Assert.assertEquals(t2.getMission(), null); }
From source file:com.chiralbehaviors.CoRE.location.Location.java
@Override public void link(Relationship r, Location child, Agency updatedBy, Agency inverseSoftware, EntityManager em) { LocationNetwork link = new LocationNetwork(this, r, child, updatedBy); em.persist(link); LocationNetwork inverse = new LocationNetwork(child, r.getInverse(), this, inverseSoftware); em.persist(inverse);/*from w w w . j a v a2 s. c o m*/ }