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

x33g5p2x  于2022-01-24 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(237)

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

NativeQuery.getSingleResult介绍

暂无

代码示例

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

"where p.id = :personId" )
.setParameter( "personId", 1L )
.getSingleResult();
  "where p.id = :personId" )
.setParameter( "personId", 1L )
.getSingleResult();
  "where p.id = :personId" )
.setParameter( "personId", 1L )
.getSingleResult();

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

@Test
public void testFlushAutoSQLNativeSession() {
  doInHibernate( this::sessionFactory, session -> {
    session.createNativeQuery( "delete from Person" ).executeUpdate();;
  } );
  doInHibernate( this::sessionFactory, session -> {
    log.info( "testFlushAutoSQLNativeSession" );
    //tag::flushing-auto-flush-sql-native-example[]
    assertTrue(((Number) session
        .createNativeQuery( "select count(*) from Person")
        .getSingleResult()).intValue() == 0 );
    Person person = new Person( "John Doe" );
    session.persist( person );
    assertTrue(((Number) session
        .createNativeQuery( "select count(*) from Person")
        .uniqueResult()).intValue() == 0 );
    //end::flushing-auto-flush-sql-native-example[]
  } );
}

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

@Test
  public void testConverterIsAppliedToElementCollection() {
    doInHibernate( this::sessionFactory, session -> {
      Company company = new Company();
      company.setId( 1L );

      Organization org1 = new Organization();
      org1.setOrganizationId( "ACME" );

      company.getOrganizations().add( org1 );

      session.persist( company );
    } );

    doInHibernate( this::sessionFactory, session -> {
      String organizationId = (String) session
          .createNativeQuery( "select organizations from Company_organizations" )
          .getSingleResult();
      assertEquals( "ORG-ACME", organizationId );

      Company company = session.find( Company.class, 1L );

      assertEquals( 1, company.getOrganizations().size() );
      assertEquals( "ACME" , company.getOrganizations().get( 0 ).getOrganizationId());
    } );
  }
}

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

@Test
public void testNativeQuery() {
  BitSet bitSet = BitSet.valueOf( new long[] {1, 2, 3} );
  doInHibernate( this::sessionFactory, session -> {
    Product product = new Product( );
    product.setId( 1 );
    product.setBitSet( bitSet );
    session.persist( product );
  } );
  doInHibernate( this::sessionFactory, session -> {
    Product product = (Product) session.getNamedNativeQuery(
        "find_person_by_bitset")
        .setParameter( "id", 1L)
        .getSingleResult();
    assertEquals(bitSet, product.getBitSet());
  } );
}

代码示例来源:origin: jpos/jPOS-EE

