本文整理了Java中org.hibernate.query.Query.uniqueResultOptional
方法的一些代码示例,展示了Query.uniqueResultOptional
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.uniqueResultOptional
方法的具体详情如下:
包路径:org.hibernate.query.Query
类名称: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));
}
内容来源于网络,如有侵权,请联系作者删除!