public SubscriptionStateEntity getByName(String name) { return (SubscriptionStateEntity) sessionFactory .getCurrentSession() .createCriteria(SubscriptionStateEntity.class) .add(Restrictions.naturalId().set("name", name)) .uniqueResult(); }
public static MessageBundleEntry loadByBundleAndCode(String bundle, String code) { return (MessageBundleEntry) getSession() .createCriteria(MessageBundleEntry.class) .setCacheable(true) .setCacheRegion("messages") .add(Restrictions.naturalId().set("bundle", bundle).set("code", code)) .uniqueResult(); }
@SuppressWarnings("unchecked") public static void removeEmptyEntries(String bundle) { List<MessageBundleEntry> entries = getSession() .createCriteria(MessageBundleEntry.class) .setCacheable(true) .setCacheRegion("messages") .add(Restrictions.sizeLe("messages", 1)) .add(Restrictions.naturalId().set("bundle", bundle)) .list(); for (MessageBundleEntry entry : entries) { entry.delete(); } }
private static <T> T maybeNaturalId(final T example) throws HibernateException, NoSuchElementException { final String natId = ((HasNaturalId) example).getNaturalId(); @SuppressWarnings("unchecked") final T ret = (T) Entities.getTransaction(example) .getTxState() .getSession() .createCriteria(example.getClass()) .add(Restrictions.naturalId().set("naturalId", natId)) .setCacheable(true) .setMaxResults(1) .setFetchSize(1) .setFirstResult(0) .uniqueResult(); if (ret == null) { throw new NoSuchElementException("@NaturalId: " + natId); } return ret; }
/** * 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>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>) * </ol> * * @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); } }