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

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

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

Query.getSingleResult介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public Person findByName(String name) {
  return (Person) this.sessionFactory.getCurrentSession().createQuery(
    "from Person person where person.name = :name").setParameter("name", name).getSingleResult();
}

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

@Before
public void prepare() {
  final Session s = openSession();
  final Transaction t = s.beginTransaction();
  try {
    final Number count = (Number) s.createQuery("SELECT count(ID) FROM SimpleEntity").getSingleResult();
    if (count.longValue() > 0L) {
      // entity already added previously
      return;
    }
    final Map<String, Object> entity = new HashMap<>();
    entity.put( "id", 1L );
    entity.put( "name", "TheParent" );
    final Map<String, Object> c1 = new HashMap<>();
    c1.put( "id", 1L );
    c1.put( "parent", entity );
    s.save( "SimpleEntity", entity );
    s.save( "ChildEntity", c1 );
  }
  finally {
    t.commit();
    s.close();
  }
}

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

@Test(expected = PersistenceException.class)
@TestForIssue(jiraKey = "HHH-12132")
public void testLegacyBooleanType() throws Exception {
  rebuildSessionFactory( configuration -> {
    configuration.setProperty( "hibernate.dialect.hana.use_legacy_boolean_type", Boolean.FALSE.toString() );
  } );
  Session s = openSession();
  s.beginTransaction();
  LegacyBooleanEntity legacyEntity = new LegacyBooleanEntity();
  legacyEntity.key = Integer.valueOf( 2 );
  legacyEntity.bool = Boolean.FALSE;
  s.persist( legacyEntity );
  s.flush();
  s.getTransaction().commit();
  s.clear();
  Query<LegacyBooleanEntity> query = s.createQuery( "select b from " + LEGACY_ENTITY_NAME + " b where bool = true", LegacyBooleanEntity.class );
  query.getSingleResult();
}

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

@Test(expected = PersistenceException.class)
@TestForIssue(jiraKey = "HHH-12132")
public void testBooleanTypeLegacyBehavior() throws Exception {
  rebuildSessionFactory( configuration -> {
    configuration.setProperty( "hibernate.dialect.hana.use_legacy_boolean_type", Boolean.TRUE.toString() );
  } );
  Session s = openSession();
  s.beginTransaction();
  BooleanEntity entity = new BooleanEntity();
  entity.key = Integer.valueOf( 2 );
  entity.bool = Boolean.FALSE;
  s.persist( entity );
  s.flush();
  s.getTransaction().commit();
  s.clear();
  Query<BooleanEntity> query = s.createQuery( "select b from " + ENTITY_NAME + " b where bool = true", BooleanEntity.class );
  query.getSingleResult();
}

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

@Test
@TestForIssue(jiraKey = "HHH-12132")
public void testLegacyBooleanTypeLegacyBehavior() throws Exception {
  rebuildSessionFactory( configuration -> {
    configuration.setProperty( "hibernate.dialect.hana.use_legacy_boolean_type", Boolean.TRUE.toString() );
  } );
  Session s = openSession();
  s.beginTransaction();
  LegacyBooleanEntity legacyEntity = new LegacyBooleanEntity();
  legacyEntity.key = Integer.valueOf( 1 );
  legacyEntity.bool = Boolean.TRUE;
  s.persist( legacyEntity );
  s.flush();
  s.getTransaction().commit();
  s.clear();
  Query<LegacyBooleanEntity> legacyQuery = s.createQuery( "select b from " + LEGACY_ENTITY_NAME + " b where bool = true", LegacyBooleanEntity.class );
  LegacyBooleanEntity retrievedEntity = legacyQuery.getSingleResult();
  assertEquals( Integer.valueOf( 1 ), retrievedEntity.key );
  assertTrue( retrievedEntity.bool );
}

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

@Test
@TestForIssue(jiraKey = "HHH-12132")
public void testBooleanTypeDefaultBehavior() throws Exception {
  rebuildSessionFactory();
  Session s = openSession();
  s.beginTransaction();
  BooleanEntity entity = new BooleanEntity();
  entity.key = Integer.valueOf( 1 );
  entity.bool = Boolean.TRUE;
  s.persist( entity );
  s.flush();
  s.getTransaction().commit();
  s.clear();
  Query<BooleanEntity> legacyQuery = s.createQuery( "select b from " + ENTITY_NAME + " b where bool = true", BooleanEntity.class );
  BooleanEntity retrievedEntity = legacyQuery.getSingleResult();
  assertEquals( Integer.valueOf( 1 ), retrievedEntity.key );
  assertTrue( retrievedEntity.bool );
}

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

