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

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

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

NativeQuery.addEntity介绍

[英]Declare a "root" entity, without specifying an alias. The expectation here is that the table alias is the same as the unqualified entity name
[中]声明一个“根”实体,但不指定别名。这里的期望是表别名与非限定实体名称相同

代码示例

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

/**
 * <p>
 * Create an {@link NativeQuery} from the session provided (preferring
 * stateless if both are available).
 * </p>
 */
@Override
@SuppressWarnings("unchecked")
public NativeQuery<E> createQuery() {
  if (isStatelessSession()) {
    return getStatelessSession().createNativeQuery(sqlQuery).addEntity(entityClass);
  }
  else {
    return getStatefulSession().createNativeQuery(sqlQuery).addEntity(entityClass);
  }
}

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

@Test
public void testTS() throws Exception {
  Session session = openSession();
  Transaction txn = session.beginTransaction();
  Simple sim = new Simple( Long.valueOf(1) );
  sim.setDate( new Date() );
  session.save( sim );
  Query q = session.createSQLQuery( "select {sim.*} from SimpleEntity {sim} where {sim}.date_ = ?" ).addEntity( "sim", Simple.class );
  q.setTimestamp( 0, sim.getDate() );
  assertTrue ( q.list().size()==1 );
  session.delete(sim);
  txn.commit();
  session.close();
}

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

@Test
@SuppressWarnings("unchecked")
public void shouldCreateQueryWithStatefulSession() {
  String sqlQuery = "select * from T_FOOS";
  hibernateQueryProvider.setSqlQuery(sqlQuery);
  Session session = mock(Session.class);
  NativeQuery<Foo> query = mock(NativeQuery.class);
  when(session.createNativeQuery(sqlQuery)).thenReturn(query);
  when(query.addEntity(Foo.class)).thenReturn(query);
  hibernateQueryProvider.setSession(session);
  assertNotNull(hibernateQueryProvider.createQuery());
}

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

@Test
@SuppressWarnings("unchecked")
public void testCreateQueryWithStatelessSession() {
  String sqlQuery = "select * from T_FOOS";
  hibernateQueryProvider.setSqlQuery(sqlQuery);
  StatelessSession session = mock(StatelessSession.class);
  NativeQuery<Foo> query = mock(NativeQuery.class);
  when(session.createNativeQuery(sqlQuery)).thenReturn(query);
  when(query.addEntity(Foo.class)).thenReturn(query);
  hibernateQueryProvider.setStatelessSession(session);
  assertNotNull(hibernateQueryProvider.createQuery());
}

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

@Test
@FailureExpected(jiraKey = "HHH-2225")
public void testNativeQueryWithFormulaAttributeWithoutAlias() {
  String sql = "select TABLE_NAME , sysdate() from all_tables  where TABLE_NAME = 'AUDIT_ACTIONS' ";
  Session s = openSession();
  s.beginTransaction();
  s.createSQLQuery( sql ).addEntity( "t", AllTables.class ).list();
  s.getTransaction().commit();
  s.close();
}

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

@Test
public void test_sql_hibernate_multi_entity_query_alias_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-multi-entity-query-alias-example[]
    List<Object> entities = session.createNativeQuery(
      "SELECT {pr.*}, {pt.*} " +
      "FROM Person pr, Partner pt " +
      "WHERE pr.name = pt.name" )
    .addEntity( "pr", Person.class)
    .addEntity( "pt", Partner.class)
    .list();
    //end::sql-hibernate-multi-entity-query-alias-example[]
    assertEquals(1, entities.size());
  });
}

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

@Test
@SkipForDialect( MySQLDialect.class )
public void testFindSimpleBySQL() throws Exception {
  Session session = openSession();
  session.beginTransaction();
  Category s = new Category();
  s.setName(String.valueOf(nextLong++));
  session.save(s);
  session.flush();
  Query query = session.createSQLQuery( "select s.category_key_col as {category.id}, s.name as {category.name}, s.\"assign-able-id\" as {category.assignable} from {category} s" )
      .addEntity( "category", Category.class );
  List list = query.list();
  assertNotNull( list );
  assertTrue( list.size() > 0 );
  assertTrue(list.get(0) instanceof Category);
  session.getTransaction().commit();
  session.close();
  // How do we handle objects with composite id's ? (such as Single)
}

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

