List of usage examples for org.hibernate SessionFactory getCurrentSession
Session getCurrentSession() throws HibernateException;
From source file:org.xerela.server.job.backup.DeviceStatusBackupPersister.java
License:Mozilla Public License
/** {@inheritDoc} */ public void endDocument(XMLEvent xmlEvent) { if (deviceStatus.getSoftwareVendor() == null) { deviceStatus.setSoftwareVendor(deviceStatus.getHardwareVendor()); }//from w w w . ja va 2 s .c o m SessionFactory factory = CoreJobsActivator.getSessionFactory(); Session session = factory.getCurrentSession(); session.saveOrUpdate(deviceStatus); }
From source file:org.ziptie.provider.security.SecurityProvider.java
License:Open Source License
/** {@inheritDoc} */ public ZRole getRole(String role) { SessionFactory sessionFactory = SecurityProviderActivator.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); ZRole zrole = (ZRole) session.get(ZRole.class, role); if (zrole != null) { // Expand ORG_ZIPTIE_ACCESS_ALL permission into all available permissions. if (zrole.hasPermission(ORG_ZIPTIE_ACCESS_ALL)) { // We're about to change the ZRole object at runtime, we don't want these // changes persisted session.setReadOnly(zrole, true); Set<String> permissions = new HashSet<String>(); for (String perm : getAvailablePermissions()) { String[] split = perm.split("="); //$NON-NLS-1$ permissions.add(split[0]); }//from w w w . j a va 2 s . c o m zrole.setPermissionSet(permissions); } } return zrole; }
From source file:org.ziptie.server.job.backup.BackupTask.java
License:Mozilla Public License
/** {@inheritDoc} */ @Override// w ww .ja va 2 s .com protected Outcome performTask(CredentialSet credentialSet, ProtocolSet protocolSet, ConnectionPath connectionPath) throws Exception { ZDeviceCore device = getDevice(); String ipAddress = device.getIpAddress(); String adapterId = device.getAdapterId(); String deviceId = Integer.toString(device.getDeviceId()); SessionFactory sessionFactory = CoreJobsActivator.getSessionFactory(); String backupOutput = null; File modelXmlFile = null; boolean success = false; TransactionElf.beginOrJoinTransaction(); // This thread OWNS this transaction try { // Execute the backup backupOutput = AdapterEndpointElf.getEndpoint(Backup.class, adapterId).backup(connectionPath); String filename = (ipAddress + "_backup.xml").replaceAll(":+", "."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ modelXmlFile = StringElf.stringToTempFile(filename, backupOutput); backupOutput = null; Session currentSession = sessionFactory.getCurrentSession(); StaxProcessor processor = new StaxProcessor(); processor.process(device, modelXmlFile); currentSession.flush(); // Save the protocol set and credential set that were both successfully used to backup the device // and map this information to the device itself. CredentialsProvider credProvider = CoreJobsActivator.getCredentialsProvider(); credProvider.mapDeviceToProtocolSet(deviceId, protocolSet); credProvider.mapDeviceToCredentialSet(deviceId, credentialSet); success = true; return Outcome.SUCCESS; } // TODO dwhite: This is for checking to see the contents of a problematic XML related to // bug #635 (http://bugs.ziptie.org/show_bug.cgi?id=635) catch (XMLStreamException xse) { throw new XMLStreamException( xse.getMessage() + "\nProblematic XML:\n" + (backupOutput != null ? backupOutput : "null"), xse); } finally { if (modelXmlFile != null && modelXmlFile.exists()) { modelXmlFile.delete(); } if (success) { TransactionElf.commit(); } else { TransactionElf.rollback(); } } }
From source file:pl.edu.agh.toik.stockpredictor.core.impl.SimpleStockQuoteService.java
@Override @Transactional/*from w ww .j a v a 2 s . c o m*/ public List<StockQuote> getStockQuotes(ListedCompany listedCompany, Date from, Date to) { List<StockQuoteEntity> entities; List<StockQuote> result; StockQuoteDAO dao = factory.newStockQuoteDAO(); SessionFactory sf = ((HibernateDAOFactory) factory).getFactory(); sf.getCurrentSession().createCriteria(StockQuoteEntity.class).list(); entities = dao.listStockQuotes(listedCompany.getShortName(), from, to); result = new ArrayList<>(entities.size()); for (StockQuoteEntity entity : entities) { result.add(entity.toStockQuote()); } return result; }
From source file:rickbw.incubator.transaction.CloseableSession.java
License:Apache License
/** * Get a wrapper for the current session, obtained as with * {@link SessionFactory#getCurrentSession()}. * Unlike all of the other sessions returned by the factory methods of * this class, those returned by this method will not be automatically * closed. That is, {@link #close()} will do nothing. * * @throws HibernateException if an error occurs. */// w w w . jav a2s .c o m public static CloseableSession getCurrentSession(final SessionFactory sessionFactory) { final Session session = sessionFactory.getCurrentSession(); return new CloseableSession(session) { @Override public void close() { // don't close shared session } }; }
From source file:sample.DAO.AccountDao.java
public AccountDao() { SessionFactory factory = NewHibernateUtil.getSessionFactory(); this.session = factory.getCurrentSession(); }
From source file:service.DatabaseService.java
public void deleteEntity(Object entity) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); //Session session = sessionFactory.openSession(); Session session = sessionFactory.getCurrentSession(); session.setFlushMode(FlushMode.MANUAL); //ManagedSessionContext.bind(session); Transaction tx = null;/* w w w . j a v a2 s. co m*/ try { tx = session.beginTransaction(); //session.merge(entity); session.delete(entity); ManagedSessionContext.unbind(HibernateUtil.getSessionFactory()); session.flush(); tx.commit(); } catch (RuntimeException e) { tx.rollback(); throw e; } finally { session.close(); } }
From source file:Servlets.addmemberServlet.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request// w w w .j a v a 2 s . c om * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); if (request.getParameter("Submit") != null) { //gets the vales from the textboxes on the jsp page. String username = request.getParameter("username"); String userpass = request.getParameter("password"); String name = request.getParameter("name"); String adress = request.getParameter("adress"); String email = request.getParameter("email"); String phone = request.getParameter("phone"); Login lid = null; try { //connects to the database //adds the new member to the database tables users and login HybernateUtil hu = new HybernateUtil(); SessionFactory sessionFactory = hu.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); Login l = new Login(username, userpass, "member"); session.save(l); session.getTransaction().commit(); session = sessionFactory.getCurrentSession(); tx = session.beginTransaction(); String queryString = "Select l from Login l where l.username= :user"; Query query = session.createQuery(queryString); query.setParameter("user", username); List<EntityBeans.Login> loginlist = query.list(); for (EntityBeans.Login login : loginlist) { if (login.getUsername().equals(username) && login.getUserpass().equals(userpass)) { lid = new Login(login.getUsername(), login.getUserpass(), login.getStatus()); lid.setIdLogin(login.getIdLogin()); } } tx.commit(); session = sessionFactory.getCurrentSession(); tx = session.beginTransaction(); User u = new User(lid, name, adress, email, phone); u.setLoginIdLogin(lid.getIdLogin()); session.save(u); session.getTransaction().commit(); hu.close(); request.getRequestDispatcher("addmembersuccess.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); } } if (request.getParameter("okbtn") != null) { request.getRequestDispatcher("index.jsp").forward(request, response); } }
From source file:Servlets.contServlet.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request//from ww w. j av a2 s . c o m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); if (request.getParameter("cont") != null) { HybernateUtil hu = new HybernateUtil(); SessionFactory sessionFactory = hu.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); System.out.println("cretaied sessions"); String queryString = "SELECT * FROM Office"; Query query = session.createSQLQuery(queryString); System.out.println("Query::::" + query.getQueryString().toString()); ; List queryList = query.list(); statefulBean sfb = new statefulBean(); sfb.setOfficeList(queryList); request.getRequestDispatcher("homecasual.jsp").forward(request, response); tx.commit(); System.out.println( "txcommit............................................................................."); hu.close(); } }
From source file:Servlets.loginServlet.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request//from ww w . ja v a 2s. c o m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (request.getParameter("Submit") != null) { System.out.println("Submit pressed"); String user = request.getParameter("username"); String pass = request.getParameter("password"); HybernateUtil hu = new HybernateUtil(); SessionFactory sessionFactory = hu.getSessionFactory(); //connects to the database //and gets the login info for the username that we got from the textboxes Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); System.out.println("Session beginTransaction"); String queryString = "Select * from Login where Login.username= :user"; Query query = session.createSQLQuery(queryString); System.out.println("querylist made"); query.setParameter("user", user); List loginlist = query.list(); Object[] obj = null; for (Object o : loginlist) { obj = (Object[]) o; System.out.println(obj[0]); } System.out.println("added query.list to List "); session.getTransaction().commit(); System.out.println("commit"); int id = Integer.parseInt(obj[0].toString()); String gotname = obj[1].toString(); String gotpass = obj[2].toString(); String gotstatus = obj[3].toString(); session = sessionFactory.openSession(); session.beginTransaction(); User u = new User(); System.out.println("new user"); u = (User) session.get(User.class, id); session.getTransaction().commit(); System.out.println("commit"); //checks to see what kind of member it is and if the username and password matches //ex admin or member //also some error handling for (int i = 0; i < loginlist.size(); i++) { System.out.println("in for loop"); if (gotname.equals(user) && gotpass.equals(pass)) { System.out.println("Success"); statefulBean stb = new statefulBean(); stb.setName(u.getName()); stb.setAddres(u.getAdress()); stb.setMail(u.getEmail()); stb.setPhone(u.getPhone()); stb.setLoggedIn(1); try { if (gotstatus.equals("member")) { session = sessionFactory.getCurrentSession(); session.beginTransaction(); queryString = "SELECT * FROM Office"; query = session.createSQLQuery(queryString); System.out.println("Query::::" + query.getQueryString().toString()); ; request.setAttribute("Loc", query.list()); request.setAttribute("Username", gotname); System.out.println("member"); request.getRequestDispatcher("homemember.jsp").forward(request, response); } else if (gotstatus.equals("admin")) { request.setAttribute("Username", gotname); System.out.println("admin"); request.getRequestDispatcher("homeadmin.jsp").forward(request, response); } } catch (Exception ex) { ex.printStackTrace(); } } else { String error = "Your Username and/or Password dont exist"; System.out.println("redirect to login in for"); request.setAttribute("error", error); request.getRequestDispatcher("payment.jsp").forward(request, response); } } if (obj.length == 0) { String error = "Your Username and/or Password dont exist"; System.out.println("redirekt to login utside for"); request.setAttribute("error", error); request.getRequestDispatcher("login.jsp").forward(request, response); } // hu.close(); } processRequest(request, response); }