List of usage examples for org.hibernate LockMode NONE
LockMode NONE
To view the source code for org.hibernate LockMode NONE.
Click Source Link
From source file:com.eucalyptus.entities.EntityWrapper.java
License:Open Source License
@SuppressWarnings({ "unchecked", "cast" }) public <T> List<T> query(final T example, final boolean readOnly) { final Example qbe = Example.create(example).enableLike(MatchMode.EXACT); final List<T> resultList = this.getSession().createCriteria(example.getClass()).setLockMode(LockMode.NONE) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).setCacheable(true).add(qbe) .setReadOnly(readOnly).list(); return Lists.newArrayList(Sets.newHashSet(resultList)); }
From source file:com.eucalyptus.entities.EntityWrapper.java
License:Open Source License
/** * Returns a list of results from the database that exactly match <code>example</code>. This method does not use <i><code>enableLike</code></i> match while the * {@link #query(Object)} does. <i><code>enableLike</code></i> criteria trips hibernate when special characters are involved. So it has been replaced by "=" (equals to) * /*ww w.j a v a 2 s . c o m*/ * @param example * @return */ @SuppressWarnings({ "unchecked", "cast" }) public <T> List<T> queryEscape(final T example) { final Example qbe = Example.create(example); final List<T> resultList = this.getSession().createCriteria(example.getClass()).setLockMode(LockMode.NONE) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).setCacheable(true).add(qbe).list(); return Lists.newArrayList(Sets.newHashSet(resultList)); }
From source file:com.eucalyptus.entities.EntityWrapper.java
License:Open Source License
@SuppressWarnings("unchecked") public <T> T getUnique(final T example) throws EucalyptusCloudException { try {//ww w.ja v a 2s .c o m Object id = null; try { id = this.getEntityManager().getEntityManagerFactory().getPersistenceUnitUtil() .getIdentifier(example); } catch (final Exception ex) { } if (id != null) { final T res = (T) this.getEntityManager().find(example.getClass(), id); if (res == null) { throw new NoSuchElementException("@Id: " + id); } else { return res; } } else if ((example instanceof HasNaturalId) && (((HasNaturalId) example).getNaturalId() != null)) { final String natId = ((HasNaturalId) example).getNaturalId(); final T ret = (T) this.createCriteria(example.getClass()).setLockMode(LockMode.NONE) .setCacheable(true).setMaxResults(1).setFetchSize(1).setFirstResult(0) .add(Restrictions.naturalId().set("naturalId", natId)).uniqueResult(); if (ret == null) { throw new NoSuchElementException("@NaturalId: " + natId); } return ret; } else { final T ret = (T) this.createCriteria(example.getClass()).setLockMode(LockMode.NONE) .setCacheable(true).setMaxResults(1).setFetchSize(1).setFirstResult(0) .add(Example.create(example).enableLike(MatchMode.EXACT)).uniqueResult(); if (ret == null) { throw new NoSuchElementException("example: " + LogUtil.dumpObject(example)); } return ret; } } catch (final NonUniqueResultException ex) { throw new EucalyptusCloudException( "Get unique failed for " + example.getClass().getSimpleName() + " because " + ex.getMessage(), ex); } catch (final NoSuchElementException ex) { throw new EucalyptusCloudException( "Get unique failed for " + example.getClass().getSimpleName() + " using " + ex.getMessage(), ex); } catch (final Exception ex) { final Exception newEx = PersistenceExceptions.throwFiltered(ex); throw new EucalyptusCloudException("Get unique failed for " + example.getClass().getSimpleName() + " because " + newEx.getMessage(), newEx); } }
From source file:com.eucalyptus.entities.EntityWrapper.java
License:Open Source License
/** * Returns the unique result from the database that exactly matches <code>example</code>. The differences between this method and {@link #getUnique(Object)} are: * <ol><li>{@link #getUnique(Object)} uses <i><code>enableLike</code></i> match and this method does not. <i><code>enableLike</code></i> criteria trips hibernate when * special characters are involved. So it has been replaced by exact "=" (equals to)</li> * <li>Unique result logic is correctly implemented in this method. If the query returns more than one result, this method correctly throws an exception * wrapping <code>NonUniqueResultException</code>. {@link #getUnique(Object)} does not throw an exception in this case and returns a result as long as it finds * one or more matching results (because of the following properties set on the query: <code>setMaxResults(1)</code>, <code>setFetchSize(1)</code> and * <code>setFirstResult(0)</code>)</li></ol> * /*from w ww .j av a2s.c om*/ * @param example * @return * @throws EucalyptusCloudException */ @SuppressWarnings("unchecked") public <T> T getUniqueEscape(final T example) throws EucalyptusCloudException { try { Object id = null; try { id = this.getEntityManager().getEntityManagerFactory().getPersistenceUnitUtil() .getIdentifier(example); } catch (final Exception ex) { } if (id != null) { final T res = (T) this.getEntityManager().find(example.getClass(), id); if (res == null) { throw new NoSuchElementException("@Id: " + id); } else { return res; } } else if ((example instanceof HasNaturalId) && (((HasNaturalId) example).getNaturalId() != null)) { final String natId = ((HasNaturalId) example).getNaturalId(); final T ret = (T) this.createCriteria(example.getClass()).setLockMode(LockMode.NONE) .setCacheable(true).add(Restrictions.naturalId().set("naturalId", natId)).uniqueResult(); if (ret == null) { throw new NoSuchElementException("@NaturalId: " + natId); } return ret; } else { final T ret = (T) this.createCriteria(example.getClass()).setLockMode(LockMode.NONE) .setCacheable(true).add(Example.create(example)).uniqueResult(); if (ret == null) { throw new NoSuchElementException("example: " + LogUtil.dumpObject(example)); } return ret; } } catch (final NonUniqueResultException ex) { throw new EucalyptusCloudException( "Get unique failed for " + example.getClass().getSimpleName() + " because " + ex.getMessage(), ex); } catch (final NoSuchElementException ex) { throw new EucalyptusCloudException( "Get unique failed for " + example.getClass().getSimpleName() + " using " + ex.getMessage(), ex); } catch (final Exception ex) { final Exception newEx = PersistenceExceptions.throwFiltered(ex); throw new EucalyptusCloudException("Get unique failed for " + example.getClass().getSimpleName() + " because " + newEx.getMessage(), newEx); } }
From source file:com.heliosapm.aa4h.parser.XMLQueryParser.java
License:Apache License
/** * Initializes a Criteria Query.//from ww w.j a v a 2 s . c o m * Mandatory Attributes:<ul> * <li><b>name</b>: The unqualified class name driving the criteria query.</li> * </ul> * Optional Attributes:<ul> * <li><b>prefix</b>: The package name of the class driving the criteria query. If null, no package is assumed.</li> * <li><b>maxSize</b>: The maximum number of rows to return from the database.</li> * <li><b>fetchSize</b>: The number of rows to fetch when rows are requested. Usually not useful for AA4H.</li> * <li><b>cacheEnabled</b>: Enables or disables caching for the queried objects.</li> * <li><b>cacheMode</b>: The cache options for the queried objects.</li> * <li><b>flushMode</b>: The session flush options.</li> * <li><b>fetchMode</b>: The collection fetch options for the query.</li> * <li><b>lockMode</b>: The row lock options for the queried rows.</li> * <li><b>timeOut</b>: The query timeout option.</li> * <li><b>rowCountOnly</b>: Returns a count of the query rows only.</li> * </ul> * @param attrs The attributes of the processed node. * @return An appended or new CriteriaSpecification * @throws SAXException */ protected CriteriaSpecification processCriteria(Attributes attrs) throws SAXException { if (inDetached) { return criteriaStack.peek(); } String name = attrs.getValue("name"); String prefix = attrs.getValue("prefix"); if (prefix != null) { className = prefix + "." + name; } else { className = name; } String maxSize = attrs.getValue("maxSize"); String fetchSize = attrs.getValue("fetchSize"); String firstResult = attrs.getValue("firstResult"); String cacheEnabled = attrs.getValue("cacheEnabled"); String cacheMode = attrs.getValue("cacheMode"); String flushMode = attrs.getValue("flushMode"); String fetchMode = attrs.getValue("fetchMode"); String lockMode = attrs.getValue("lockMode"); String timeOut = attrs.getValue("timeOut"); String rowCountOnly = attrs.getValue("rowCountOnly"); Criteria newCriteria = null; try { if (criteriaStack.size() == 0) { newCriteria = session.createCriteria(className); } else { newCriteria = ((Criteria) criteriaStack.peek()).createCriteria(className); } criteriaStack.push(newCriteria); if ("true".equalsIgnoreCase(rowCountOnly)) { newCriteria.setProjection(Projections.projectionList().add(Projections.rowCount()) ); setRowCountOnly(true); } if (maxSize != null && isRowCountOnly() == false) { newCriteria.setMaxResults(Integer.parseInt(maxSize)); } if (fetchSize != null && isRowCountOnly() == false) { newCriteria.setFetchSize(Integer.parseInt(fetchSize)); } if (firstResult != null && isRowCountOnly() == false) { newCriteria.setFirstResult(Integer.parseInt(firstResult)); } if (timeOut != null) { newCriteria.setTimeout(Integer.parseInt(timeOut)); } if ("true".equalsIgnoreCase(cacheEnabled)) { newCriteria.setCacheable(true); } else if ("false".equalsIgnoreCase(cacheEnabled)) { newCriteria.setCacheable(false); } if (fetchMode != null && fetchMode.length() > 0) { if ("JOIN".equalsIgnoreCase(fetchMode)) { newCriteria.setFetchMode(name, FetchMode.JOIN); } else if ("SELECT".equalsIgnoreCase(fetchMode)) { newCriteria.setFetchMode(name, FetchMode.SELECT); } else { newCriteria.setFetchMode(name, FetchMode.DEFAULT); } } else { newCriteria.setFetchMode(name, FetchMode.DEFAULT); } if (cacheMode != null && cacheMode.length() > 0) { if ("GET".equalsIgnoreCase(cacheMode)) { newCriteria.setCacheMode(CacheMode.GET); } else if ("IGNORE".equalsIgnoreCase(cacheMode)) { newCriteria.setCacheMode(CacheMode.IGNORE); } else if ("NORMAL".equalsIgnoreCase(cacheMode)) { newCriteria.setCacheMode(CacheMode.NORMAL); } else if ("PUT".equalsIgnoreCase(cacheMode)) { newCriteria.setCacheMode(CacheMode.PUT); } else if ("REFRESH".equalsIgnoreCase(cacheMode)) { newCriteria.setCacheMode(CacheMode.REFRESH); } else { newCriteria.setCacheMode(CacheMode.NORMAL); } } if (lockMode != null && lockMode.length() > 0) { if ("NONE".equalsIgnoreCase(lockMode)) { newCriteria.setLockMode(LockMode.NONE); } else if ("READ".equalsIgnoreCase(lockMode)) { newCriteria.setLockMode(LockMode.READ); } else if ("UPGRADE".equalsIgnoreCase(lockMode)) { newCriteria.setLockMode(LockMode.UPGRADE); } else if ("UPGRADE_NOWAIT".equalsIgnoreCase(lockMode)) { newCriteria.setLockMode(LockMode.UPGRADE_NOWAIT); } else if ("WRITE".equalsIgnoreCase(lockMode)) { newCriteria.setLockMode(LockMode.WRITE); } else { throw new SAXException("lockMode[" + lockMode + "] Not Recognized"); } } if (flushMode != null && flushMode.length() > 0) { if ("ALWAYS".equalsIgnoreCase(flushMode)) { newCriteria.setFlushMode(FlushMode.ALWAYS); } else if ("AUTO".equalsIgnoreCase(flushMode)) { newCriteria.setFlushMode(FlushMode.AUTO); } else if ("COMMIT".equalsIgnoreCase(flushMode)) { newCriteria.setFlushMode(FlushMode.COMMIT); } else if ("NEVER".equalsIgnoreCase(flushMode)) { // NEVER is deprecated, so we won't throw an exception but we'll ignore it. } else { throw new SAXException("flushMode[" + flushMode + "] Not Recognized"); } } return newCriteria; } catch (Exception e) { throw new SAXException("Unable to configure class " + className, e); } }
From source file:com.jettmarks.routes.server.bean.BikeTrainDAO.java
License:Apache License
@SuppressWarnings("deprecation") public void attachClean(BikeTrain instance) { logger.debug("attaching clean BikeTrain instance"); try {/*from w ww. j a v a 2 s . c o m*/ session.lock(instance, LockMode.NONE); logger.debug("attach successful"); } catch (RuntimeException re) { logger.error("attach failed", re); throw re; } }
From source file:com.jettmarks.routes.server.bean.BikeTrainElementGroupDAO.java
License:Apache License
public void attachClean(BikeTrainElementGroup instance) { logger.debug("attaching clean BikeTrainElementGroup instance"); try {// www. j a va2 s .c om session.lock(instance, LockMode.NONE); logger.debug("attach successful"); } catch (RuntimeException re) { logger.error("attach failed", re); throw re; } }
From source file:com.jettmarks.routes.server.bean.BikeTrainGroupDAO.java
License:Apache License
public void attachClean(BikeTrainGroup instance) { logger.debug("attaching clean BikeTrainGroup instance"); try {/* ww w.ja va 2 s . c o m*/ session.lock(instance, LockMode.NONE); logger.debug("attach successful"); } catch (RuntimeException re) { logger.error("attach failed", re); throw re; } }
From source file:com.jettmarks.routes.server.bean.CurrentTrainsDAO.java
License:Apache License
public void attachClean(CurrentTrains instance) { logger.debug("attaching clean CurrentTrains instance"); try {/*w w w .j a v a 2 s . c o m*/ session.lock(instance, LockMode.NONE); logger.debug("attach successful"); } catch (RuntimeException re) { logger.error("attach failed", re); throw re; } }
From source file:com.jettmarks.routes.server.bean.DisplayElementDAO.java
License:Apache License
public void attachClean(DisplayElement instance) { logger.debug("attaching clean DisplayElement instance"); try {/*from w w w . j a v a2 s .c o m*/ session.lock(instance, LockMode.NONE); logger.debug("attach successful"); } catch (RuntimeException re) { logger.error("attach failed", re); throw re; } }