@Test
public void testFindBySQLDiscriminatedDiffSession() throws Exception {
  Session session = openSession();
  session.beginTransaction();
  for ( Object entity : session.createQuery( "from A" ).list() ) {
    session.delete( entity );
  }
  A savedA = new A();
  session.save(savedA);
  B savedB = new B();
  session.save(savedB);
  session.getTransaction().commit();
  int count = session.createQuery("from A").list().size();
  session.close();
  session = openSession();
  session.beginTransaction();
  Query query = session.createSQLQuery( "select identifier_column as {a.id}, clazz_discriminata as {a.class}, count_ as {a.count}, name as {a.name} from TA" )
      .addEntity( "a", A.class );
  List list = query.list();
  assertNotNull(list);
  assertEquals(count, list.size());
  session.getTransaction().commit();
  session.close();
}

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

@Test
public void testFindBySQLSimpleByDiffSessions() throws Exception {
  Session session = openSession();
  session.beginTransaction();
  Category s = new Category();
  s.setName(String.valueOf(nextLong++));
  session.save(s);
  session.getTransaction().commit();
  session.close();
  if ( getDialect() instanceof MySQLDialect ) {
    return;
  }
  session = openSession();
  session.beginTransaction();
  Query query = session.createSQLQuery( "select s.category_key_col as {category.id}, s.name as {category.name}, s.\"assign-able-id\" as {category.assignable} from {category} s" )
      .addEntity( "category", Category.class );
  List list = query.list();
  assertNotNull( list );
  assertTrue( list.size() > 0 );
  assertTrue( list.get( 0 ) instanceof Category );
  // How do we handle objects that does not have id property (such as Simple ?)
  // How do we handle objects with composite id's ? (such as Single)
  session.getTransaction().commit();
  session.close();
}

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

@Test
public void test_sql_hibernate_entity_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-entity-query-example[]
    List<Person> persons = session.createNativeQuery(
      "SELECT * FROM Person" )
    .addEntity( Person.class )
    .list();
    //end::sql-hibernate-entity-query-example[]
    assertEquals(3, persons.size());
  });
}

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

@Test
public void test_sql_hibernate_entity_query_explicit_result_set_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-entity-query-explicit-result-set-example[]
    List<Person> persons = session.createNativeQuery(
      "SELECT id, name, nickName, address, createdOn, version " +
      "FROM Person" )
    .addEntity( Person.class )
    .list();
    //end::sql-hibernate-entity-query-explicit-result-set-example[]
    assertEquals(3, persons.size());
  });
}

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

@Test
public void test_sql_hibernate_entity_associations_query_many_to_one_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-entity-associations-query-many-to-one-example[]
    List<Phone> phones = session.createNativeQuery(
      "SELECT id, phone_number, phone_type, person_id " +
      "FROM Phone" )
    .addEntity( Phone.class )
    .list();
    //end::sql-hibernate-entity-associations-query-many-to-one-example[]
    assertEquals(3, phones.size());
  });
}

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

@Test
public void test_sql_hibernate_inheritance_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-inheritance-query-example[]
    List<CreditCardPayment> payments = session.createNativeQuery(
      "SELECT * " +
      "FROM Payment p " +
      "JOIN CreditCardPayment cp on cp.id = p.id" )
    .addEntity( CreditCardPayment.class )
    .list();
    //end::sql-hibernate-inheritance-query-example[]
    assertEquals(1, payments.size());
  });
}

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

@Test
public void test_sql_hibernate_query_parameters_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-query-parameters-example[]
    List<Person> persons = session.createNativeQuery(
      "SELECT * " +
      "FROM Person " +
      "WHERE name like :name" )
    .addEntity( Person.class )
    .setParameter("name", "J%")
    .list();
    //end::sql-hibernate-query-parameters-example[]
    assertEquals(1, persons.size());
  });
}

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

private void componentTest(String sql) throws SQLException {
  Componentizable c = setupComponentData();
  Session session = openSession();
  session.beginTransaction();
  SQLQuery q = session.createSQLQuery( sql ).addEntity( "comp", Componentizable.class );
  List list = q.list();
  assertEquals( list.size(), 1 );
  Componentizable co = (Componentizable) list.get(0);
  assertEquals( c.getNickName(), co.getNickName() );
  assertEquals( c.getComponent().getName(), co.getComponent().getName() );
  assertEquals( c.getComponent().getSubComponent().getSubName(), co.getComponent().getSubComponent().getSubName() );
  session.delete( co );
  session.getTransaction().commit();
  session.close();
}

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

