Example usage for org.hibernate.criterion Restrictions idEq

List of usage examples for org.hibernate.criterion Restrictions idEq

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions idEq.

Prototype

public static Criterion idEq(Object value) 

Source Link

Document

Apply an "equal" constraint to the identifier property

Usage

From source file:org.emonocot.persistence.dao.hibernate.DaoImpl.java

License:Open Source License

/**
 * @param id// w  w w.  j  a  v  a  2s. co m
 *            Set the id
 * @param fetch
 *            Set the fetch profile (can be null)
 * @return the loaded object
 */
public T load(final Long id, final String fetch) {

    Criteria criteria = getSession().createCriteria(type).add(Restrictions.idEq(id));

    enableProfilePreQuery(criteria, fetch);

    T t = (T) criteria.uniqueResult();

    if (t == null) {
        throw new HibernateObjectRetrievalFailureException(
                new UnresolvableObjectException(id, "Object could not be resolved"));
    }
    enableProfilePostQuery(t, fetch);
    return t;
}

From source file:org.emonocot.persistence.dao.hibernate.DaoImpl.java

License:Open Source License

/**
 * @param id/*from  w w  w.jav a 2s. c om*/
 *            Set the id
 * @param fetch
 *            Set the fetch profile
 * @return the object or null if it cannot be found
 */
public T find(final Long id, final String fetch) {
    Criteria criteria = getSession().createCriteria(type).add(Restrictions.idEq(id));
    enableProfilePreQuery(criteria, fetch);
    T t = (T) criteria.uniqueResult();
    enableProfilePostQuery(t, fetch);

    return t;
}

From source file:org.faster.orm.service.hibernate.HibernateExistsService.java

License:Open Source License

@Override
public boolean exists(ID id) {
    DetachedCriteria dc = buildCriteria();
    dc.add(Restrictions.idEq(id));
    return existsByCriteria(dc);
}

From source file:org.faster.orm.service.hibernate.HibernateValidateService.java

License:Open Source License

@Override
public void validatesUniquenessOf(PO po, String propertyName, ID excludeId, Errors<PO> errors) {
    Object value = Beans.getProperty(po, propertyName);
    if (Beans.isNullOrEmpty(value)) {
        return;/*from ww  w  .j  a va 2 s  . c o m*/
    }

    DetachedCriteria dc = buildCriteriaByPropertyAndValue(propertyName, value);
    if (excludeId != null) {
        dc.add(Restrictions.not(Restrictions.idEq(excludeId)));
    }

    if (existsByCriteria(dc)) {
        errors.addErrorCode(propertyName.toUpperCase() + "_ALREADY_EXISTS");
    }
}

From source file:org.fosstrak.epcis.repository.capture.CaptureOperationsModule.java

License:Open Source License

/**
 * (nkef) Inserts vocabulary attribute into the database by searching for
 * already existing entries; if found, the corresponding ID is returned. If
 * not found, the vocabulary is extended if "insertmissingvoc" is true;
 * otherwise an SQLException is thrown//from ww  w  . j a  v  a 2  s  .c o m
 * 
 * @param tableName
 *            The name of the vocabulary table.
 * @param uri
 *            The vocabulary adapting the URI to be inserted into the
 *            vocabulary table.
 * @return The ID of an already existing vocabulary table with the given
 *         uri.
 * @throws UnsupportedOperationException
 *             If we are not allowed to insert a missing vocabulary.
 */
