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

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

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

NativeQuery.uniqueResult介绍

暂无

代码示例

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

private int countInsertedRows(Session s) {
  return ((Number) s.createSQLQuery( "SELECT COUNT(*) FROM sequenceIdentifier" )
      .uniqueResult()).intValue();
}

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

private long getTableRowCount(Session s, String tableName) {
  // the type returned for count(*) in a native query depends on the dialect
  // Oracle returns Types.NUMERIC, which is mapped to BigDecimal;
  // H2 returns Types.BIGINT, which is mapped to BigInteger;
  Object retVal = s.createSQLQuery( "select count(*) from " + tableName ).uniqueResult();
  assertTrue( Number.class.isInstance( retVal ) );
  return ( ( Number ) retVal ).longValue();
}

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

@Test
public void testImportFile() throws Exception {
  Session s = openSession();
  final Transaction tx = s.beginTransaction();
  BigInteger count = (BigInteger) s.createSQLQuery( "SELECT COUNT(*) FROM test_data" ).uniqueResult();
  assertEquals( "Incorrect row number", 3L, count.longValue() );
  final String multiLineText = (String) s.createSQLQuery( "SELECT text FROM test_data WHERE id = 2" )
      .uniqueResult();
  //  "Multi-line comment line 1\n-- line 2'\n/* line 3 */"
  final String expected = String.format( "Multi-line comment line 1%n-- line 2'%n/* line 3 */" );
  assertEquals( "Multi-line string inserted incorrectly", expected, multiLineText );
  String empty = (String) s.createSQLQuery( "SELECT text FROM test_data WHERE id = 3" ).uniqueResult();
  assertNull( "NULL value inserted incorrectly", empty );
  tx.commit();
  s.close();
}

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

@SkipForDialect(value = AbstractHANADialect.class, comment = "On HANA, this returns an clob for the text column which doesn't get mapped to a String")
@Test
public void testTextTypeInSQLQuery() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  String description = buildLongString( 15000, 'a' );
  TextHolder holder = new TextHolder( description );
  s.persist( holder );
  t.commit();
  s.close();
  s = openSession();
  t = s.beginTransaction();
  String descriptionRead = ( String ) s.createSQLQuery( getDescriptionsSQL() )
      .uniqueResult();
  assertEquals( description, descriptionRead );
  s.delete( holder );
  t.commit();
  s.close();
}

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

@Test
public void testFlushSQL() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    entityManager.createNativeQuery("delete from Person").executeUpdate();
  });
  doInJPA( this::entityManagerFactory, entityManager -> {
    log.info("testFlushSQL");
    //tag::flushing-manual-flush-example[]
    Person person = new Person("John Doe");
    entityManager.persist(person);
    Session session = entityManager.unwrap( Session.class);
    session.setHibernateFlushMode( FlushMode.MANUAL );
    assertTrue(((Number) entityManager
      .createQuery("select count(id) from Person")
      .getSingleResult()).intValue() == 0);
    assertTrue(((Number) session
      .createNativeQuery("select count(*) from Person")
      .uniqueResult()).intValue() == 0);
    //end::flushing-manual-flush-example[]
  });
}

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

@Test
public void testFlushAutoSQLSynchronization() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    entityManager.createNativeQuery( "delete from Person" ).executeUpdate();;
  } );
  doInJPA( this::entityManagerFactory, entityManager -> {
    log.info( "testFlushAutoSQLSynchronization" );
    //tag::flushing-auto-flush-sql-synchronization-example[]
    assertTrue(((Number) entityManager
        .createNativeQuery( "select count(*) from Person")
        .getSingleResult()).intValue() == 0 );
    Person person = new Person( "John Doe" );
    entityManager.persist( person );
    Session session = entityManager.unwrap( Session.class );
    assertTrue(((Number) session
        .createNativeQuery( "select count(*) from Person")
        .addSynchronizedEntityClass( Person.class )
        .uniqueResult()).intValue() == 1 );
    //end::flushing-auto-flush-sql-synchronization-example[]
  } );
}

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

@SkipForDialect(value = AbstractHANADialect.class, comment = "On HANA, this returns a blob for the image column which doesn't get mapped to a byte[]")
@Test
public void testImageTypeInSQLQuery() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  byte[] photo = buildLongByteArray( 15000, true );
  ImageHolder holder = new ImageHolder( photo );
  s.persist( holder );
  t.commit();
  s.close();
  s = openSession();
  t = s.beginTransaction();
  byte[] photoRead = ( byte[] ) s.createSQLQuery( getPhotosSQL() )
      .uniqueResult();
  assertTrue( Arrays.equals( photo, photoRead ) );
  s.delete( holder );
  t.commit();
  s.close();
}
@Test

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

@Test
public void testFlushSQL() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    entityManager.createNativeQuery("delete from Person").executeUpdate();
  });
  doInJPA( this::entityManagerFactory, entityManager -> {
    log.info("testFlushSQL");
    //tag::flushing-always-flush-sql-example[]
    Person person = new Person("John Doe");
    entityManager.persist(person);
    Session session = entityManager.unwrap( Session.class);
    assertTrue(((Number) session
        .createNativeQuery("select count(*) from Person")
        .setFlushMode( FlushMode.ALWAYS)
        .uniqueResult()).intValue() == 1);
    //end::flushing-always-flush-sql-example[]
  });
}

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

Double sizeViaSql = ( (Number)s.createSQLQuery("select size_mb from image").uniqueResult() ).doubleValue();
assertEquals(SIZE_IN_MB, sizeViaSql, 0.01d);
t.commit();
s.flush();
sizeViaSql = ( (Number)s.createSQLQuery("select size_mb from image").uniqueResult() ).doubleValue();
assertEquals(NEW_SIZE_IN_MB, sizeViaSql, 0.01d);

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