@Test
@TestForIssue(jiraKey = "HHH-12132")
public void testBooleanType() throws Exception {
  rebuildSessionFactory( configuration -> {
    configuration.setProperty( "hibernate.dialect.hana.use_legacy_boolean_type", Boolean.FALSE.toString() );
  } );
  Session s = openSession();
  s.beginTransaction();
  BooleanEntity entity = new BooleanEntity();
  entity.key = Integer.valueOf( 1 );
  entity.bool = Boolean.TRUE;
  s.persist( entity );
  s.flush();
  s.getTransaction().commit();
  s.clear();
  Query<BooleanEntity> legacyQuery = s.createQuery( "select b from " + ENTITY_NAME + " b where bool = true", BooleanEntity.class );
  BooleanEntity retrievedEntity = legacyQuery.getSingleResult();
  assertEquals( Integer.valueOf( 1 ), retrievedEntity.key );
  assertTrue( retrievedEntity.bool );
}

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

final Number count = (Number) s.createQuery("SELECT count(ID) FROM SimpleEntity").getSingleResult();
if (count.longValue() > 0L) {

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

@Test
public void testPolymorphicAssociation() {
  doInHibernate( this::sessionFactory, session1 -> {
    Family family = session1.createQuery( "FROM Family f", Family.class ).getSingleResult();
    List<Person> members = family.getMembers();
    assertThat( members.size(), is( familyMembers.size() ) );
    for ( Person person : members ) {
      if ( person instanceof Man ) {
        assertThat( ( (Man) person ).getHobby(), is( john.getHobby() ) );
      }
      else if ( person instanceof Woman ) {
        assertThat( ( (Woman) person ).getJob(), is( jane.getJob() ) );
      }
      else if ( person instanceof Child ) {
        if ( person.getName().equals( "Susan" ) ) {
          assertThat( ( (Child) person ).getFavouriteToy(), is( susan.getFavouriteToy() ) );
        }
        else {
          assertThat( ( (Child) person ).getFavouriteToy(), is( mark.getFavouriteToy() ) );
        }
      }
      else {
        fail( "Unexpected result: " + person );
      }
    }
  } );
}

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

"select pa from PersonAddress pa where pa.person = :person", PersonAddress.class )
.setParameter( "person", person )
.getSingleResult();

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

@Test
public void test() {
  final DateEvent _dateEvent = doInJPA( this::entityManagerFactory, entityManager -> {
    DateEvent dateEvent = new DateEvent();
    dateEvent.setCreatedOn( Instant.from( DateTimeFormatter.ISO_INSTANT.parse( "2016-10-13T06:40:18.745Z" ) ) );
    entityManager.persist( dateEvent );
    return dateEvent;
  } );
  doInJPA( this::entityManagerFactory, entityManager -> {
    DateEvent dateEvent = entityManager.unwrap( Session.class )
    .createQuery(
      "select de " +
      "from DateEvent de " +
      "where de.createdOn = '2016-10-13T06:40:18.745Z' ", DateEvent.class )
    .getSingleResult();
    assertNotNull(dateEvent);
    assertEquals(_dateEvent.getId(), dateEvent.getId());
  } );
  doInJPA( this::entityManagerFactory, entityManager -> {
    DateEvent dateEvent = entityManager.unwrap( Session.class )
    .createQuery(
      "select de " +
      "from DateEvent de " +
      "where de.createdOn = :createdOn ", DateEvent.class )
    .setParameter( "createdOn", Instant.from( DateTimeFormatter.ISO_INSTANT.parse( "2016-10-13T06:40:18.745Z" ) ) )
    .getSingleResult();
    assertNotNull(dateEvent);
    assertEquals(_dateEvent.getId(), dateEvent.getId());
  } );
}

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

@Test
public void testJPQLUpperAttributeValueBindParameterType() throws Exception {
  doInJPA( this::entityManagerFactory, entityManager -> {
    //tag::basic-attribute-converter-query-parameter-converter-object-example[]
    SessionFactory sessionFactory = entityManager.getEntityManagerFactory()
        .unwrap( SessionFactory.class );
    MetamodelImplementor metamodelImplementor = (MetamodelImplementor) sessionFactory.getMetamodel();
    Type captionType = metamodelImplementor
        .entityPersister( Photo.class.getName() )
        .getPropertyType( "caption" );
    Photo photo = (Photo) entityManager.createQuery(
      "select p " +
      "from Photo p " +
      "where upper(caption) = upper(:caption) ", Photo.class )
    .unwrap( Query.class )
    .setParameter( "caption", new Caption("Nicolae Grigorescu"), captionType)
    .getSingleResult();
    //end::basic-attribute-converter-query-parameter-converter-object-example[]
    assertEquals( "Dorobantul", photo.getName() );
  } );
}

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

