List of usage examples for org.hibernate Session refresh
void refresh(Object object);
From source file:hibernate.CatetoryAndArticleTest.java
License:Open Source License
public void testCategoryAndArticle() { Session session = sessionFactory.openSession(); session.beginTransaction();/* w w w .j a v a2 s .c om*/ Category category = new Category(); category.setCatName(""); category.setCreateDate(new Date()); session.save(category);// ?sql insert??? assertNotNull("??Category???", category.getCatId()); // session.persist(category);// ?sql insert??flush?? Article article = new Article(); // ? // hibernate??? article.setCategory(category); article.setTitle(""); article.setContent(""); article.setCreateDate(new Date()); article.setModifyDate(new Date()); article.setSummary("?"); article.setVisitCount(0); article.setIsPublished(Boolean.TRUE); article.setType("post"); article.setCommentStatus("open"); article.setArticleStatus("public"); article.setLink("http://localhost:8080/newblog/post/abce"); session.save(article); assertNotNull("??Article???", article.getArticleId()); // ? // category.getArticles().add(article); // session.update(category); Query createQuery = session.createQuery("from Category c where c.catId = :catId"); createQuery.setParameter("catId", category.getCatId()); Category cat = (Category) createQuery.uniqueResult(); // hibernate??? session.refresh(cat); assertEquals(cat.getCatId(), category.getCatId()); assertEquals(cat.getCatName(), category.getCatName()); assertEquals(cat.getCreateDate(), category.getCreateDate()); logger.debug("" + cat.getCatId()); logger.debug("??" + cat.getCatName()); logger.debug(""); List<Article> articles = cat.getArticles(); for (Article bean : articles) { logger.debug("------------"); logger.debug("" + bean.getArticleId()); logger.debug("" + bean.getTitle()); logger.debug("" + bean.getContent()); logger.debug("------------"); } for (Article bean : cat.getArticles()) { session.delete(bean); // ?? } session.delete(cat); session.getTransaction().commit(); session.close(); }
From source file:hibernate.TagAndArticleTest.java
License:Open Source License
public void testTagAndArticle() { Session session = sessionFactory.openSession(); session.beginTransaction();// w w w. j av a2 s .c o m Tag tag = new Tag(); tag.setTagName(""); tag.setCreateDate(new Date()); session.save(tag); assertNotNull("??Tag???", tag.getTagId()); Tag tag1 = new Tag(); tag1.setTagName(""); tag1.setCreateDate(new Date()); session.save(tag1); assertNotNull("??Tag???", tag1.getTagId()); Tag tag2 = new Tag(); tag2.setTagName("?"); tag2.setCreateDate(new Date()); session.save(tag2); assertNotNull("??Tag???", tag2.getTagId()); Tag tag3 = new Tag(); tag3.setTagName(""); tag3.setCreateDate(new Date()); session.save(tag3); assertNotNull("??Tag???", tag3.getTagId()); Article article = new Article(); article.setTitle(""); article.setContent(""); article.setCreateDate(new Date()); article.setModifyDate(new Date()); article.setSummary("?"); article.setVisitCount(0); article.setIsPublished(Boolean.TRUE); article.setType("post"); article.setCommentStatus("open"); article.setArticleStatus("public"); article.setLink("http://localhost:8080/newblog/post/abce"); Set<Tag> ts = article.getTags(); ts.add(tag); ts.add(tag1); ts.add(tag2); ts.add(tag3); session.save(article); assertNotNull("??Article???", article.getArticleId()); article = new Article(); article.setTitle("2"); article.setContent("2"); article.setCreateDate(new Date()); article.setModifyDate(new Date()); article.setSummary("?2"); article.setVisitCount(0); article.setIsPublished(Boolean.TRUE); article.setType("post"); article.setCommentStatus("open"); article.setArticleStatus("public"); article.setLink("http://localhost:8080/newblog/post/abce2"); ts = article.getTags(); ts.add(tag); ts.add(tag1); ts.add(tag2); ts.add(tag3); session.save(article); assertNotNull("??Article???", article.getArticleId()); session.getTransaction().commit(); session.close(); session = sessionFactory.openSession(); session.beginTransaction(); List<Article> articles = session.createQuery( "select p from Article p left join fetch p.tags t where t.tagName = :tagName order by p.createDate desc") .setParameter("tagName", tag.getTagName()).list(); logger.debug("'" + tag.getTagName() + "'?:"); for (Article bean : articles) { session.refresh(bean); logger.debug("------------------------"); logger.debug("" + bean.getArticleId()); logger.debug("" + bean.getTitle()); logger.debug(""); for (Tag t : bean.getTags()) { logger.debug(t.getTagName()); } logger.debug("------------------------"); } // hibernatesql?doReturningWork session.doWork(new Work() { @Override public void execute(Connection connection) throws SQLException { Statement stmt = connection.createStatement(); stmt.executeUpdate("delete from tag_article"); stmt.executeUpdate("delete from article"); stmt.executeUpdate("delete from tag"); stmt.close(); } }); session.getTransaction().commit(); session.close(); }
From source file:io.jeandavid.projects.vod.service.AuthorFacadeREST.java
License:Open Source License
@POST @Path("{id}/dvd") @Consumes(MediaType.APPLICATION_JSON)/*from w ww. j a va 2 s. co m*/ public void addDvd(@PathParam("id") Long id, Dvd dvd) { Author author = super.find(id); Session session = this.getSessionFactory().openSession(); Transaction tr = session.beginTransaction(); session.refresh(dvd); author.addDvd(dvd); session.flush(); tr.commit(); session.close(); }
From source file:io.jeandavid.projects.vod.service.DirectorFacadeREST.java
License:Open Source License
@POST @Path("{id}/dvd") @Consumes(MediaType.APPLICATION_JSON)// ww w . j a v a2 s . co m public void addDvd(@PathParam("id") Long id, Dvd dvd) { Director director = super.find(id); Session session = this.getSessionFactory().openSession(); Transaction tr = session.beginTransaction(); session.refresh(dvd); director.addDvd(dvd); session.flush(); tr.commit(); session.close(); }
From source file:io.jeandavid.projects.vod.service.DvdOrderFacadeREST.java
License:Open Source License
@POST @Path("{id}/dvd") @Consumes(MediaType.APPLICATION_JSON)//from w w w. java 2 s . com public void addDvd(@PathParam("id") Long id, Dvd dvd) { Session session = this.getSessionFactory().openSession(); DvdOrder order = (DvdOrder) session.load(DvdOrder.class, id, LockMode.PESSIMISTIC_WRITE); if (order.getInternalState() != DvdOrder.CREATED) { session.close(); return; } int quantity = dvd.getQuantity(); session.refresh(dvd); Transaction tr = session.beginTransaction(); session.refresh(order); DvdOrderDvd temp = new DvdOrderDvd(); temp.setQuantity(quantity); session.persist(temp); dvd.addDvdOrderDvd(temp); order.addDvdOrderDvd(temp); session.saveOrUpdate(temp); session.saveOrUpdate(dvd); float price = temp.computePrice(); temp.setPrice(price); session.saveOrUpdate(temp); order.updatePrice(order.getPrice() + price); session.save(session.merge(order)); session.flush(); tr.commit(); session.close(); }
From source file:io.jeandavid.projects.vod.service.DvdOrderFacadeREST.java
License:Open Source License
@Asynchronous public void transformIntoSubOrders(DvdOrder dvdOrder) { Session rootSession = em.unwrap(Session.class); rootSession.refresh(dvdOrder); HashMap<DvdProvider, List<Dvd>> split = dvdOrder.sortByDvdProvider(); for (Entry<DvdProvider, List<Dvd>> entry : split.entrySet()) { Session session = this.getSessionFactory().openSession(); Transaction tr = session.beginTransaction(); DvdOrder subOrder = new DvdOrder(); subOrder.switchInternalState(DvdOrder.PENDING); session.persist(subOrder);//from w ww .ja v a2 s. com subOrder.setDvdProvider(entry.getKey()); dvdOrder.addSubOrder(subOrder); for (Dvd dvd : entry.getValue()) { for (DvdOrderDvd dvdOrderDvd : new HashSet<>(dvdOrder.getDvdOrderDvds())) { if (dvdOrderDvd.getDvd().equals(dvd)) { subOrder.addDvdOrderDvd(dvdOrderDvd); session.saveOrUpdate(session.merge(dvdOrderDvd)); dvdOrder.getDvdOrderDvds().remove(dvdOrderDvd); rootSession.save(dvdOrder); } } } subOrder.computePrice(); session.persist(subOrder); session.flush(); tr.commit(); session.close(); doThePackaging(subOrder); } this.refreshParentDvdOrderStatus(dvdOrder); }
From source file:io.jeandavid.projects.vod.service.DvdOrderFacadeREST.java
License:Open Source License
public void doThePackaging(DvdOrder dvdOrder) { if (dvdOrder.getInternalState() != DvdOrder.PENDING) return;/*from w ww . j a v a 2s. c o m*/ boolean pending = false; Session session = this.getSessionFactory().openSession(); session.refresh(dvdOrder); dvdOrder.switchInternalState(DvdOrder.PACKAGED); Transaction tr = session.beginTransaction(); TreeSet<DvdOrderDvd> sortedDvdOrderDvds = dvdOrder.getSortedDvdOrderDvds(); for (DvdOrderDvd dvdOrderDvd : sortedDvdOrderDvds) { Dvd dvd = (Dvd) session.load(Dvd.class, dvdOrderDvd.getDvd().getId()); LockRequest lockRequest = session.buildLockRequest(LockOptions.UPGRADE); lockRequest.lock(dvd); Integer occurenciesNumber = dvdOrderDvd.getQuantity(); if (dvd.getQuantity() >= occurenciesNumber) { dvd.setQuantity(dvd.getQuantity() - occurenciesNumber); session.saveOrUpdate(session.merge(dvd)); } else { pending = true; break; } } if (!pending) { session.saveOrUpdate(dvdOrder); session.flush(); tr.commit(); } session.close(); }
From source file:io.jeandavid.projects.vod.service.DvdOrderFacadeREST.java
License:Open Source License
@Asynchronous public void refreshParentDvdOrderStatus(DvdOrder dvdOrder) { if (dvdOrder.getParentDvdOrder() != null) return;/*w w w. j a va 2s . c om*/ Session session = this.getSessionFactory().openSession(); Transaction tr = session.beginTransaction(); session.refresh(dvdOrder); dvdOrder.switchInternalState(DvdOrder.PACKAGED); for (DvdOrder subOrder : dvdOrder.getSubDvdOrders()) { session.refresh(subOrder); if (subOrder.getInternalState() == DvdOrder.PENDING) { dvdOrder.switchInternalState(DvdOrder.PENDING); break; } } session.flush(); tr.commit(); session.close(); }
From source file:io.jeandavid.projects.vod.service.DvdProviderFacadeREST.java
License:Open Source License
@POST @Path("{id}/dvd") @Consumes(MediaType.APPLICATION_JSON)//from ww w.j a v a 2 s . co m public void addDvd(@PathParam("id") Long id, Dvd dvd) { DvdProvider provider = super.find(id); Session session = this.getSessionFactory().openSession(); Transaction tr = session.beginTransaction(); session.refresh(dvd); provider.addDvd(dvd); session.flush(); tr.commit(); session.close(); }
From source file:it.eng.spagobi.analiticalmodel.document.dao.BIObjectDAOHibImpl.java
License:Mozilla Public License
/** * Erase bi object./* w w w . j a v a 2s. co m*/ * * @param obj the obj * @param idFunct the id funct * * @throws EMFUserError the EMF user error * * @see it.eng.spagobi.analiticalmodel.document.dao.IBIObjectDAO#eraseBIObject(it.eng.spagobi.analiticalmodel.document.bo.BIObject, java.lang.Integer) */ public void eraseBIObject(BIObject obj, Integer idFunct) throws EMFUserError { logger.debug("IN"); Session aSession = null; Transaction tx = null; try { aSession = getSession(); tx = aSession.beginTransaction(); // load object SbiObjects hibBIObject = (SbiObjects) aSession.load(SbiObjects.class, obj.getId()); // erase object from functionalities Set hibObjFuncs = hibBIObject.getSbiObjFuncs(); Iterator itObjFunc = hibObjFuncs.iterator(); while (itObjFunc.hasNext()) { SbiObjFunc aSbiObjFunc = (SbiObjFunc) itObjFunc.next(); if (idFunct == null || aSbiObjFunc.getId().getSbiFunctions().getFunctId().intValue() == idFunct.intValue()) { logger.debug("Deleting object [" + obj.getName() + "] from folder [" + aSbiObjFunc.getId().getSbiFunctions().getPath() + "]"); aSession.delete(aSbiObjFunc); } } aSession.flush(); // reload object aSession.refresh(hibBIObject); // if the object is no more referenced in any folder, erases it from sbi_obejcts table hibObjFuncs = hibBIObject.getSbiObjFuncs(); if (hibObjFuncs == null || hibObjFuncs.size() == 0) { logger.debug("The object [" + obj.getName() + "] is no more referenced by any functionality. It will be completely deleted from db."); // delete templates String hql = "from SbiObjTemplates sot where sot.sbiObject.biobjId=" + obj.getId(); Query query = aSession.createQuery(hql); List templs = query.list(); Iterator iterTempls = templs.iterator(); while (iterTempls.hasNext()) { SbiObjTemplates hibObjTemp = (SbiObjTemplates) iterTempls.next(); SbiBinContents hibBinCont = hibObjTemp.getSbiBinContents(); aSession.delete(hibObjTemp); aSession.delete(hibBinCont); } //delete subobjects eventually associated ISubObjectDAO subobjDAO = DAOFactory.getSubObjectDAO(); List subobjects = subobjDAO.getSubObjects(obj.getId()); for (int i = 0; i < subobjects.size(); i++) { SubObject s = (SubObject) subobjects.get(i); //subobjDAO.deleteSubObject(s.getId()); subobjDAO.deleteSubObjectSameConnection(s.getId(), aSession); } //delete viewpoints eventually associated List viewpoints = new ArrayList(); IViewpointDAO biVPDAO = DAOFactory.getViewpointDAO(); viewpoints = biVPDAO.loadAllViewpointsByObjID(obj.getId()); for (int i = 0; i < viewpoints.size(); i++) { Viewpoint vp = (Viewpoint) viewpoints.get(i); biVPDAO.eraseViewpoint(vp.getVpId()); } //delete snapshots eventually associated ISnapshotDAO snapshotsDAO = DAOFactory.getSnapshotDAO(); List snapshots = snapshotsDAO.getSnapshots(obj.getId()); for (int i = 0; i < snapshots.size(); i++) { Snapshot aSnapshots = (Snapshot) snapshots.get(i); snapshotsDAO.deleteSnapshot(aSnapshots.getId()); } //delete notes eventually associated IObjNoteDAO objNoteDAO = DAOFactory.getObjNoteDAO(); objNoteDAO.eraseNotes(obj.getId()); //delete metadata eventually associated List metadata = DAOFactory.getObjMetadataDAO().loadAllObjMetadata(); IObjMetacontentDAO objMetaContentDAO = DAOFactory.getObjMetacontentDAO(); if (metadata != null && !metadata.isEmpty()) { Iterator it = metadata.iterator(); while (it.hasNext()) { ObjMetadata objMetadata = (ObjMetadata) it.next(); ObjMetacontent objMetacontent = (ObjMetacontent) DAOFactory.getObjMetacontentDAO() .loadObjMetacontent(objMetadata.getObjMetaId(), obj.getId(), null); if (objMetacontent != null) { objMetaContentDAO.eraseObjMetadata(objMetacontent); } } } // delete parameters associated // before deleting parameters associated is needed to delete all dependencies, // otherwise in case there could be error if is firstly deleted a parameter from wich some else is dependant // (thought priority parameter is not costraining dependencies definition) Set objPars = hibBIObject.getSbiObjPars(); Iterator itObjParDep = objPars.iterator(); BIObjectParameterDAOHibImpl objParDAO = new BIObjectParameterDAOHibImpl(); while (itObjParDep.hasNext()) { SbiObjPar aSbiObjPar = (SbiObjPar) itObjParDep.next(); BIObjectParameter aBIObjectParameter = new BIObjectParameter(); aBIObjectParameter.setId(aSbiObjPar.getObjParId()); objParDAO.eraseBIObjectParameterDependencies(aBIObjectParameter, aSession); } Iterator itObjPar = objPars.iterator(); while (itObjPar.hasNext()) { SbiObjPar aSbiObjPar = (SbiObjPar) itObjPar.next(); BIObjectParameter aBIObjectParameter = new BIObjectParameter(); aBIObjectParameter.setId(aSbiObjPar.getObjParId()); objParDAO.eraseBIObjectParameter(aBIObjectParameter, aSession, false); } // delete dossier temp parts eventually associated IDossierPartsTempDAO dptDAO = DAOFactory.getDossierPartsTempDAO(); dptDAO.eraseDossierParts(obj.getId()); // delete dossier presentations eventually associated IDossierPresentationsDAO dpDAO = DAOFactory.getDossierPresentationDAO(); dpDAO.deletePresentations(obj.getId()); // update subreports table ISubreportDAO subrptdao = DAOFactory.getSubreportDAO(); subrptdao.eraseSubreportByMasterRptId(obj.getId()); subrptdao.eraseSubreportBySubRptId(obj.getId()); // delete object aSession.delete(hibBIObject); logger.debug("OUT"); } // commit all changes tx.commit(); } catch (HibernateException he) { logger.error(he); if (tx != null && tx.isActive()) tx.rollback(); throw new EMFUserError(EMFErrorSeverity.ERROR, 100); } catch (Exception ex) { logger.error(ex); if (tx != null && tx.isActive()) tx.rollback(); throw new EMFUserError(EMFErrorSeverity.ERROR, 100); } finally { if (aSession != null) { if (aSession.isOpen()) aSession.close(); } } }