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

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

本文整理了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

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

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

  1. @Test
  2. public void testNullQueryResult() {
  3. Session s = openSession();
  4. s.beginTransaction();
  5. assertNull( s.createQuery( "select u from User u where u.id = -1" ).uniqueResult() );
  6. Optional<User> user = s.createQuery( "select u from User u where u.id = -1" ).uniqueResultOptional();
  7. assertNotNull( user );
  8. assertFalse( user.isPresent() );
  9. try {
  10. user.get();
  11. fail( "Expecting call to Optional#get to throw NoSuchElementException" );
  12. }
  13. catch (NoSuchElementException expected) {
  14. // the expected result...
  15. }
  16. s.getTransaction().commit();
  17. s.close();
  18. }
  19. }

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

  1. @Override
  2. @SuppressWarnings("unchecked")
  3. public DirectoryMapping findDirectoryMapping(long applicationId, long directoryId) throws DirectoryMappingNotFoundException {
  4. final Optional<DirectoryMapping> maybeDirectoryMapping = session().getNamedQuery("findDirectoryMapping")
  5. .setLong("applicationId", applicationId)
  6. .setLong("directoryId", directoryId)
  7. .uniqueResultOptional();
  8. return maybeDirectoryMapping.orElseThrow(() -> new DirectoryMappingNotFoundException(applicationId, directoryId));
  9. }

相关文章