"JOIN parent.string s " +
        "JOIN FETCH s.map", MultilingualString.class )
    .getSingleResult();
assertEquals( 2, m.getMap().size() );
localizedString = m.getMap().get( "English" );

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

@Test
public void testFetchEager() {
  doInHibernate( this::sessionFactory, s -> {
    PropertySet set = new PropertySet( "string" );
    Property property = new StringProperty( "name", "Alex" );
    set.setSomeProperty( property );
    s.save( set );
  } );
  PropertySet result = doInHibernate( this::sessionFactory, s -> {
    return s.createQuery( "select s from PropertySet s where name = :name", PropertySet.class )
        .setParameter( "name", "string" )
        .getSingleResult();
  } );
  assertNotNull( result );
  assertNotNull( result.getSomeProperty() );
  assertTrue( result.getSomeProperty() instanceof StringProperty );
  assertEquals( "Alex", result.getSomeProperty().asString() );
}

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

checkResult( session.createQuery( "from Person p", Person.class ).getSingleResult() );
});

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

@Test
@TestForIssue(jiraKey = "HHH-12978")
public void testEnumAsBindParameterAndExtract() {
  doInHibernate( this::sessionFactory, s -> {
    binderTriggerable.reset();
    extractorTriggerable.reset();
    s.createQuery( "select p.id from Person p where p.id = :id", Long.class )
        .setParameter( "id", person.getId() )
        .getSingleResult();
    assertTrue( binderTriggerable.wasTriggered() );
    assertTrue( extractorTriggerable.wasTriggered() );
  } );
  doInHibernate( this::sessionFactory, s -> {
    binderTriggerable.reset();
    extractorTriggerable.reset();
    s.createQuery(
        "select p.gender from Person p where p.gender = :gender and p.hairColor = :hairColor",
        Gender.class
    )
        .setParameter( "gender", Gender.MALE )
        .setParameter( "hairColor", HairColor.BROWN )
        .getSingleResult();
    assertTrue( binderTriggerable.wasTriggered() );
    assertTrue( extractorTriggerable.wasTriggered() );
  } );
}

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

.getSingleResult();
.getSingleResult();

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

simple = session.createQuery( "FROM Simple", Simple.class ).getSingleResult();
session.getTransaction().commit();

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

@Test
public void testFetchLazy() {
  doInHibernate( this::sessionFactory, s -> {
    LazyPropertySet set = new LazyPropertySet( "string" );
    Property property = new StringProperty( "name", "Alex" );
    set.setSomeProperty( property );
    s.save( set );
  } );
  LazyPropertySet result = doInHibernate( this::sessionFactory, s -> {
    return s.createQuery( "select s from LazyPropertySet s where name = :name", LazyPropertySet.class )
        .setParameter( "name", "string" )
        .getSingleResult();
  } );
  assertNotNull( result );
  assertNotNull( result.getSomeProperty() );
  try {
    result.getSomeProperty().asString();
    fail( "should not get the property string after session closed." );
  }
  catch (LazyInitializationException e) {
    // expected
  }
  catch (Exception e) {
    fail( "should not throw exception other than LazyInitializationException." );
  }
}

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

@Test
public void testFetchLazy() {
  doInHibernate( this::sessionFactory, s -> {
    LazyPropertySet set = new LazyPropertySet( "string" );
    Property property = new StringProperty( "name", "Alex" );
    set.setSomeProperty( property );
    s.save( set );
  } );
  LazyPropertySet result = doInHibernate( this::sessionFactory, s -> {
    return s.createQuery( "select s from LazyPropertySet s where name = :name", LazyPropertySet.class )
        .setParameter( "name", "string" )
        .getSingleResult();
  } );
  assertNotNull( result );
  assertNotNull( result.getSomeProperty() );
  try {
    result.getSomeProperty().asString();
    fail( "should not get the property string after session closed." );
  }
  catch (LazyInitializationException e) {
    // expected
  }
  catch (Exception e) {
    fail( "should not throw exception other than LazyInitializationException." );
  }
}

相关文章