public int getMaxIdLength() {
    String queryString = "select max(length(id)) as maxidlen from sysconfig";
    if (prefix != null)
      queryString += " where id like :query";
    NativeQuery query =  db.session().createNativeQuery(queryString);
    if (prefix != null)
      query.setParameter ("query", prefix + "%");
    return (int) query.addScalar("maxidlen",  IntegerType.INSTANCE).getSingleResult();
  }
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
  public void testFetchJsonProperty() {
    doInJPA(entityManager -> {
      JsonNode properties = (JsonNode) entityManager
      .createNativeQuery(
        "SELECT properties " +
        "FROM book " +
        "WHERE isbn = :isbn")
      .setParameter("isbn", "978-9730228236")
      .unwrap(org.hibernate.query.NativeQuery.class)
      .addScalar("properties", JsonNodeBinaryType.INSTANCE)
      .getSingleResult();

      assertEquals(
        "High-Performance Java Persistence",
        properties.get("title").asText()
      );
    });
  }
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testFlushAutoNativeSQLFlushModeAlways() {
  doInJPA(entityManager -> {
    assertEquals(
      0,
      ((Number)
        entityManager
        .createNativeQuery(
          "select count(*) " +
          "from post")
        .getSingleResult()
      ).intValue()
    );
    Post post = new Post("High-Performance Java Persistence");
    entityManager.persist(post);
    int postCount = ((Number)
    entityManager
    .unwrap(Session.class)
    .createNativeQuery(
      "select count(*) " +
      "from post")
    .setFlushMode(FlushMode.ALWAYS)
    .getSingleResult()).intValue();
    assertEquals(1, postCount);
  });
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testFlushAutoNativeSQLSynchronizedEntityClass() {
  doInJPA(entityManager -> {
    assertEquals(
      0,
      ((Number)
        entityManager
        .createNativeQuery(
          "select count(*) " +
          "from post")
        .getSingleResult()
      ).intValue()
    );
    Post post = new Post("High-Performance Java Persistence");
    entityManager.persist(post);
    int postCount = ((Number)
    entityManager
    .unwrap(Session.class)
    .createNativeQuery(
      "select count(*) " +
      "from post")
    .addSynchronizedEntityClass(Post.class)
    .getSingleResult()).intValue();
    assertEquals(1, postCount);
  });
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

"from post")
.addSynchronizedQuerySpace("post")
.getSingleResult()).intValue();

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testParentForKeyShareConflictingFkConcurrentUpdate() throws SQLException {
  AtomicBoolean prevented = new AtomicBoolean();
  doInHibernate( session -> {
    session.unwrap(Session.class).doWork(this::prepareConnection);
    Number _postCommentId = (Number) session.createNativeQuery(
        "select post_id " +
        "from post_comment " +
        "where id = :id " +
        "for update")
    .setParameter("id", 1L)
    .getSingleResult();
    executeAsync(() -> {
      doInHibernate(_session -> {
        _session.unwrap(Session.class).doWork(this::prepareConnection);
        PostComment postComment = _session.getReference(PostComment.class, _postCommentId.longValue());
        postComment.setPost(null);
        aliceLatch.countDown();
        _session.flush();
        LOGGER.info("Update on conflicting FK column {} prevented by explicit parent lock", prevented.get() ? "was" : "was not");
        bobLatch.countDown();
      });
    });
    awaitOnLatch(aliceLatch);
    sleep(WAIT_MILLIS);
    LOGGER.info("Alice woke up!");
    prevented.set(true);
  } );
  awaitOnLatch(bobLatch);
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testParentForKeyShareConflictingNoFkConcurrentUpdate() throws SQLException {
  AtomicBoolean prevented = new AtomicBoolean();
  doInHibernate( session -> {
    session.unwrap(Session.class).doWork(this::prepareConnection);
    String _postTitle = (String) session.createNativeQuery(
        "select title " +
        "from post " +
        "where id = :id " +
        "for key share")
    .setParameter("id", 1L)
    .getSingleResult();
    executeAsync(() -> {
      doInHibernate(_session -> {
        _session.unwrap(Session.class).doWork(this::prepareConnection);
        Post post = _session.getReference(Post.class, 1L);
        post.setTitle("High-Performance Hibernate");
        aliceLatch.countDown();
        _session.flush();
        LOGGER.info("Update on conflicting column {} prevented by explicit parent lock", prevented.get() ? "was" : "was not");
        bobLatch.countDown();
      });
    });
    awaitOnLatch(aliceLatch);
    sleep(WAIT_MILLIS);
    LOGGER.info("Alice woke up!");
    prevented.set(true);
  } );
  awaitOnLatch(bobLatch);
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testParentForNoKeyUpdatePreventsConcurrentUpdate() throws SQLException {
  AtomicBoolean prevented = new AtomicBoolean();
  doInHibernate( session -> {
    session.unwrap(Session.class).doWork(this::prepareConnection);
    Number _postId = (Number) session.createNativeQuery(
        "select id " +
        "from Post " +
        "where id = :id " +
        "for no key update")
    .setParameter("id", 1L)
    .getSingleResult();
    executeAsync(() -> {
      doInHibernate(_session -> {
        _session.unwrap(Session.class).doWork(this::prepareConnection);
        Post post = _session.getReference(Post.class, 1L);
        post.setTitle("High-Performance Hibernate");
        aliceLatch.countDown();
        _session.flush();
        LOGGER.info("Update on non-conflicting column {} prevented by explicit parent lock", prevented.get() ? "was" : "was not");
        bobLatch.countDown();
      });
    });
    awaitOnLatch(aliceLatch);
    sleep(WAIT_MILLIS);
    LOGGER.info("Alice woke up!");
    prevented.set(true);
  } );
  awaitOnLatch(bobLatch);
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testParentForKeyShareNonConflictingConcurrentUpdate() throws SQLException {
  AtomicBoolean prevented = new AtomicBoolean();
  doInHibernate( session -> {
    session.unwrap(Session.class).doWork(this::prepareConnection);
    Number _postId = (Number) session.createNativeQuery(
        "select id " +
        "from Post " +
        "where id = :id " +
        "for key share")
    .setParameter("id", 1L)
    .getSingleResult();
    executeAsync(() -> {
      doInHibernate(_session -> {
        _session.unwrap(Session.class).doWork(this::prepareConnection);
        Post post = _session.getReference(Post.class, 1L);
        post.setTitle("High-Performance Hibernate");
        aliceLatch.countDown();
        _session.flush();
        LOGGER.info("Update on non-conflicting column {} prevented by explicit parent lock", prevented.get() ? "was" : "was not");
        bobLatch.countDown();
      });
    });
    awaitOnLatch(aliceLatch);
    sleep(WAIT_MILLIS);
    LOGGER.info("Alice woke up!");
    prevented.set(true);
  } );
  awaitOnLatch(bobLatch);
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

"for no key update", Post.class)
.setParameter("id", 1L)
.getSingleResult();

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

"for update")
.setParameter("id", 1L)
.getSingleResult();

相关文章