@Test
public void testFlushAutoSQLNativeSession() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    entityManager.createNativeQuery( "delete from Person" ).executeUpdate();;
  } );
  doInJPA( this::entityManagerFactory, entityManager -> {
    log.info( "testFlushAutoSQLNativeSession" );
    //tag::flushing-auto-flush-sql-native-example[]
    assertTrue(((Number) entityManager
        .createNativeQuery( "select count(*) from Person")
        .getSingleResult()).intValue() == 0 );
    Person person = new Person( "John Doe" );
    entityManager.persist( person );
    Session session = entityManager.unwrap(Session.class);
    // For this to work, the Session/EntityManager must be put into COMMIT FlushMode
    //  - this is a change since 5.2 to account for merging EntityManager functionality
    // 		directly into Session.  Flushing would be the JPA-spec compliant behavior,
    //		so we know do that by default.
    session.setFlushMode( FlushModeType.COMMIT );
    //		or using Hibernate's FlushMode enum
    //session.setHibernateFlushMode( FlushMode.COMMIT );
    assertTrue(((Number) session
        .createNativeQuery( "select count(*) from Person")
        .uniqueResult()).intValue() == 0 );
    //end::flushing-auto-flush-sql-native-example[]
  } );
}

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

.uniqueResult());
assertEquals( 0, sqlValue.intValue() );
s.flush();
sqlValue = ( (Number) s.createSQLQuery("select child_position from ParentChild c where c.name='Child One'")
    .uniqueResult() );
assertEquals( 1, sqlValue.intValue() );
s.delete( p );

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

( (Number)s.createSQLQuery("select size_in_cm from t_staff where t_staff.id=1").uniqueResult() )
        .doubleValue();
assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d);
    ( (Number)s.createSQLQuery("select radiusS from t_staff where t_staff.id=1").uniqueResult() )
        .doubleValue();
assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d);
    ( (Number)s.createSQLQuery("select diamet from t_staff where t_staff.id=1").uniqueResult() )
        .doubleValue();
assertEquals(HEIGHT_CENTIMETERS*2, heightViaSql, 0.01d);
s.flush();
heightViaSql =
    ( (Number)s.createSQLQuery("select size_in_cm from t_staff where t_staff.id=1").uniqueResult() )
        .doubleValue();
assertEquals(2.54d, heightViaSql, 0.01d);

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

( (Number)s.createSQLQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult())
        .doubleValue();
assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d);
s.flush();
heightViaSql =
    ( (Number)s.createSQLQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult() )
        .doubleValue();
assertEquals(2.54d, heightViaSql, 0.01d);

代码示例来源: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

( (Number)s.createSQLQuery("select height_centimeters from humans").uniqueResult() ).doubleValue();
assertEquals(HUMAN_CENTIMETERS, humanHeightViaSql, 0.01d);
Double alienHeightViaSql =
    ( (Number)s.createSQLQuery("select height_centimeters from aliens").uniqueResult() ).doubleValue();
assertEquals(ALIEN_CENTIMETERS, alienHeightViaSql, 0.01d);
s.clear();

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

Number heightViaSql = (Number)s.createSQLQuery("select height_centimeters from person where name='Emmanuel'").uniqueResult();
assertEquals(HEIGHT_CENTIMETERS, heightViaSql.doubleValue(), 0.01d);
Number expiryViaSql = (Number)s.createSQLQuery("select pwd_expiry_weeks from t_user where person_id=?")
u.setPasswordExpiryDays(7d);
s.flush();
heightViaSql = (Number)s.createSQLQuery("select height_centimeters from person where name='Emmanuel'").uniqueResult();
assertEquals(2.54d, heightViaSql.doubleValue(), 0.01d);
expiryViaSql = (Number)s.createSQLQuery("select pwd_expiry_weeks from t_user where person_id=?")

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

( (Number)s.createSQLQuery("select height_centimeters from JPerson where name='Emmanuel'").uniqueResult() )
        .doubleValue();
assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d);
s.flush();
heightViaSql =
    ( (Number)s.createSQLQuery("select height_centimeters from JPerson where name='Emmanuel'").uniqueResult() )
        .doubleValue();
assertEquals(2.54d, heightViaSql, 0.01d);

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

( (Number)s.createSQLQuery("select height_centimeters from UPerson where name='Emmanuel'").uniqueResult() ).doubleValue();
assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d);
Double expiryViaSql =
s.flush();
heightViaSql =
    ( (Number)s.createSQLQuery("select height_centimeters from UPerson where name='Emmanuel'").uniqueResult() )
        .doubleValue();
assertEquals(2.54d, heightViaSql, 0.01d);

代码示例来源:origin: com.centit.framework/centit-persistence-hibernate

public final static Object getSingleObjectBySql(BaseDaoImpl<?, ?> baseDao,
    final String sSql, final Object paramObject) {
  NativeQuery queryObject = baseDao.getCurrentSession().createNativeQuery(sSql);
  setQueryParameter(queryObject,new Object[] {paramObject});
  return queryObject.uniqueResult();
}

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

@Test
@TestForIssue( jiraKey = "OGM-1375" )
public void testUniqueResultWithAddEntityWithProjection() {
  thrown.expect( PersistenceException.class );
  thrown.expectMessage( PROJECTION_ADD_ENTITY_MESSAGE );
  inTransaction( session -> {
    session.createNativeQuery( getNativeQueryWithProjectionIdName() )
      .addEntity( Movie.class )
      .uniqueResult();
  } );
}

相关文章