List of usage examples for org.hibernate.criterion DetachedCriteria getExecutableCriteria
public Criteria getExecutableCriteria(Session session)
From source file:org.springframework.orm.hibernate3.StatelessHibernateTemplate.java
License:Apache License
public List findByCriteria(final DetachedCriteria criteria, final int firstResult, final int maxResults) throws DataAccessException { Assert.notNull(criteria, "DetachedCriteria must not be null"); return (List) execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Criteria executableCriteria = criteria.getExecutableCriteria(session); prepareCriteria(executableCriteria); if (firstResult >= 0) { executableCriteria.setFirstResult(firstResult); }/* w w w . java 2s.c o m*/ if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); } }, true); }
From source file:org.springframework.orm.hibernate5.HibernateTemplate.java
License:Apache License
@Override public List<?> findByCriteria(final DetachedCriteria criteria, final int firstResult, final int maxResults) throws DataAccessException { Assert.notNull(criteria, "DetachedCriteria must not be null"); return executeWithNativeSession(new HibernateCallback<List<?>>() { @Override/*from w w w .j a v a2 s .c o m*/ public List<?> doInHibernate(Session session) throws HibernateException { Criteria executableCriteria = criteria.getExecutableCriteria(session); prepareCriteria(executableCriteria); if (firstResult >= 0) { executableCriteria.setFirstResult(firstResult); } if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); } }); }
From source file:org.squashtest.tm.internal.domain.report.query.hibernate.HibernateReportQueryDao.java
License:Open Source License
private List<?> executeDetachedCriteria(DetachedCriteria dCriteria) { Session session = currentSession();/*www . j av a 2 s. c o m*/ Criteria criteria = dCriteria.getExecutableCriteria(session); return criteria.list(); }
From source file:org.tynamo.hibernate.services.HibernatePersistenceServiceImpl.java
License:Apache License
/** * https://trails.dev.java.net/servlets/ReadMsg?listName=users&msgNo=1226 * <p/>/*from www . ja va 2 s . com*/ * Very often I find myself writing: * <code> * Object example = new Object(); example.setProperty(uniqueValue); * List objects = ((TynamoPage)getPage()).getPersistenceService().getInstances(example); * (MyObject)objects.get(0); * </code> * when, in fact, I know that the single property I populated my example object with should be unique, and thus only * one object should be returned * * @param type The type to use to check for security restrictions. * @param detachedCriteria * @return */ public <T> T getInstance(final Class<T> type, DetachedCriteria detachedCriteria) { final DetachedCriteria criteria = alterCriteria(type, detachedCriteria); return (T) criteria.getExecutableCriteria(getSession()).uniqueResult(); }
From source file:org.tynamo.hibernate.services.HibernatePersistenceServiceImpl.java
License:Apache License
public <T> List<T> getInstances(Class<T> type, DetachedCriteria criteria) { criteria = alterCriteria(type, criteria); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); return criteria.getExecutableCriteria(getSession()).list(); }
From source file:org.tynamo.hibernate.services.HibernatePersistenceServiceImpl.java
License:Apache License
public List getInstances(final Object example, final TynamoClassDescriptor classDescriptor) { //create Criteria instance DetachedCriteria searchCriteria = DetachedCriteria.forClass(example.getClass()); searchCriteria = alterCriteria(example.getClass(), searchCriteria); //loop over the example object's PropertyDescriptors for (TynamoPropertyDescriptor propertyDescriptor : classDescriptor.getPropertyDescriptors()) { //only add a Criterion to the Criteria instance if this property is searchable if (propertyDescriptor.isSearchable()) { String propertyName = propertyDescriptor.getName(); Class propertyClass = propertyDescriptor.getPropertyType(); Object value = getPropertyAccess().get(example, propertyName); //only add a Criterion to the Criteria instance if the value for this property is non-null if (value != null) { if (String.class.isAssignableFrom(propertyClass) && ((String) value).length() > 0) { searchCriteria.add(Restrictions.like(propertyName, value.toString(), MatchMode.ANYWHERE)); }/*from w w w . java 2s . c om*/ /** * 'one'-end of many-to-one, one-to-one * * Just match the identifier */ else if (propertyDescriptor.isObjectReference()) { Serializable identifierValue = getIdentifier(value, descriptorService.getClassDescriptor(propertyDescriptor.getBeanType())); searchCriteria.createCriteria(propertyName).add(Restrictions.idEq(identifierValue)); } else if (propertyClass.isPrimitive()) { //primitive types: ignore zeroes in case of numeric types, ignore booleans anyway (TODO come up with something...) if (!propertyClass.equals(boolean.class) && ((Number) value).longValue() != 0) { searchCriteria.add(Restrictions.eq(propertyName, value)); } } else if (propertyDescriptor.isCollection()) { //one-to-many or many-to-many CollectionDescriptor collectionDescriptor = (CollectionDescriptor) propertyDescriptor; TynamoClassDescriptor collectionClassDescriptor = descriptorService .getClassDescriptor(collectionDescriptor.getElementType()); if (collectionClassDescriptor != null) { String identifierName = collectionClassDescriptor.getIdentifierDescriptor().getName(); Collection<Serializable> identifierValues = new ArrayList<Serializable>(); Collection associatedItems = (Collection) value; if (associatedItems != null && associatedItems.size() > 0) { for (Object o : associatedItems) { identifierValues.add(getIdentifier(o, collectionClassDescriptor)); } //add a 'value IN collection' restriction searchCriteria.createCriteria(propertyName) .add(Restrictions.in(identifierName, identifierValues)); } } } } } } searchCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); // FIXME This won't work because the shadow proxy doesn't implement SessionImplementor // that session is casted to. Maybe we should inject SessionManager instead // and obtain the Session from it return searchCriteria.getExecutableCriteria(getSession()).list(); }
From source file:org.tynamo.hibernate.services.HibernatePersistenceServiceImpl.java
License:Apache License
public int count(Class type, DetachedCriteria detachedCriteria) { detachedCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); final DetachedCriteria criteria = alterCriteria(type, detachedCriteria); Criteria executableCriteria = criteria.getExecutableCriteria(getSession()) .setProjection(Projections.rowCount()); return ((Long) executableCriteria.uniqueResult()).intValue(); }
From source file:org.tynamo.hibernate.services.HibernatePersistenceServiceImpl.java
License:Apache License
public List getInstances(final DetachedCriteria detachedCriteria, final int startIndex, final int maxResults) { detachedCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); Criteria executableCriteria = detachedCriteria.getExecutableCriteria(getSession()); if (startIndex >= 0) { executableCriteria.setFirstResult(startIndex); }// w ww.jav a 2s . c o m if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); }
From source file:org.workin.persistence.hibernate.v3.dao.Hibernate3PersistenceDaoImpl.java
License:Apache License
@Override public T findUniqueByCriteria(final DetachedCriteria criteria) { return getHibernateTemplate().execute(new HibernateCallback<T>() { @SuppressWarnings("unchecked") @Override/*from w ww . j ava 2 s . co m*/ public T doInHibernate(Session session) throws HibernateException, SQLException { Criteria executableCriteria = criteria.getExecutableCriteria(session); return (T) executableCriteria.uniqueResult(); } }); }
From source file:org.workin.persistence.hibernate.v3.dao.Hibernate3PersistenceDaoImpl.java
License:Apache License
@Override public long countByCriteria(final DetachedCriteria criteria, final boolean distinct) { return getHibernateTemplate().execute(new HibernateCallback<Long>() { @Override//w ww. j a va 2s . co m public Long doInHibernate(Session session) throws HibernateException, SQLException { if (distinct) criteria.setProjection(Projections.distinct(Projections.rowCount())); else criteria.setProjection(Projections.rowCount()); Criteria executableCriteria = criteria.getExecutableCriteria(session); return (Long) executableCriteria.uniqueResult(); } }); }