List of usage examples for org.hibernate Session setFlushMode
@Deprecated
void setFlushMode(FlushMode flushMode);
From source file:org.jboss.dashboard.workspace.SectionsManagerImpl.java
License:Apache License
public Section getSectionByDbId(final Long dbid) throws Exception { if (dbid == null) return null; final List<Section> results = new ArrayList<Section>(); new HibernateTxFragment() { protected void txFragment(Session session) throws Exception { FlushMode flushMode = session.getFlushMode(); session.setFlushMode(FlushMode.COMMIT); StringBuffer sql = new StringBuffer(); sql.append("select item "); sql.append("from ").append(Section.class.getName()).append(" as item "); sql.append("where item.dbid = :dbid"); Query query = session.createQuery(sql.toString()); query.setLong("dbid", dbid.longValue()); query.setCacheable(true);//from w w w. j a v a2 s . c o m results.addAll(query.list()); session.setFlushMode(flushMode); } }.execute(); if (results.size() > 0) return results.get(0); else log.debug("Does not exists a section with dbid: " + dbid); return null; }
From source file:org.jboss.dashboard.workspace.WorkspaceImpl.java
License:Apache License
/** * Returns all the children for a given section *//*from w ww.ja v a2s .com*/ public Section[] getAllChildSections(final Long sectionId) { final List<Section> childSections = new ArrayList<Section>(); try { new HibernateTxFragment() { protected void txFragment(Session session) throws Exception { FlushMode oldFlushMode = session.getFlushMode(); session.setFlushMode(FlushMode.NEVER); StringBuffer hql = new StringBuffer("from "); hql.append(Section.class.getName()).append(" as section where section.workspace=:workspace"); if (sectionId == null) hql.append(" and section.parentSectionId is null"); else hql.append(" and section.parentSectionId=:parentSectionId"); Query query = session.createQuery(hql.toString()); query.setParameter("workspace", WorkspaceImpl.this); if (sectionId != null) query.setLong("parentSectionId", sectionId); query.setCacheable(true); childSections.addAll(query.list()); session.setFlushMode(oldFlushMode); } }.execute(); } catch (Exception e) { log.error("Error: ", e); } Collections.sort(childSections); return childSections.toArray(new Section[childSections.size()]); }
From source file:org.jboss.dashboard.workspace.WorkspaceImpl.java
License:Apache License
public int getSectionsCount() { try {/*from w w w . j ava2 s. co m*/ final int[] size = new int[1]; new HibernateTxFragment() { protected void txFragment(Session session) throws Exception { FlushMode oldFlushMode = session.getFlushMode(); session.setFlushMode(FlushMode.NEVER); Query query = session.createQuery("from " + Section.class.getName() + " as section " + "where section.workspace=:workspace"); query.setParameter("workspace", WorkspaceImpl.this); query.setCacheable(true); size[0] = query.list().size(); session.setFlushMode(oldFlushMode); } }.execute(); return size[0]; } catch (Exception e) { log.error("Error: ", e); } return -1; }
From source file:org.jboss.dashboard.workspace.WorkspaceImpl.java
License:Apache License
/** * Returns a given section according to its identifier *///from w w w . ja va2s.c o m public Section getSection(final Long id) { if (id == null) return null; final List<Section> candidates = new ArrayList<Section>(); try { new HibernateTxFragment() { protected void txFragment(Session session) throws Exception { FlushMode oldFlushMode = session.getFlushMode(); session.setFlushMode(FlushMode.NEVER); Query query = session.createQuery("from " + Section.class.getName() + " as section " + "where section.workspace=:workspace and section.sectionId=:pageid"); query.setParameter("workspace", WorkspaceImpl.this); query.setLong("pageid", id.longValue()); query.setCacheable(true); candidates.addAll(query.list()); session.setFlushMode(oldFlushMode); } }.execute(); } catch (Exception e) { log.error("Error: ", e); } if (candidates.size() == 1) { return candidates.get(0); } return null; }
From source file:org.jboss.dashboard.workspace.WorkspaceImpl.java
License:Apache License
public PanelInstance getPanelInstance(final Long id) { if (id == null) return null; final List<PanelInstance> candidates = new ArrayList<PanelInstance>(); try {/*from w w w .j a v a 2s . co m*/ new HibernateTxFragment() { protected void txFragment(Session session) throws Exception { FlushMode oldFlushMode = session.getFlushMode(); session.setFlushMode(FlushMode.NEVER); Query query = session.createQuery("from " + PanelInstance.class.getName() + " as instance " + "where instance.workspace=:workspace and instance.instanceId=:instid"); query.setParameter("workspace", WorkspaceImpl.this); query.setLong("instid", id.longValue()); query.setCacheable(true); candidates.addAll(query.list()); session.setFlushMode(oldFlushMode); } }.execute(); } catch (Exception e) { log.error("Error: ", e); } if (candidates.size() == 1) { return candidates.get(0); } return null; }
From source file:org.jboss.dashboard.workspace.WorkspaceImpl.java
License:Apache License
/** * Returns a given section according to its url *///ww w .j a va2s.co m public Section getSectionByUrl(final String friendlyUrl) { if (friendlyUrl == null) return null; final List<Section> candidates = new ArrayList<Section>(); try { new HibernateTxFragment() { protected void txFragment(Session session) throws Exception { FlushMode oldFlushMode = session.getFlushMode(); session.setFlushMode(FlushMode.NEVER); Query query = session.createQuery("from " + Section.class.getName() + " as section " + "where section.workspace=:workspace and section.friendlyUrl=:pageid"); query.setParameter("workspace", WorkspaceImpl.this); query.setString("pageid", friendlyUrl); query.setCacheable(true); candidates.addAll(query.list()); session.setFlushMode(oldFlushMode); } }.execute(); } catch (Exception e) { log.error("Error: ", e); } if (candidates.size() == 1) { return candidates.get(0); } return null; }
From source file:org.jboss.dashboard.workspace.WorkspacesManager.java
License:Apache License
/** * Return all workspaces//w ww. j av a2 s.c om */ public WorkspaceImpl[] getWorkspaces() { final List<WorkspaceImpl> workspaces = new ArrayList<WorkspaceImpl>(); HibernateTxFragment txFragment = new HibernateTxFragment() { protected void txFragment(Session session) throws Exception { FlushMode oldFlushMode = session.getFlushMode(); session.setFlushMode(FlushMode.NEVER); Query q = session.createQuery(" from " + WorkspaceImpl.class.getName()); q.setCacheable(true); workspaces.addAll(q.list()); session.setFlushMode(oldFlushMode); } }; try { txFragment.execute(); } catch (Exception e) { log.error("Error:", e); } return workspaces.toArray(new WorkspaceImpl[workspaces.size()]); }
From source file:org.jboss.dashboard.workspace.WorkspacesManager.java
License:Apache License
public Workspace getWorkspaceByUrl(final String url) throws Exception { final Workspace[] workspace = new Workspace[1]; new HibernateTxFragment() { protected void txFragment(Session session) throws Exception { FlushMode oldFlushMode = session.getFlushMode(); session.setFlushMode(FlushMode.NEVER); Query q = session// w ww . j ava 2 s . com .createQuery(" from " + WorkspaceImpl.class.getName() + " p where p.friendlyUrl = :url"); q.setString("url", url); q.setCacheable(true); List l = q.list(); if (l.size() == 1) { workspace[0] = (Workspace) l.get(0); } session.setFlushMode(oldFlushMode); } }.execute(); return workspace[0]; }
From source file:org.jooby.hbm.OpenSessionInView.java
License:Apache License
@Override public void handle(final Request req, final Response rsp, final Chain chain) throws Throwable { SessionFactory sf = emf.getSessionFactory(); EntityManager em = emf.createEntityManager(); Session session = (Session) em.getDelegate(); String sessionId = Integer.toHexString(System.identityHashCode(session)); keys.forEach(key -> req.set(key, em)); log.debug("session opened: {}", sessionId); TrxResponse trxrsp = new TrxResponse(rsp, em); try {/*from w ww . j av a2 s. co m*/ log.debug(" [{}] binding", sessionId); ManagedSessionContext.bind(session); FlushMode flushMode = FlushMode.AUTO; log.debug(" [{}] flush mode: {}", sessionId, flushMode); session.setFlushMode(flushMode); trxrsp.begin(); // invoke next handler chain.next(req, trxrsp); } catch (Exception ex) { trxrsp.setRollbackOnly(); throw ex; } finally { trxrsp.done(); log.debug(" [{}] unbinding", sessionId); ManagedSessionContext.unbind(sf); log.debug("session released: [{}]", sessionId); } }
From source file:org.jspresso.framework.application.backend.persistence.hibernate.HibernateBackendController.java
License:Open Source License
/** * Retrieves the current thread-bound / tx-bound Hibernate session. this is * now the preferred way to perform Hibernate operations. It relies on the * Hibernate 3.1+ {@link SessionFactory#getCurrentSession()} method. * * @return the current thread-bound / tx-bound Hibernate session. *//* w ww. j a v a2 s. c om*/ public Session getHibernateSession() { Session currentSession; if (isUnitOfWorkActive()) { try { currentSession = getHibernateSessionFactory().getCurrentSession(); } catch (HibernateException ex) { currentSession = getNoTxSession(); } } else { currentSession = getNoTxSession(); } if (currentSession != noTxSession) { // we are on a transactional session. currentSession.setFlushMode(getDefaultTxFlushMode()); } configureHibernateGlobalFilter(currentSession.enableFilter(JSPRESSO_SESSION_GLOBALS)); return currentSession; }