org.hibernate.Query.uniqueResult()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(359)

本文整理了Java中org.hibernate.Query.uniqueResult方法的一些代码示例,展示了Query.uniqueResult的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.uniqueResult方法的具体详情如下:
包路径:org.hibernate.Query
类名称:Query
方法名:uniqueResult

Query.uniqueResult介绍

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

代码示例

代码示例来源:origin: citerus/dddsample-core

  1. public Cargo find(TrackingId tid) {
  2. return (Cargo) getSession().
  3. createQuery("from Cargo where trackingId = :tid").
  4. setParameter("tid", tid).
  5. uniqueResult();
  6. }

代码示例来源:origin: gocd/gocd

  1. private Agent fetchAgentByUuid(final String uuid) {
  2. return (Agent) getHibernateTemplate().execute(session -> {
  3. Query query = session.createQuery("from Agent where uuid = :uuid");
  4. query.setString("uuid", uuid);
  5. return query.uniqueResult();
  6. });
  7. }
  8. }

代码示例来源:origin: gocd/gocd

  1. Modification findModificationWithRevision(Session session, long materialId, String revision) {
  2. Modification modification;
  3. String key = cacheKeyForModificationWithRevision(materialId, revision);
  4. modification = (Modification) goCache.get(key);
  5. if (modification == null) {
  6. synchronized (key) {
  7. modification = (Modification) goCache.get(key);
  8. if (modification == null) {
  9. Query query = session.createQuery("FROM Modification WHERE materialId = ? and revision = ? ORDER BY id DESC");
  10. query.setLong(0, materialId);
  11. query.setString(1, revision);
  12. modification = (Modification) query.uniqueResult();
  13. goCache.put(key, modification);
  14. }
  15. }
  16. }
  17. return modification;
  18. }

代码示例来源:origin: openmrs/openmrs-core

  1. /**
  2. * @see org.openmrs.hl7.db.HL7DAO#getNextHL7InQueue()
  3. */
  4. @Override
  5. public HL7InQueue getNextHL7InQueue() throws DAOException {
  6. Query query = sessionFactory.getCurrentSession().createQuery(
  7. "from HL7InQueue as hiq where hiq.messageState = ? order by HL7InQueueId").setParameter(0,
  8. HL7Constants.HL7_STATUS_PENDING, StandardBasicTypes.INTEGER).setMaxResults(1);
  9. if (query == null) {
  10. return null;
  11. }
  12. return (HL7InQueue) query.uniqueResult();
  13. }

代码示例来源:origin: org.ow2.bonita/bonita-core

  1. public ProcessVersion getLastProcessVersion(String processId, String packageId) {
  2. Query query = getSession().getNamedQuery("getLastProcessVersion");
  3. query.setCacheable(true);
  4. query.setString("processId", processId);
  5. query.setString("packageId", packageId);
  6. query.setMaxResults(1);
  7. ProcessVersion pv = (ProcessVersion) query.uniqueResult();
  8. return pv;
  9. }

代码示例来源:origin: gocd/gocd

  1. public Long getTotalModificationsFor(final MaterialInstance materialInstance) {
  2. String key = materialModificationCountKey(materialInstance);
  3. Long totalCount = (Long) goCache.get(key);
  4. if (totalCount == null || totalCount == 0) {
  5. synchronized (key) {
  6. totalCount = (Long) goCache.get(key);
  7. if (totalCount == null || totalCount == 0) {
  8. totalCount = (Long) getHibernateTemplate().execute((HibernateCallback) session -> {
  9. Query q = session.createQuery("select count(*) FROM Modification WHERE materialId = ?");
  10. q.setLong(0, materialInstance.getId());
  11. return q.uniqueResult();
  12. });
  13. goCache.put(key, totalCount);
  14. }
  15. }
  16. }
  17. return totalCount;
  18. }

代码示例来源:origin: sakaiproject/sakai

  1. public Integer getCountItemFacades(final Long questionPoolId) {
  2. final HibernateCallback<Number> hcb = session -> {
  3. Query q = session.createQuery("select count(ab) from ItemData ab, QuestionPoolItemData qpi where ab.itemId = qpi.itemId and qpi.questionPoolId = :id");
  4. q.setLong("id", questionPoolId);
  5. q.setCacheable(true);
  6. return (Number) q.uniqueResult();
  7. };
  8. return getHibernateTemplate().execute(hcb).intValue();
  9. }

代码示例来源:origin: gocd/gocd

  1. Modification findLatestModification(final MaterialInstance expandedInstance) {
  2. Modifications modifications = cachedModifications(expandedInstance);
  3. if (modifications != null && !modifications.isEmpty()) {
  4. return modifications.get(0);
  5. }
  6. String cacheKey = latestMaterialModificationsKey(expandedInstance);
  7. synchronized (cacheKey) {
  8. Modification modification = (Modification) getHibernateTemplate().execute((HibernateCallback) session -> {
  9. Query query = session.createQuery("FROM Modification WHERE materialId = ? ORDER BY id DESC");
  10. query.setMaxResults(1);
  11. query.setLong(0, expandedInstance.getId());
  12. return query.uniqueResult();
  13. });
  14. goCache.put(cacheKey, new Modifications(modification));
  15. return modification;
  16. }
  17. }

代码示例来源:origin: hibernate/hibernate-demos

  1. public Project getProject(long id) {
  2. final Session s = openSession();
  3. s.getTransaction().begin();
  4. final Query q = s.createQuery( "FROM Project WHERE id = :id" );
  5. q.setParameter( "id", id );
  6. q.setCacheable( true );
  7. final Project project = (Project) q.uniqueResult();
  8. s.getTransaction().commit();
  9. return project;
  10. }

