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

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

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

Query.uniqueResult介绍

[英]Convenience method to return a single instance that matches the query, or null if the query returns no results.
[中]方法返回与查询匹配的单个实例,如果查询不返回结果,则返回null。

代码示例

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

  1. default R getSingleResult() {
  2. return uniqueResult();
  3. }

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

  1. /**
  2. * Convenience method to return a single instance that matches the query, or null if the query
  3. * returns no results.
  4. *
  5. * @param query the query to run
  6. * @return the single result or {@code null}
  7. * @throws HibernateException if there is more than one matching result
  8. * @see Query#uniqueResult()
  9. */
  10. protected E uniqueResult(Query<E> query) throws HibernateException {
  11. return requireNonNull(query).uniqueResult();
  12. }

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

  1. private void assertCount(long expected){
  2. long count = (Long) session.createQuery("select count(h) from Human h").uniqueResult();
  3. Assert.assertEquals(expected, count);
  4. }

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

  1. private <T> T getDerivedClassById(Employee e, Session s, Class<T> clazz) {
  2. return clazz.cast( s.createQuery( "from " + clazz.getName() + " d where d.emp.empId = :empId" )
  3. .setParameter( "empId", e.empId ).uniqueResult() );
  4. }

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

  1. @Test
  2. public void testJpqlBooleanLiteral() {
  3. Session session = openSession();
  4. session.getTransaction().begin();
  5. assertNotNull( session.createQuery( "from Employee e where e.active = true" ).uniqueResult() );
  6. assertNull( session.createQuery( "from Employee e where e.active = false" ).uniqueResult() );
  7. session.getTransaction().commit();
  8. session.close();
  9. }

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

  1. @Test
  2. public void testFilter() {
  3. try (Session session = openSession()) {
  4. Assert.assertEquals(
  5. Long.valueOf( 4 ),
  6. session.createQuery( "select count(u) from User u" ).uniqueResult()
  7. );
  8. session.enableFilter( "ageFilter" ).setParameter( "age", 24 );
  9. Assert.assertEquals(
  10. Long.valueOf( 2 ),
  11. session.createQuery( "select count(u) from User u" ).uniqueResult()
  12. );
  13. }
  14. }

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

  1. @Test
  2. public void testJpqlFloatLiteral() {
  3. Session session = openSession();
  4. session.getTransaction().begin();
  5. Employee jDoe = (Employee) session.createQuery( "from Employee e where e.salary = " + SALARY + "f" ).uniqueResult();
  6. assertNotNull( jDoe );
  7. session.getTransaction().commit();
  8. session.close();
  9. }

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

  1. @Test
  2. public void testQueryUsingLockOptions() {
  3. // todo : need an association here to make sure the alias-specific lock modes are applied correctly
  4. doInHibernate( this::sessionFactory, session -> {
  5. session.createQuery( "from A a" )
  6. .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
  7. .uniqueResult();
  8. session.createQuery( "from A a" )
  9. .setLockOptions( new LockOptions().setAliasSpecificLockMode( "a", LockMode.PESSIMISTIC_WRITE ) )
  10. .uniqueResult();
  11. } );
  12. }

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

  1. @Test
  2. public void shouldRetrieveSubSubEntityWithHQL() {
  3. session = openSession();
  4. try {
  5. SubSubEntity loaded = (SubSubEntity) session.createQuery(
  6. "select se from SubSubEntity se where se.id = :id" )
  7. .setLong( "id", subSubEntityId )
  8. .uniqueResult();
  9. assertNotNull( loaded );
  10. }
  11. finally {
  12. session.close();
  13. }
  14. }

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

  1. @Test
  2. public void shouldNotRetrieveSubSubSubEntityWithHQL() {
  3. session = openSession();
  4. try {
  5. SubSubSubEntity loaded = (SubSubSubEntity) session.createQuery(
  6. "select se from SubSubSubEntity se where se.id = :id" )
  7. .setLong( "id", subSubEntityId )
  8. .uniqueResult();
  9. assertNull( loaded );
  10. }
  11. finally {
  12. session.close();
  13. }
  14. }

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

  1. @Test
  2. @TestForIssue(jiraKey = "HHH-13084")
  3. public void testHql() {
  4. doInHibernate( this::sessionFactory, session -> {
  5. assertEquals( 1, session.createQuery( "from Person p where p.id is null", Person.class ).list().size() );
  6. assertEquals( 2, session.createQuery( "from Person p where p.id is not null", Person.class ).list().size() );
  7. assertEquals( 3L, session.createQuery( "select count( p ) from Person p" ).uniqueResult() );
  8. } );
  9. }

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

  1. @Test
  2. @TestForIssue(jiraKey = "HHH-13084")
  3. public void testHql() {
  4. doInHibernate( this::sessionFactory, session -> {
  5. assertEquals( 2, session.createQuery( "from Person p where p.id = 0", Person.class ).list().size() );
  6. assertEquals( 3L, session.createQuery( "select count( p ) from Person p" ).uniqueResult() );
  7. } );
  8. }

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

  1. @Test
  2. public void test_hql_api_unique_result_example() {
  3. doInJPA( this::entityManagerFactory, entityManager -> {
  4. Session session = entityManager.unwrap( Session.class );
  5. //tag::hql-api-unique-result-example[]
  6. Person person = (Person) session.createQuery(
  7. "select p " +
  8. "from Person p " +
  9. "where p.name like :name" )
  10. .setParameter( "name", "J%" )
  11. .uniqueResult();
  12. //end::hql-api-unique-result-example[]
  13. });
  14. }

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

  1. @Test
  2. @TestForIssue( jiraKey = "HHH-11957")
  3. public void testSubstrWithoutStringUnits() {
  4. mostRecentStatementInspector.clear();
  5. doInHibernate(
  6. this::sessionFactory, session -> {
  7. String value = session.createQuery(
  8. "select substr( e.description, 21, 11 ) from AnEntity e",
  9. String.class
  10. ).uniqueResult();
  11. assertEquals( "description", value );
  12. }
  13. );
  14. assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substr(" ) );
  15. }

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

  1. @Test
  2. public void testManagedWithUninitializedAssociation() {
  3. // Delete the Parent
  4. doInHibernate( this::sessionFactory, s -> {
  5. Parent loadedParent = (Parent) s.createQuery( "SELECT p FROM Parent p WHERE name=:name" )
  6. .setParameter( "name", "PARENT" )
  7. .uniqueResult();
  8. checkInterceptor( loadedParent, false );
  9. assertFalse( Hibernate.isPropertyInitialized( loadedParent, "children" ) );
  10. s.delete( loadedParent );
  11. } );
  12. // If the lazy relation is not fetch on cascade there is a constraint violation on commit
  13. }

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

  1. @Test
  2. public void testQuery() {
  3. // open a session, begin a transaction and lock row
  4. doInHibernate( this::sessionFactory, session -> {
  5. A it = (A) session.createQuery( "from A a" )
  6. .setLockMode( "a", LockMode.PESSIMISTIC_WRITE )
  7. .uniqueResult();
  8. // make sure we got it
  9. assertNotNull( it );
  10. // that initial transaction is still active and so the lock should still be held.
  11. // Lets open another session/transaction and verify that we cannot update the row
  12. nowAttemptToUpdateRow();
  13. } );
  14. }

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

  1. @Test(expected = TransactionRequiredException.class)
  2. public void testFlushDisallowingOutOfTransactionUpdateOperations() throws Exception {
  3. allowUpdateOperationOutsideTransaction = "false";
  4. rebuildSessionFactory();
  5. prepareTest();
  6. try (Session s = openSession()) {
  7. final MyEntity entity = (MyEntity) s.createQuery( "from MyEntity e where e.name = :n" )
  8. .setParameter( "n", "entity" )
  9. .uniqueResult();
  10. assertThat( entity, not( nullValue() ) );
  11. entity.setName( "changed" );
  12. session.flush();
  13. }
  14. }

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

  1. @Test(expected = TransactionRequiredException.class)
  2. public void testFlushOutOfTransaction() throws Exception {
  3. allowUpdateOperationOutsideTransaction = "";
  4. rebuildSessionFactory();
  5. prepareTest();
  6. try (Session s = openSession()) {
  7. final MyEntity entity = (MyEntity) s.createQuery( "from MyEntity e where e.name = :n" )
  8. .setParameter( "n", "entity" )
  9. .uniqueResult();
  10. assertThat( entity, not( nullValue() ) );
  11. entity.setName( "changed" );
  12. session.flush();
  13. }
  14. }

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

  1. @Test
  2. public void testFlushAllowingOutOfTransactionUpdateOperations() throws Exception {
  3. allowUpdateOperationOutsideTransaction = "true";
  4. rebuildSessionFactory();
  5. prepareTest();
  6. try (Session s = openSession()) {
  7. final MyEntity entity = (MyEntity) s.createQuery( "from MyEntity e where e.name = :n" )
  8. .setParameter( "n", "entity" )
  9. .uniqueResult();
  10. assertThat( entity, not( nullValue() ) );
  11. entity.setName( "changed" );
  12. session.flush();
  13. }
  14. }

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

  1. @Test
  2. @TestForIssue( jiraKey = "HHH-9637")
  3. public void testExplicitJoinAfterFetchJoins() {
  4. Session s = openSession();
  5. s.getTransaction().begin();
  6. Entity1 e1Queryied =
  7. (Entity1) s.createQuery(
  8. "select e1 from Entity1 e1 inner join fetch e1.entity2 e2 inner join fetch e2.entity3 inner join e1.entity2 e1Restrict where e1Restrict.value = 'entity2'" )
  9. .uniqueResult();
  10. assertEquals( "entity1", e1Queryied.getValue() );
  11. assertTrue( Hibernate.isInitialized( e1Queryied.getEntity2() ) );
  12. assertTrue( Hibernate.isInitialized( e1Queryied.getEntity2().getEntity3() ) );
  13. s.getTransaction().commit();
  14. s.close();
  15. }

相关文章