List of usage examples for org.hibernate Session flush
void flush() throws HibernateException;
From source file:acceptance.hibernate.HibernateReferenceTest.java
License:Open Source License
/** * Create the object within a Hibernate session and persist it. *//*from w w w . ja v a 2s .c o m*/ private Division setupPersistentDivision() { final Session session = getSessionFactory().getCurrentSession(); session.beginTransaction(); final Division div = new Division("Div1"); final Department dep = new Department("Dep1", div); final Site site = new Site("Site1"); /* * This save is necessitated by the fact that Hibernate's transitive persistence is * depth-first and does not do a full graph analysis. Therefore it would be possible for * Hibernate to try to save the person record before the site record, which would throw * an error if the person.site FK is non-nullable. */ session.save(site); new Person("Tom", dep, site); session.save(div); session.flush(); session.getTransaction().commit(); return div; }
From source file:angeldx.manager.EstudianteManager.java
public List<Estudiante> listar() { List<Estudiante> list = new ArrayList<Estudiante>(); Session session = HibernateUtil.openSession(); Transaction tran = null;//from w w w. j av a 2 s . com try { tran = session.getTransaction(); tran.begin(); list = session.createQuery("from Estudiante").list(); //from Estudiante de llama de la persitencia de datos (to) tran.commit(); } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return list; }
From source file:angeldx.manager.EstudianteManager.java
public boolean registrar(Estudiante est) { Session session = HibernateUtil.openSession(); Transaction tran = null;// www. j a va 2 s. com try { tran = session.getTransaction(); tran.begin(); session.saveOrUpdate(est); tran.commit(); } catch (Exception e) { if (tran != null) { tran.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } return true; }
From source file:angeldx.manager.EstudianteManager.java
public Estudiante buscarPorId(int id) { Estudiante est = null;/*from w w w .j a va 2s. c o m*/ Transaction tran = null; Session session = HibernateUtil.openSession(); try { tran = session.beginTransaction(); String queryString = "from Estudiante where id = :id"; Query query = session.createQuery(queryString); query.setInteger("id", id); est = (Estudiante) query.uniqueResult(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return est; }
From source file:angeldx.manager.EstudianteManager.java
public boolean eliminar(int id) { Session session = HibernateUtil.openSession(); Transaction tx = null;/* w w w. j a v a2 s. c om*/ try { tx = session.beginTransaction(); Estudiante est = (Estudiante) session.load(Estudiante.class, new Integer(id)); session.delete(est); tx.commit(); } catch (RuntimeException e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } return true; }
From source file:app.service.UserService.java
License:Open Source License
public void doStuff() { Session session = Db.getSession(); session.beginTransaction();/*from ww w . ja v a 2 s .co m*/ User user = new User(new Person("Pascal", "Mazars"), "pma", "cacaprout"); System.out.println("Hello " + user); ID<User> id = user.save(); System.out.println("Persisted with id " + id); System.out.println( "Persisted with id " + new User(new Person("Daddy", "Longleg"), "dlg", "cacaprout").save()); // User foundUser = UserFinder.getInstance().findUnique("from app.core.User where person.firstName = 'Pascal'"); // System.out.println("Found unique with firstName = 'Pascal': " + foundUser.toString()); // User foundUser2 = new IDFinder<User>().get(id); // System.out.println("Found with id " + id + ": " + foundUser2.toString()); //foundUser2.getPerson().setDateOfBirth(new Date()); // User foundUser3 = foundUser2.getPerson().getUser(); // System.out.println("Found with getPerson().getUser(): " + foundUser3.toString()); List<User> userList = User.findAll(); //find("from app.core.User"); System.out.println("List<app.core.User>: \n" + ORMUtils.toResultString(userList)); System.out.println("Db dump: " + Db.dump()); session.flush(); throw ApplicationError.somethingWentTerriblyWrong(); }
From source file:appointment.data.ObjectFactory.java
public void deleteResource(Object cannedObject) { Session session = factory.openSession(); Transaction tx = session.beginTransaction(); session.delete(cannedObject);/*w w w . ja va 2 s . co m*/ session.flush(); tx.commit(); }
From source file:ar.com.zauber.commons.repository.closures.OpenSessionClosure.java
License:Apache License
/** @see Closure#execute(Object) */ public final void execute(final T t) { if (dryrun) { //permite evitar que se hagan commit() en el ambiente de test target.execute(t);/*from w w w .j ava 2s. co m*/ } else { final Session session = SessionFactoryUtils.getSession(sessionFactory, true); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); Transaction transaction = null; try { transaction = session.beginTransaction(); target.execute(t); session.flush(); transaction.commit(); } catch (final Throwable e) { if (transaction != null && transaction.isActive()) { transaction.rollback(); } throw new UnhandledException(e); } finally { TransactionSynchronizationManager.unbindResource(sessionFactory); SessionFactoryUtils.closeSession(session); } } }
From source file:au.com.nicta.ct.db.CtKeyValueProperties.java
License:Open Source License
public static void setValue(String key, String value) { CtProperties p = find(key);//w ww.ja v a2 s . c om if (p == null) { persist(key, value); // will cache return; } //x Session s = CtSession.Current(); Session s = CtSession.Create(); try { p.setValue(value); s.saveOrUpdate(p); s.flush(); } catch (Exception e) { } finally { s.close(); } if (useReadCaching) { od.put(key, value); } }
From source file:au.com.nicta.ct.experiment.setup.CtDBProgressDialog.java
License:Open Source License
private void initTask() { if (task != null) { task = null;//from ww w. j a va 2 s . c o m } task = new CtDatabaseWorker(setupModel.experimentName, setupModel.experimentURI, setupModel.imageSourceDirectory); progressBar.setMaximum(100); setTitle("Copying data ... "); //t = s.getTransaction(); // start a timer to watch the task and update the progress taskTimer = new Timer(CtDBProgressDialog._updateInterval, new ActionListener() { public void actionPerformed(ActionEvent event) { //not very elegant way of doing things, but ... if (taskCounter == 0) { Transaction t = null; try { Session s = CtSession.Current(); t = s.getTransaction(); t.begin(); System.out.println("Experiment : " + t.isActive()); task.saveExperiment(s); taskCounter++; progressBar.setValue(10); statusLabel.setText(" Copying Image data"); System.out.println("Saving of Experiment Finished"); } catch (HibernateException he) { if (t != null) { t.rollback(); } he.printStackTrace(); //in this case we need to delete the experiment folder we created String experimentURI = CtApplication.experimentsPath() + File.separator + setupModel.experimentName; File destDir = new File(experimentURI); destDir.delete(); javax.swing.JOptionPane.showMessageDialog(containerFrame, "Could not save all data in the database", "Report", javax.swing.JOptionPane.OK_OPTION); setupWorker.setdataSaveResult(false); windowAction(CMD_DONE); } } else if (taskCounter == 1) { System.out.println("Creating Images"); String result = task.createImages(setupModel.imageFileNames); progressBar.setValue(25); System.out.println("Creating Coordinates"); result = task.createCoordinatesImagesCoordinates(setupModel.imageNameParts); progressBar.setValue(75); System.out.println("Result String : " + result); if (result.equals(CtMissingCoordinateDetector._missingImageMessage)) { int userInput = javax.swing.JOptionPane.showConfirmDialog(containerFrame, result + ".Continue creating the experiment?", "Warning", javax.swing.JOptionPane.YES_NO_OPTION); if (userInput == javax.swing.JOptionPane.NO_OPTION) { String experimentURI = CtApplication.experimentsPath() + File.separator + setupModel.experimentName; File destDir = new File(experimentURI); destDir.delete(); try { //somehow the rollback does not work //so taking things in hand, deleting the //created experiment manually. //May sound trouble for later saving operations //if rollback really does not work Session s = CtSession.Current(); s.delete(task.solution); s.delete(task.experiment); //s.delete( task.experiment.getCtSolutionses() ); //t = s.getTransaction(); //t.rollback(); } catch (HibernateException heInner) { } //in this case we need to delete the experiment folder we created setupWorker.setdataSaveResult(false); windowAction(CMD_DONE); } else { //if the user wants to continue with the missing //images, the dummy image name and corresponding //coordinates are to be added to the list task.addMissingImagesANDImagesCoordinates(setupModel.imageNameParts); } } taskCounter++; progressBar.setValue(20); statusLabel.setText(" Copying Image data"); } else if (taskCounter == 2) { Transaction t = null; try { Session s = CtSession.Current(); t = s.getTransaction(); t.begin(); task.saveImages(s); progressBar.setValue(85); statusLabel.setText(" Copying Coordinates "); task.saveCoordinatesImagesCoordinates(s); taskCounter++; progressBar.setValue(85); statusLabel.setText(" Setting coordinate limits "); CtUsers u = (CtUsers) CtObjectDirectory.get("user"); Set ugs = u.getCtUsersGroupses(); for (Object o : ugs) { CtUsersGroups ug = (CtUsersGroups) o; CtGroupsExperiments ge = new CtGroupsExperiments(); ge.setCtGroups(ug.getCtGroups()); ge.setCtExperiments(task.experiment); CtSession.Current().save(ge); } // determine the limits for the experiment: // CtLimits l = new CtLimits(); // l.updateLimits( task.experiment ); t.commit(); s.flush(); // s.flush(); setupWorker.setExperiment(task.experiment); progressBar.setValue(100); statusLabel.setText("Done"); setupWorker.setdataSaveResult(true); } catch (HibernateException he) { //in this case we need to delete the experiment folder we created String experimentURI = CtApplication.experimentsPath() + File.separator + setupModel.experimentName; File destDir = new File(experimentURI); destDir.delete(); he.printStackTrace(); Session s = CtSession.Current(); // t = s.getTransaction(); System.out.println("Exception : " + t.isActive()); if (t != null) { t.rollback(); } javax.swing.JOptionPane.showMessageDialog(containerFrame, "Could not save all data in the database", "Report", javax.swing.JOptionPane.OK_OPTION); setupWorker.setdataSaveResult(false); windowAction(CMD_DONE); } } if (progressBar.getValue() >= 100) { setupWorker.setdataSaveResult(true); windowAction(CMD_DONE); } } }); taskTimer.setInitialDelay(0); taskTimer.start(); }