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

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

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

Query.uniqueResultOptional介绍

暂无

代码示例

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

@Test
public void testUniqueResult() {
  final String entityName = "expected";
  try (Session session = openSession()) {
    final CriteriaQuery<TestEntity> query = createTestEntityCriteriaQuery( entityName, session );
    final Optional<TestEntity> result = session.createQuery( query ).uniqueResultOptional();
    assertThat( result.isPresent(), is( false ) );
  }
}

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

@Test
  public void testNullQueryResult() {
    Session s = openSession();
    s.beginTransaction();

    assertNull( s.createQuery( "select u from User u where u.id = -1" ).uniqueResult() );

    Optional<User> user = s.createQuery( "select u from User u where u.id = -1" ).uniqueResultOptional();
    assertNotNull( user );
    assertFalse( user.isPresent() );
    try {
      user.get();
      fail( "Expecting call to Optional#get to throw NoSuchElementException" );
    }
    catch (NoSuchElementException expected) {
      // the expected result...
    }

    s.getTransaction().commit();
    s.close();

  }
}

代码示例来源:origin: com.atlassian.crowd/crowd-persistence-hibernate5

@Override
@SuppressWarnings("unchecked")
public DirectoryMapping findDirectoryMapping(long applicationId, long directoryId) throws DirectoryMappingNotFoundException {
  final Optional<DirectoryMapping> maybeDirectoryMapping = session().getNamedQuery("findDirectoryMapping")
      .setLong("applicationId", applicationId)
      .setLong("directoryId", directoryId)
      .uniqueResultOptional();
  return maybeDirectoryMapping.orElseThrow(() -> new DirectoryMappingNotFoundException(applicationId, directoryId));
}

相关文章