@Test
public void test_sql_hibernate_entity_associations_query_one_to_many_join_example_1() {
  try {
    doInJPA( this::entityManagerFactory, entityManager -> {
      Session session = entityManager.unwrap( Session.class );
      List<Phone> phones = session.createNativeQuery(
        "SELECT * " +
        "FROM Phone ph " +
        "JOIN phone_call c ON c.phone_id = ph.id" )
      .addEntity("phone", Phone.class )
      .addJoin( "c", "phone.calls")
      .setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY )
      .list();
      for(Phone phone : phones) {
        List<Call> calls = phone.getCalls();
      }
      assertEquals(2, phones.size());
    });
  }
  catch (Exception e) {
    log.error( "HHH-10504", e );
    //See issue https://hibernate.atlassian.net/browse/HHH-10504
  }
}

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

@Test
public void testNativeQueryWithFormulaAttribute() {
  SQLFunction dateFunction = getDialect().getFunctions().get( "current_date" );
  String dateFunctionRendered = dateFunction.render(
      null,
      java.util.Collections.EMPTY_LIST,
      sessionFactory()
  );
  String sql = String.format(
      "select t.TABLE_NAME as {t.tableName}, %s as {t.daysOld} from ALL_TABLES t  where t.TABLE_NAME = 'AUDIT_ACTIONS' ",
      dateFunctionRendered
  );
  String sql2 = String.format(
      "select TABLE_NAME as t_name, %s as t_time from ALL_TABLES   where TABLE_NAME = 'AUDIT_ACTIONS' ",
      dateFunctionRendered
  );
  Session s = openSession();
  s.beginTransaction();
  s.createSQLQuery( sql ).addEntity( "t", AllTables.class ).list();
  s.createSQLQuery( sql2 ).setResultSetMapping( "all" ).list();
  SQLQuery q = s.createSQLQuery( sql2 );
  q.addRoot( "t", AllTables.class ).addProperty( "tableName", "t_name" ).addProperty( "daysOld", "t_time" );
  q.list();
  s.getTransaction().commit();
  s.close();
}

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

@Test
public void test_sql_hibernate_entity_associations_query_many_to_one_join_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-entity-associations-query-many-to-one-join-example[]
    List<Object[]> tuples = session.createNativeQuery(
      "SELECT * " +
      "FROM Phone ph " +
      "JOIN Person pr ON ph.person_id = pr.id" )
    .addEntity("phone", Phone.class )
    .addJoin( "pr", "phone.person")
    .list();
    for(Object[] tuple : tuples) {
      Phone phone = (Phone) tuple[0];
      Person person = (Person) tuple[1];
      assertNotNull( person.getName() );
    }
    //end::sql-hibernate-entity-associations-query-many-to-one-join-example[]
    assertEquals(3, tuples.size());
  });
}

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

@Test
public void test_sql_hibernate_entity_associations_query_many_to_one_join_result_transformer_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-entity-associations-query-many-to-one-join-result-transformer-example[]
    List<Person> persons = session.createNativeQuery(
      "SELECT * " +
      "FROM Phone ph " +
      "JOIN Person pr ON ph.person_id = pr.id" )
    .addEntity("phone", Phone.class )
    .addJoin( "pr", "phone.person")
    .setResultTransformer( Criteria.ROOT_ENTITY )
    .list();
    for(Person person : persons) {
      person.getPhones();
    }
    //end::sql-hibernate-entity-associations-query-many-to-one-join-result-transformer-example[]
    assertEquals(3, persons.size());
  });
}

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

@Test
@RequiresDialect(H2Dialect.class)
@RequiresDialect(Oracle8iDialect.class)
@RequiresDialect(PostgreSQL82Dialect.class)
public void test_sql_hibernate_entity_associations_query_one_to_many_join_example_2() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-entity-associations-query-one-to-many-join-example[]
    List<Object[]> tuples = session.createNativeQuery(
      "SELECT * " +
      "FROM Phone ph " +
      "JOIN phone_call c ON c.phone_id = ph.id" )
    .addEntity("phone", Phone.class )
    .addJoin( "c", "phone.calls")
    .list();
    for(Object[] tuple : tuples) {
      Phone phone = (Phone) tuple[0];
      Call call = (Call) tuple[1];
    }
    //end::sql-hibernate-entity-associations-query-one-to-many-join-example[]
    assertEquals(2, tuples.size());
  });
}

相关文章