Example usage for org.hibernate Criteria uniqueResult

List of usage examples for org.hibernate Criteria uniqueResult

Introduction

In this page you can find the example usage for org.hibernate Criteria uniqueResult.

Prototype

public Object uniqueResult() throws HibernateException;

Source Link

Document

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Usage

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.EventDAO.java

License:Apache License

public EventPE tryFind(String identifier, EntityType entityType, EventType eventType) {
    assert identifier != null : "Unspecified identifier.";
    assert entityType != null : "Unspecified entityType.";
    assert eventType != null : "Unspecified eventType.";

    final Criteria criteria = getSession().createCriteria(EventPE.class);
    criteria.add(Restrictions.eq("identifier", identifier));
    criteria.add(Restrictions.eq("entityType", entityType));
    criteria.add(Restrictions.eq("eventType", eventType));
    final EventPE result = tryGetEntity(criteria.uniqueResult());
    if (operationLog.isDebugEnabled()) {
        String methodName = MethodUtils.getCurrentMethod().getName();
        operationLog.debug(String.format("%s: '%s'.", methodName, result));
    }//w  w  w .  j  a  v  a2 s.  c  o  m
    return result;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.ExperimentDAO.java

License:Apache License

public ExperimentPE tryFindByCodeAndProject(final ProjectPE project, final String experimentCode) {
    assert experimentCode != null : "Unspecified experiment code.";
    assert project != null : "Unspecified project.";

    final Criteria criteria = getSession().createCriteria(getEntityClass());
    criteria.add(Restrictions.eq("code", CodeConverter.tryToDatabase(experimentCode)));
    criteria.add(Restrictions.eq("projectInternal", project));
    criteria.setFetchMode("experimentType.experimentTypePropertyTypesInternal", FetchMode.JOIN);
    final ExperimentPE experiment = (ExperimentPE) criteria.uniqueResult();
    if (operationLog.isDebugEnabled()) {
        operationLog/*w w  w  .  j a  va 2s. c  o  m*/
                .debug(String.format("Following experiment '%s' has been found for code '%s' and project '%s'.",
                        experiment, experimentCode, project));
    }
    return experiment;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.MaterialDAO.java

License:Apache License

public MaterialPE tryFindMaterial(MaterialIdentifier identifier) {
    assert identifier != null : "identifier not given";

    String code = CodeConverter.tryToDatabase(identifier.getCode());
    String typeCode = CodeConverter.tryToDatabase(identifier.getTypeCode());

    final Criteria criteria = getSession().createCriteria(ENTITY_CLASS);
    criteria.add(Restrictions.eq("code", code));
    criteria.createCriteria("materialType").add(Restrictions.eq("code", typeCode));
    criteria.setFetchMode("materialType.materialTypePropertyTypesInternal", FetchMode.JOIN);
    final MaterialPE material = (MaterialPE) criteria.uniqueResult();
    if (operationLog.isDebugEnabled()) {
        operationLog/*  www  . j  a va  2 s. co m*/
                .debug(String.format("Following material '%s' has been found for " + "code '%s' and type '%s'.",
                        material, code, typeCode));
    }
    return material;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.ProjectDAO.java

License:Apache License

public ProjectPE tryFindProject(final String databaseInstanceCode, final String groupCode,
        final String projectCode) {
    assert projectCode != null : "Unspecified project code.";
    assert groupCode != null : "Unspecified group code.";

    final Criteria criteria = getSession().createCriteria(ProjectPE.class);
    criteria.add(Restrictions.eq("code", CodeConverter.tryToDatabase(projectCode)));
    final Criteria groupCriteria = criteria.createCriteria("group");
    groupCriteria.add(Restrictions.eq("code", CodeConverter.tryToDatabase(groupCode)));
    if (StringUtils.isBlank(databaseInstanceCode)) {
        groupCriteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    } else {//from w w w . j a v a  2  s .co m
        groupCriteria.createCriteria("databaseInstance")
                .add(Restrictions.eq("code", CodeConverter.tryToDatabase(databaseInstanceCode)));
    }
    final ProjectPE project = (ProjectPE) criteria.uniqueResult();
    return project;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.SampleDAO.java

License:Apache License

public SamplePE tryToFindByPermID(String permID) throws DataAccessException {
    assert permID != null : "Unspecified permanent ID.";
    final Criteria criteria = getSession().createCriteria(ENTITY_CLASS);
    criteria.add(Restrictions.eq("permId", permID));
    criteria.setFetchMode("sampleType.sampleTypePropertyTypesInternal", FetchMode.JOIN);
    final SamplePE sample = (SamplePE) criteria.uniqueResult();
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("Following sample '%s' has been found for " + "permanent ID '%s'.",
                sample, permID));// w ww .j  a v  a  2  s  .c  o  m
    }
    return sample;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.SampleDAO.java

License:Apache License

public final SamplePE tryFindByCodeAndDatabaseInstance(final String sampleCode,
        final DatabaseInstancePE databaseInstance) {
    assert sampleCode != null : "Unspecified sample code.";
    assert databaseInstance != null : "Unspecified database instance.";

    final Criteria criteria = getSession().createCriteria(ENTITY_CLASS);
    addSampleCodeCriterion(criteria, sampleCode);
    criteria.add(Restrictions.eq("databaseInstance", databaseInstance));
    criteria.setFetchMode("sampleType.sampleTypePropertyTypesInternal", FetchMode.JOIN);
    final SamplePE sample = (SamplePE) criteria.uniqueResult();
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format(
                "Following sample '%s' has been found for " + "code '%s' and database instance '%s'.", sample,
                sampleCode, databaseInstance));
    }/*from  w  w  w  .  j a  va  2  s  .c om*/
    return sample;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.SampleDAO.java

License:Apache License

public final SamplePE tryFindByCodeAndGroup(final String sampleCode, final GroupPE group) {
    assert sampleCode != null : "Unspecified sample code.";
    assert group != null : "Unspecified group.";

    final Criteria criteria = getSession().createCriteria(ENTITY_CLASS);
    addSampleCodeCriterion(criteria, sampleCode);
    criteria.add(Restrictions.eq("group", group));
    criteria.setFetchMode("sampleType.sampleTypePropertyTypesInternal", FetchMode.JOIN);
    final SamplePE sample = (SamplePE) criteria.uniqueResult();
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("Following sample '%s' has been found for code '%s' and group '%s'.",
                sample, sampleCode, group));
    }//from  www. j a va 2 s  .  co m
    return sample;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.VocabularyDAO.java

License:Apache License

public VocabularyTermPE tryFindVocabularyTermByCode(VocabularyPE vocabulary, String code) {
    assert vocabulary != null : "Unspecified vocabulary.";
    assert code != null : "Unspecified code.";

    final Criteria criteria = getSession().createCriteria(VocabularyTermPE.class);
    criteria.add(Restrictions.eq("code", code));
    criteria.add(Restrictions.eq("vocabularyInternal", vocabulary));
    final VocabularyTermPE result = tryGetEntity(criteria.uniqueResult());
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%s(%s): '%s'.", vocabulary.getCode(), code, result));
    }//w  w  w. jav a  2 s  .c  om
    return result;
}