代码示例来源:origin: org.sakaiproject.common/sakai-common-composite-component

  1. public Object doInHibernate(Session session) throws HibernateException, SQLException
  2. {
  3. Query q = session.getNamedQuery(FINDTYPEBYTUPLE);
  4. q.setString(AUTHORITY, authority);
  5. q.setString(DOMAIN, domain);
  6. q.setString(KEYWORD, keyword);
  7. q.setCacheable(cacheFindTypeByTuple);
  8. q.setCacheRegion(Type.class.getCanonicalName());
  9. return q.uniqueResult();
  10. }
  11. };

代码示例来源:origin: kaaproject/kaa

  1. @Override
  2. public EventClassFamily findByEcfvId(String ecfvId) {
  3. LOG.debug("Searching event class family by ecfv id [{}]", ecfvId);
  4. Query query = getSession().createSQLQuery(
  5. "select ecf.*"
  6. + " from " + EVENT_CLASS_FAMILY_TABLE_NAME + " as ecf"
  7. + " join " + EVENT_CLASS_FAMILY_VERSION_TABLE_NAME + " as ecfv"
  8. + " on ecf.id = ecfv." + EVENT_CLASS_FAMILY_ID
  9. + " where ecfv.id = :id").addEntity(getEntityClass());
  10. query.setLong("id", Long.valueOf(ecfvId));
  11. EventClassFamily eventClassFamily = (EventClassFamily) query.uniqueResult();
  12. LOG.debug("[{}] Search result: {}.", ecfvId, eventClassFamily);
  13. return eventClassFamily;
  14. }

代码示例来源:origin: jtalks-org/jcommune

  1. /**
  2. * {@inheritDoc}
  3. */
  4. @Override
  5. public UserContact getContactById(long id) {
  6. return (UserContact) session()
  7. .createQuery("from UserContact u where u.id = ?")
  8. .setCacheable(true)
  9. .setLong(0, id)
  10. .uniqueResult();
  11. }
  12. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Override
  2. protected Object getResults(Session s, boolean isSingleResult) {
  3. Query query = getQuery( s ).setCacheable( getQueryCacheMode() != CacheMode.IGNORE ).setCacheMode( getQueryCacheMode() );
  4. return ( isSingleResult ? query.uniqueResult() : query.list() );
  5. }
  6. }

代码示例来源:origin: hibernate/hibernate-orm

  1. s.save( bar2 );
  2. List list = s.createQuery(
  3. "from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar %'"
  4. ).list();
  5. assertTrue( row instanceof Object[] && ( (Object[]) row ).length==3 );
  6. Query q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar%'");
  7. list = q.list();
  8. if ( !(getDialect() instanceof SAPDBDialect) ) assertTrue( list.size()==2 );
  9. q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where ( bar.name in (:nameList) or bar.name in (:nameList) ) and bar.string = :stringVal");
  10. HashSet nameList = new HashSet();
  11. nameList.add( "bar" );
  12. q = s.createQuery("select bar, b from Bar bar inner join bar.baz baz inner join baz.cascadingBars b where bar.name like 'Bar%'");
  13. Object result = q.uniqueResult();
  14. assertTrue( result != null );
  15. q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like :name and b.name like :name");
  16. q.setString( "name", "Bar%" );
  17. list = q.list();
  18. assertTrue( list.size()==1 );

代码示例来源:origin: sakaiproject/sakai

  1. protected GradebookAssignment getAssignmentWithoutStats(final String gradebookUid, final Long assignmentId) throws HibernateException {
  2. return (GradebookAssignment) getSessionFactory().getCurrentSession()
  3. .createQuery("from GradebookAssignment as asn where asn.id = :assignmentid and asn.gradebook.uid = :gradebookuid and asn.removed is false")
  4. .setLong("assignmentid", assignmentId)
  5. .setString("gradebookuid", gradebookUid)
  6. .uniqueResult();
  7. }

代码示例来源:origin: netgloo/spring-boot-samples

  1. public User getByEmail(String email) {
  2. return (User) getSession().createQuery(
  3. "from User where email = :email")
  4. .setParameter("email", email)
  5. .uniqueResult();
  6. }

代码示例来源:origin: OpenNMS/opennms

  1. public OnmsNode doInHibernate(Session session) throws HibernateException, SQLException {
  2. Integer nodeId = (Integer)session.createQuery(query2).setMaxResults(1).uniqueResult();
  3. return getNode(nodeId, session);
  4. }
  5. });

代码示例来源:origin: org.ow2.bonita/bonita-core

  1. public PackageFullDefinition getPackage(String packageId, String version) {
  2. final Query query = getSession().getNamedQuery("getPackageFromIdAndVersion");
  3. query.setCacheable(true);
  4. query.setString("packageId", packageId);
  5. query.setString("version", version);
  6. query.setMaxResults(1);
  7. return (PackageFullDefinition) query.uniqueResult();
  8. }

代码示例来源:origin: sakaiproject/sakai

  1. /**
  2. */
  3. public CourseGrade getCourseGrade(final Long gradebookId) {
  4. return (CourseGrade) getSessionFactory().getCurrentSession().createQuery(
  5. "from CourseGrade as cg where cg.gradebook.id = :gradebookid")
  6. .setLong("gradebookid", gradebookId)
  7. .uniqueResult();
  8. }

代码示例来源:origin: sakaiproject/sakai

  1. public long getSubPoolSize(final Long poolId) {
  2. final HibernateCallback<Number> hcb = session -> {
  3. Query q = session.createQuery("select count(qpp) from QuestionPoolData qpp where qpp.parentPoolId = :id");
  4. q.setCacheable(true);
  5. q.setLong("id", poolId);
  6. return (Number) q.uniqueResult();
  7. };
  8. return getHibernateTemplate().execute(hcb).longValue();
  9. }

相关文章