List of usage examples for org.hibernate Session setFlushMode
@Deprecated
void setFlushMode(FlushMode flushMode);
From source file:org.yestech.lib.hibernate.search.YesHibernateSearchTemplate.java
License:LGPL
/** * Execute the action specified by the given action object within a Session. * * @param action callback object that specifies the Hibernate action * @param enforceNewSession whether to enforce a new Session for this template * even if there is a pre-bound transactional Session * @param enforceNativeSession whether to enforce exposure of the native * Hibernate Session to callback code * @return a result object returned by the action, or <code>null</code> * @throws org.springframework.dao.DataAccessException * in case of Hibernate errors//from w w w. j a v a 2 s . c o m */ public Object doExecute(HibernateSearchCallback action, boolean enforceNewSession, boolean enforceNativeSession) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); Session session = (enforceNewSession ? SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor()) : getSession()); boolean existingTransaction = (!enforceNewSession && (SessionFactoryUtils.isSessionTransactional(session, getSessionFactory()))); if (existingTransaction) { logger.debug("Found thread-bound Session for HibernateTemplate"); } FlushMode previousFlushMode = null; try { previousFlushMode = applyFlushMode(session, existingTransaction); enableFilters(session); Session sessionToExpose = (enforceNativeSession || isExposeNativeSession() ? session : createSessionProxy(session)); Object result = action.doInHibernate(sessionToExpose); flushIfNecessary(session, existingTransaction); return result; } catch (HibernateException ex) { throw convertHibernateAccessException(ex); } finally { if (existingTransaction) { logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate"); disableFilters(session); if (previousFlushMode != null) { session.setFlushMode(previousFlushMode); } } else { // Never use deferred close for an explicitly new Session. if (isAlwaysUseNewSession()) { SessionFactoryUtils.closeSession(session); } else { if (SessionFactoryUtils.isDeferredCloseActive(getSessionFactory())) { SessionFactoryUtils.initDeferredClose(getSessionFactory()); } else { SessionFactoryUtils.closeSession(session); } } } } }
From source file:org.zanata.model.validator.UniqueValidator.java
License:Open Source License
private int countRows(Object value) { // we need to use entityManager.unwrap because injected session will // be a weld proxy and criteria.getExecutableCriteria method will try // to cast it to SessionImplementor (ClassCastException) Session session = entityManager.unwrap(Session.class); ClassMetadata metadata = session.getSessionFactory().getClassMetadata(value.getClass()); String idName = metadata.getIdentifierPropertyName(); // FIXME was EntityMode.POJO Serializable id = metadata.getIdentifier(value, null); DetachedCriteria criteria = DetachedCriteria.forClass(value.getClass()); for (String property : parameters.properties()) { // FIXME was EntityMode.POJO criteria.add(Restrictions.eq(property, metadata.getPropertyValue(value, property))); }//w w w . j a va 2s .co m // Id property if (id != null) { criteria.add(Restrictions.ne(idName, id)); } criteria.setProjection(Projections.rowCount()); // change the flush mode temporarily to perform the query or else // incomplete entities will try to get flushed // After the query, go back to the original mode FlushMode flushMode = session.getFlushMode(); session.setFlushMode(FlushMode.MANUAL); List results = criteria.getExecutableCriteria(session).list(); Number count = (Number) results.iterator().next(); session.setFlushMode(flushMode); return count.intValue(); }
From source file:org.zkoss.zkgrails.ZKGrailsOpenSessionInViewListener.java
License:Open Source License
private Session getSession() throws DataAccessResourceFailureException { Session session = SessionFactoryUtils.getSession(sessionFactory, true); session.setFlushMode(FlushMode.AUTO); return session; }
From source file:owldb.util.HibernateUtil.java
License:Open Source License
/** * Executes the given Hibernate wrapper. * //from ww w. j a v a2 s. c o m * @param <T> The type of the result value * @param factory A session factory * @param wrapper The wrapper to execute * @return The result object */ @SuppressWarnings("unchecked") public static <T> T executeTransaction(final SessionFactory factory, final HibernateWrapper<T> wrapper) { final Session session = factory.openSession(); session.setFlushMode(FlushMode.COMMIT); Transaction t = null; try { t = session.beginTransaction(); final Object result = wrapper.doInHibernate(session); t.commit(); return (T) result; } catch (final RuntimeException ex) { if (t != null) t.rollback(); throw ex; } finally { session.close(); } }
From source file:service.DatabaseService.java
@Transactional @Override// w w w . j a v a 2 s . c o m public void saveEntity(Object entity) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); session.setFlushMode(FlushMode.MANUAL); ManagedSessionContext.bind(session); Transaction tx = null; try { tx = session.beginTransaction(); session.save(entity); ManagedSessionContext.unbind(sessionFactory); session.flush(); tx.commit(); // Flush happens automatically } catch (RuntimeException e) { tx.rollback(); throw e; } finally { session.close(); } }
From source file:service.DatabaseService.java
@Override @Transactional//from ww w. j a v a 2 s . c o m public Object getEntityByName(String column, String table, String login) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); session.setFlushMode(FlushMode.MANUAL); ManagedSessionContext.bind(session); String table_capitalized = table.substring(0, 1).toUpperCase() + table.substring(1); String str_query = "from " + table_capitalized + " t where t." + column.toLowerCase() + " = :crit"; Query query = session.createQuery(str_query); query.setParameter("crit", login); //ManagedSessionContext.unbind(sessionFactory); session.flush(); if (query.list().size() == 0) return null; return query.list().get(0); }
From source file:service.DatabaseService.java
@Override @Transactional//from w w w . j ava2 s . c o m public Object getEntityById(String column, String table, Integer id) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); session.setFlushMode(FlushMode.MANUAL); ManagedSessionContext.bind(session); String table_capitalized = table.substring(0, 1).toUpperCase() + table.substring(1); String str_query = "from " + table_capitalized + " t where t." + column.toLowerCase() + " = :crit"; Query query = session.createQuery(str_query); query.setParameter("crit", id); //ManagedSessionContext.unbind(sessionFactory); session.flush(); if (query.list().size() == 0) return null; return query.list().get(0); }
From source file:service.DatabaseService.java
@Override @Transactional//from ww w . j a v a 2s . co m public void updateEntity(Object entity) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); session.setFlushMode(FlushMode.MANUAL); ManagedSessionContext.bind(session); Transaction tx = null; try { tx = session.beginTransaction(); session.merge(entity); ManagedSessionContext.unbind(HibernateUtil.getSessionFactory()); session.flush(); tx.commit(); } catch (RuntimeException e) { tx.rollback(); throw e; } finally { session.close(); } }
From source file:service.DatabaseService.java
@Override @Transactional/*from w ww . j av a2 s . c o m*/ public void findEntitiesMatchingPartialQuery(String field, String query_text, Object entity) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); session.setFlushMode(FlushMode.MANUAL); ManagedSessionContext.bind(session); FullTextSession fullTextSession = org.hibernate.search.Search.getFullTextSession(session); Transaction tx = fullTextSession.beginTransaction(); QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(entity.getClass()).get(); org.apache.lucene.search.Query query = qb.keyword().onFields(field).matching(query_text).createQuery(); org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, entity.getClass()); List result = hibQuery.list(); if (result.size() > 0) System.out.println(result.get(0)); }
From source file:service.DatabaseService.java
@Override public List listEntity(String table) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); session.setFlushMode(FlushMode.MANUAL); ManagedSessionContext.bind(session); String table_capitalized = table.substring(0, 1).toUpperCase() + table.substring(1); String str_query = "from " + table_capitalized; Query query = session.createQuery(str_query); //ManagedSessionContext.unbind(sessionFactory); session.flush();//from w w w. ja v a 2 s . c om return query.list(); }