List of usage examples for org.hibernate.criterion Restrictions idEq
public static Criterion idEq(Object value)
From source file:org.openspaces.persistency.hibernate.StatelessHibernateSpaceSynchronizationEndpoint.java
License:Open Source License
private boolean exists(DataSyncOperation dataSyncOperation, StatelessSession session) { Criteria criteria = null;// ww w . j a v a 2 s .co m switch (dataSyncOperation.getDataSyncOperationType()) { case REMOVE: case WRITE: case UPDATE: if (!dataSyncOperation.supportsDataAsObject()) return false; Object entry = dataSyncOperation.getDataAsObject(); criteria = session.createCriteria(entry.getClass().getName()); ClassMetadata classMetaData = getSessionFactory().getClassMetadata(entry.getClass()); criteria.add(Restrictions.idEq(classMetaData.getIdentifier(entry))); criteria.setProjection(Projections.rowCount()); return ((Number) criteria.uniqueResult()).intValue() > 0; case PARTIAL_UPDATE: if (!dataSyncOperation.supportsGetTypeDescriptor()) return false; SpaceTypeDescriptor typeDescriptor = dataSyncOperation.getTypeDescriptor(); criteria = session.createCriteria(typeDescriptor.getTypeName()); criteria.add(Restrictions.idEq(typeDescriptor.getIdPropertyName())); criteria.setProjection(Projections.rowCount()); return ((Number) criteria.uniqueResult()).intValue() > 0; default: return false; } }
From source file:org.quackbot.dao.hibernate.GenericHbDAO.java
License:Open Source License
@Override public T findById(Long id) { return (T) getSession().createCriteria(getPersistentClass()).add(Restrictions.idEq(id)).uniqueResult(); }
From source file:org.shredzone.cilla.service.search.strategy.AbstractSearchStrategy.java
License:Open Source License
/** * Creates a {@link Criteria} object for the filter given in the * {@link SearchResultImpl}.//from w ww.ja v a2 s.c o m */ protected Criteria createCriteria(SearchResultImpl result) throws CillaServiceException { Criteria crit = pageDao.criteria(); crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); crit.add(Restrictions.and(Restrictions.isNotNull("publication"), Restrictions.le("publication", result.getNow()))); crit.add( Restrictions.or(Restrictions.isNull("expiration"), Restrictions.gt("expiration", result.getNow()))); crit.add(Restrictions.eq("hidden", false)); FilterModel filter = result.getFilter(); if (filter != null) { if (filter.getCategory() != null) { // I'd prefer to use Restrictions.in instead, but this seems to be difficult. // http://stackoverflow.com/questions/407737/hibernate-criteria-querying-tables-in-nm-relationship Disjunction dis = Restrictions.disjunction(); result.getEffectiveCategories().stream().mapToLong(Category::getId) .forEach(id -> dis.add(Restrictions.idEq(id))); crit.createCriteria("categories").add(dis); } if (filter.getTag() != null) { long tagId = filter.getTag().getId(); Disjunction dis = Restrictions.disjunction(); // All pages with the requested tag crit.createAlias("tags", "tt"); dis.add(Restrictions.eq("tt.id", tagId)); // All pages with pictures in a gallery section having the requested tag DetachedCriteria subcrit = DetachedCriteria.forClass(GallerySection.class); subcrit.createCriteria("pictures").createCriteria("tags").add(Restrictions.idEq(tagId)); subcrit.setProjection(Projections.distinct(Projections.property("page.id"))); dis.add(Subqueries.propertyIn("id", subcrit)); crit.add(dis); } if (filter.getCreator() != null) { crit.add(Restrictions.eq("creator", filter.getCreator())); } if (filter.getPage() != null) { crit.add(Restrictions.idEq(filter.getPage().getId())); } if (filter.getDate() != null) { DateRange dr = filter.getDate(); PageOrder order = (filter.getOrder() != null ? filter.getOrder() : PageOrder.PUBLICATION); crit.add(Restrictions.between(order.getColumn(), dr.getFromDate().getTime(), dr.getThruDate().getTime())); } if (filter.getQuery() != null) { // No challenge protected pages for context search, because protected // contents may be revealed in the search results. crit.add(Restrictions.isNull("challenge")); } } return crit; }
From source file:org.tynamo.hibernate.services.HibernatePersistenceServiceImpl.java
License:Apache License
/** * (non-Javadoc)//from ww w . j a v a2 s . co m * * @see org.tynamo.services.PersistenceService#getInstance(Class,Serializable) */ public <T> T getInstance(final Class<T> type, final Serializable id) { DetachedCriteria criteria = DetachedCriteria.forClass(type).add(Restrictions.idEq(id)); return getInstance(type, criteria); }
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 ww w . ja v a2 s. 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.unitec.casosdeuso.DAOReservacion.java
public Cliente buscarPorId(Integer id) throws Exception { Cliente a = (Cliente) sesion.createCriteria(Cliente.class).add(Restrictions.idEq(id)).uniqueResult(); cerrarTodo();/* ww w . j a v a2s . c o m*/ return a; }
From source file:org.unitec.examenbimestral.Apoyo.java
public Apoyo2 buscarPorId(Integer id) throws Exception { Apoyo2 u = (Apoyo2) session.createCriteria(Apoyo2.class).add(Restrictions.idEq(id)).uniqueResult(); cerrarTodo();//from ww w . j a v a 2s .c o m return u; }
From source file:org.unitec.examenbimestral.Apoyo.java
public void eliminar(Integer id) throws Exception { Apoyo2 u = (Apoyo2) session.createCriteria(Apoyo2.class).add(Restrictions.idEq(1)).uniqueResult(); session.delete(u);//ww w . java 2 s.co m cerrarTodo(); }
From source file:org.webical.dao.hibernateImpl.CalendarDaoHibernateImpl.java
License:Open Source License
@Transaction(readOnly = true) public Calendar getCalendarForEvent(Event event) throws DaoException { if (event.getCalendar() == null || event.getCalendar().getCalendarId() == null) { return null; } else {/* ww w . j ava2s .co m*/ try { return (Calendar) getSession().createCriteria(Calendar.class) .add(Restrictions.idEq(event.getCalendar().getCalendarId())).uniqueResult(); } catch (Exception e) { log.error("Could not decrypt calendars password", e); throw new DaoException("Could not decrypt calendars password", e); } } }
From source file:org.webical.dao.hibernateImpl.EventDaoWebDavHibernateBufferedImpl.java
License:Open Source License
@Transaction(readOnly = false) @SuppressWarnings("unchecked") public void storeEvent(Event updatedEvent) throws DaoException, DeleteConflictException, UpdateConflictException { if (updatedEvent == null) return;//from w w w. j a va2s. c o m try { boolean update = (updatedEvent.getUid() != null); // Create a new WebDavCalendarSynchronisation util WebDavCalendarSynchronisation calendarSynchronisation = new WebDavCalendarSynchronisation(); if (update) { if (log.isDebugEnabled()) { log.debug("Merging existing Event: " + updatedEvent.getUid()); } //Remove the old events (except the updated event) Criteria criteria = getSession().createCriteria(Event.class); criteria.add(Restrictions.eq(Calendar.CALENDAR_PROPERTY_NAME, updatedEvent.getCalendar())); criteria.add(Restrictions.not(Restrictions.idEq(updatedEvent.getEventId()))); List<Event> eventsToRemove = criteria.list(); if (eventsToRemove != null) { deleteAll(eventsToRemove); } // Synchronize the updated event List<Event> newEvents = calendarSynchronisation.updateEvent(updatedEvent, updatedEvent.getCalendar()); //Store the new events if (newEvents != null) { for (Event event : newEvents) { saveOrUpdate(event); } } // Write changes to remote calendar calendarSynchronisation.writeToRemoteCalendarFile(updatedEvent.getCalendar(), newEvents); } else { updatedEvent.setUid(generateUid()); if (log.isDebugEnabled()) { log.debug("Storing new Event: " + updatedEvent.getUid()); } saveOrUpdate(updatedEvent); // Calendar Ical4J over webdav has its own transactions? calendarSynchronisation.writeToRemoteCalendarFile(updatedEvent.getCalendar(), getEventsForCalendar(updatedEvent.getCalendar())); } } catch (Exception e) { log.error(e, e); throw new DaoException("Could not store Event", e); } }