public VocabularyAttributeElement getOrEditVocabularyAttributeElement(Session session, String vocabularyType,
        Long vocabularyElementID, String vocabularyAttributeElement, String vocabularyAttributeElementValue,
        String mode) throws SAXException {

    boolean deleteAttribute = false;

    if (mode.equals("3")) {
        deleteAttribute = true;
    }
    Class<?> c = vocAttributeClassMap.get(vocabularyType);
    Criteria c0 = session.createCriteria(c);
    c0.setCacheable(true);

    VocabularyAttrCiD vocabularyAttrCiD = new VocabularyAttrCiD();
    vocabularyAttrCiD.setAttribute(vocabularyAttributeElement);
    vocabularyAttrCiD.setId(vocabularyElementID);

    c0.add(Restrictions.idEq(vocabularyAttrCiD));

    VocabularyAttributeElement vocAttributeElement = null;

    try {
        vocAttributeElement = (VocabularyAttributeElement) c0.uniqueResult();
    } catch (ObjectNotFoundException e) {
        vocAttributeElement = null;
        e.printStackTrace();
    }

    if (vocAttributeElement == null || (deleteAttribute && (vocAttributeElement != null))
            || vocAttributeElement != null) {
        // the uri does not yet exist: insert it if allowed. According to
        // the specs, some vocabulary is not allowed to be extended; this is
        // currently ignored here
        if (!insertMissingVoc) {
            throw new UnsupportedOperationException(
                    "Not allowed to add new vocabulary - use existing vocabulary");
        } else {
            // VocabularyAttributeElement subclasses should always have
            // public zero-arg constructor to avoid problems here
            try {
                vocAttributeElement = (VocabularyAttributeElement) c.newInstance();
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            vocAttributeElement.setVocabularyAttrCiD(vocabularyAttrCiD);
            vocAttributeElement.setValue(vocabularyAttributeElementValue);

            if (vocAttributeElement == null) {
                session.save(vocAttributeElement);
            }

            else if (deleteAttribute) {
                Object vocabularyAttr = session.get(c, vocabularyAttrCiD);
                if (vocabularyAttr != null)
                    session.delete(vocabularyAttr);
                session.flush();
                return null;
            } else {
                session.merge(vocAttributeElement);
            }

            session.flush();
        }
    }

    return vocAttributeElement;
}

From source file:org.geoserver.catalog.hib.HibCatalogFacade.java

License:Open Source License

public LayerInfo detach(LayerInfo layer) {
    Session s = sessionFactory.getCurrentSession();
    layer = (LayerInfo) s.createCriteria(LayerInfo.class).setFetchMode("styles", FetchMode.JOIN)
            .setFetchMode("defaultStyle", FetchMode.JOIN).setFetchMode("attribution", FetchMode.JOIN)
            .add(Restrictions.idEq(layer.getId())).uniqueResult();

    return layer;
}

From source file:org.gluewine.persistence_jpa_hibernate.impl.HibernateTransactionalSessionImpl.java

License:Apache License

@Override
public Object get(Class<?> cl, Serializable id) {
    Criteria cr = createCriteria(cl);
    cr.add(Restrictions.idEq(id));
    return cr.uniqueResult();
}

From source file:org.granite.test.tide.hibernate.data.AbstractTestHibernate3ChangeSetPublisher.java

License:Open Source License

@SuppressWarnings("unchecked")
protected <T> T find(Class<T> entityClass, Serializable id) {
    Criteria criteria = session.createCriteria(entityClass);
    criteria.add(Restrictions.idEq(id));
    return (T) criteria.uniqueResult();
}

From source file:org.granite.test.tide.hibernate.data.TestHibernate3DataPublish.java

License:Open Source License

@SuppressWarnings("unchecked")
protected <T> T find(Class<T> entityClass, Serializable id) {
    Criteria c = session.createCriteria(entityClass);
    c.add(Restrictions.idEq(id));
    return (T) c.uniqueResult();
}

From source file:org.granite.test.tide.spring.AbstractTestTideLazyLoadingHibernate.java

License:Open Source License

@Test
public void testLazyLoading() {
    TransactionDefinition def = new DefaultTransactionDefinition();

    TransactionStatus tx = txManager.getTransaction(def);
    Person person = new Person();
    person.initUid();//  w ww  . ja va 2s . c  o  m
    sessionFactory.getCurrentSession().persist(person);
    txManager.commit(tx);

    tx = txManager.getTransaction(def);
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Person.class);
    criteria.add(Restrictions.idEq(person.getId()));
    person = (Person) criteria.uniqueResult();
    txManager.commit(tx);

    Person result = (Person) initializeObject(person, new String[] { "contacts" });

    ClassGetter classGetter = ((ConvertersConfig) GraniteContext.getCurrentInstance().getGraniteConfig())
            .getClassGetter();
    Assert.assertTrue("Person initialized", classGetter.isInitialized(null, null, result));
    Assert.assertTrue("Collection initialized",
            classGetter.isInitialized(result, "contacts", result.getContacts()));

    Assert.assertEquals("Sessions closed", sessionFactory.getStatistics().getSessionOpenCount(),
            sessionFactory.getStatistics().getSessionCloseCount());
}