From source file:chat.service.DoChat.java

License:LGPL

/**
 * also persist SO//from w w  w.ja v  a 2s .  c om
 *
 * @return with {@link Chat#datime}
 */
@Service
public Chat post(Chat c, Input.Upload smiley) throws Exception {
    c.out = new User().id(sess.me);
    validate(c);

    Criteria<?> t = data.criteria(User.class).setProjection(Projections.rowCount());
    t.add(Restrictions.idEq(c.in.id));
    t.createCriteria("friends").add(Restrictions.idEq(c.out.id));
    if ((Integer) t.uniqueResult() == 0)
        throw err("You must be his/her friend");
    c.datime = new Date();
    while (smiley != null && smiley.available() > 0) {
        final Bytes b = new Bytes(smiley, false);
        final ByteArrayInputStream in = new ByteArrayInputStream(b.bytes, b.beginBi, b.byteN());
        Smiley s = new Smiley();
        s.in = c.in;
        s.image = new Blob() {
            @Override
            public long length() {
                return b.byteN();
            }

            @Override
            public void truncate(long pos) {
                throw new UnsupportedOperationException();
            }

            @Override
            public byte[] getBytes(long pos, int len) {
                throw new UnsupportedOperationException();
            }

            @Override
            public int setBytes(long pos, byte[] bytes) {
                throw new UnsupportedOperationException();
            }

            @Override
            public int setBytes(long pos, byte[] bytes, int i, int j) {
                throw new UnsupportedOperationException();
            }

            @Override
            public long position(byte[] bytes, long pos) {
                throw new UnsupportedOperationException();
            }

            @Override
            public InputStream getBinaryStream() {
                in.reset();
                return in;
            }

            @Override
            public OutputStream setBinaryStream(long pos) {
                throw new UnsupportedOperationException();
            }

            @Override
            public long position(Blob blob, long pos) {
                throw new UnsupportedOperationException();
            }

            @Override
            public void free() {
            }

            @Override
            public InputStream getBinaryStream(long pos, long length) {
                throw new UnsupportedOperationException();
            }
        };
        s.type = smiley.type();
        if (c.smileys == null)
            c.smileys = new ArrayList<Smiley>();
        c.smileys.add(s);
        smiley.next();
    }
    data.save(c);
    return c;
}

From source file:Clases.ClienteHelper.java

public Cliente findByCI(String CI) {
    Cliente cliente = null;/* w ww  .ja v a2  s  .  co m*/
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Cliente.class).add(Restrictions.eq("CI", CI));
        Object result = criteria.uniqueResult();
        if (result != null) {
            cliente = (Cliente) result;
        }
    } catch (Exception e) {
        throw e;
    } finally {
        session.close();
    }
    return cliente;
}