org.hibernate.query.Query类的使用及代码示例

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

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

Query介绍

[英]Represents an HQL/JPQL query or a compiled Criteria query. Also acts as the Hibernate extension to the JPA Query/TypedQuery contract

NOTE: org.hibernate.Query is deprecated, and slated for removal in 6.0. For the time being we leave all methods defined on org.hibernate.Queryrather than here because it was previously the public API so we want to leave that unchanged in 5.x. For 6.0 we will move those methods here and then delete that class.
[中]表示HQL/JPQL查询或编译条件查询。还充当JPA查询/TypedQuery合同的Hibernate扩展
注:org。冬眠查询已弃用,并计划在6.0中删除。目前,我们将所有方法都留在org上定义。冬眠Queryrather比这里更重要,因为它以前是公共API,所以我们希望在5年内保持不变。x、 对于6.0,我们将把这些方法移到这里,然后删除该类。

代码示例

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

public Long createHolidayCalendar() {
  Session session = sessionFactory.getCurrentSession();
  session.beginTransaction();
  // delete all existing calendars
  List calendars = session.createQuery("from HolidayCalendar").setCacheable(true).list();
  for (ListIterator li = calendars.listIterator(); li.hasNext(); ) {
    session.delete(li.next());
  }
  HolidayCalendar calendar = new HolidayCalendar();
  calendar.init();
  Long calendarId = (Long)session.save(calendar);
  session.getTransaction().commit();
  return calendarId;
}

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

@Test
public void testJPAPositionalParameterList() {
  Session s = openSession();
  s.beginTransaction();
  ArrayList<String> params = new ArrayList<String>();
  params.add( "Doe" );
  params.add( "Public" );
  s.createQuery( "from Human where name.last in (?1)" )
      .setParameterList( 1, params )
      .list();
  s.createQuery( "from Human where name.last in ?1" )
      .setParameterList( 1, params )
      .list();
      .setParameter( 1, "Yogster" )
      .setParameter( 2, "Yogi"  )
      .setParameterList( 3, params )
      .list();
      .setParameter( 1, "Yogster" )
      .setParameter( 2, "Yogi" )
      .setParameterList( 3, params )
      .list();
      .setParameter( 1, "Yogster" )
      .setParameter( 2, "Yogi"  )
      .setParameterList( 3, params )
      .list();

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

@Test
public void testPessimisticLockWithFirstResultsThenFollowOnLocking() {
  final Session session = openSession();
  session.beginTransaction();
  sqlStatementInterceptor.getSqlQueries().clear();
  List<Product> products =
      session.createQuery(
          "select p from Product p", Product.class )
          .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
          .setFirstResult( 40 )
          .setMaxResults( 10 )
          .getResultList();
  assertEquals( 10, products.size() );
  assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
  session.getTransaction().commit();
  session.close();
}

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

@SuppressWarnings("unchecked")
private List<BatchJob> nextFiveBatchJobs(Session session, Integer maxResult) {
  Query query = session.createQuery(
      "select j from BatchJob j", BatchJob.class )
      .setMaxResults( maxResult )
      .unwrap( Query.class );
  
  applySkipLocked(query);
  return query.list();
}

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

public List listEvents() {
  Session session = sessionFactory.getCurrentSession();
  session.beginTransaction();
  List result = session.createQuery("from Event").setCacheable(true).list();
  session.getTransaction().commit();
  return result;
}

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

private void cleanupData() {
  Session session = openSession();
  session.beginTransaction();
  session.createQuery( "delete EmployeeInfo" ).executeUpdate();
  session.createQuery( "delete Employee" ).executeUpdate();
  session.getTransaction().commit();
  session.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
public void testNestedCollectionFetch() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  s.createQuery("from Animal a left join fetch a.offspring o left join fetch o.offspring where a.mother.id = 1 order by a.description").list();
  s.createQuery("from Zoo z left join fetch z.animals a left join fetch a.offspring where z.name ='MZ' order by a.description").list();
  s.createQuery("from Human h left join fetch h.pets a left join fetch a.offspring where h.name.first ='Gavin' order by a.description").list();
  t.commit();
  s.close();
}

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

session.getTransaction().begin();
Address address = new Address();
Zoo zoo = new Zoo( "ZOO 1", address );
address.setCity( "City 1" );
session.save( zoo );
session.getTransaction().commit();
session.clear();
session.getTransaction().begin();
List result = session.createQuery( "FROM Zoo z WHERE z.name IN (?1) and z.address.city IN (?2)" )
    .setParameterList( 1, namesArray )
    .setParameterList( 2, citiesArray )
    .list();

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

@Test
public void testPaginationWithHQL() {
  doInHibernate( this::sessionFactory, session -> {
    for ( int i = 20; i < 30; i++ ) {
      session.persist( new Product2( i, "Kit" + i ) );
    }
    session.flush();
    session.clear();
    List list = session.createQuery( "from Product2 order by id" ).setFirstResult( 3 ).setMaxResults( 2 ).list();
    assertEquals( Arrays.asList( new Product2( 23, "Kit23" ), new Product2( 24, "Kit24" ) ), list );
  } );
}

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

@Test
public void testCriteriaWithoutFilteredEntity() throws Exception {
  indexTestData();
  // Search
  Session session = openSession();
  Transaction tx = session.beginTransaction();
  FullTextSession fullTextSession = Search.getFullTextSession( session );
  MultiFieldQueryParser parser = new MultiFieldQueryParser( new String[] { "kurztext" }, TestConstants.standardAnalyzer );
  Query query = parser.parse( "combi OR sport" );
  Criteria criteria = session.createCriteria( AbstractCar.class );
  criteria.add( Restrictions.eq( "hasColor", Boolean.FALSE ) );
  org.hibernate.query.Query hibQuery = fullTextSession.createFullTextQuery( query )
      .setCriteriaQuery( criteria );
  List result = hibQuery.list();
  assertEquals( 2, result.size() );
  tx.commit();
  session.close();
}

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

@Test
public void testSubselectFetchFromQueryList() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  EmployeeGroup group1 = new EmployeeGroup();
  Employee employee1 = new Employee("Jane");
  group2.addEmployee( employee4 );
  s.save( group1 );
  s.save( group2 );
  s.flush();
  List<EmployeeGroup> results = s.createQuery(
      "from SubselectFetchCollectionFromBatchTest$EmployeeGroup where id in :groups"
  ).setParameterList(
      "groups",
      new Long[] { group1.getId(), group2.getId() }
  ).list();
  t.rollback();
  s.close();

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

@Test
public void testJpqlBooleanLiteral() {
  Session session = openSession();
  session.getTransaction().begin();
  assertNotNull( session.createQuery( "from Employee e where e.active = true" ).uniqueResult() );
  assertNull( session.createQuery( "from Employee e where e.active = false" ).uniqueResult() );
  session.getTransaction().commit();
  session.close();
}

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

@Test
public void testMaxResultMoreThanTotalNumberOfHits() throws Exception {
  FullTextSession fullTextSession = Search.getFullTextSession( openSession() );
  Transaction tx = fullTextSession.beginTransaction();
  QueryParser parser = new QueryParser( "title", TestConstants.stopAnalyzer );
  Query query = parser.parse( "summary:Festina Or brand:Seiko" );
  org.hibernate.query.Query hibQuery = fullTextSession.createFullTextQuery( query, Clock.class, Book.class );
  hibQuery.setFirstResult( 0 );
  hibQuery.setMaxResults( 3 );
  List result = hibQuery.list();
  assertNotNull( result );
  assertEquals( "max result out of limit", 2, result.size() );
  tx.commit();
  fullTextSession.close();
}

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

@Test
@TestForIssue(jiraKey = "HSEARCH-296")
public void testClassProjection() throws Exception {
  FullTextSession s = Search.getFullTextSession( openSession() );
  prepEmployeeIndex( s );
  s.clear();
  Transaction tx = s.beginTransaction();
  QueryParser parser = new QueryParser( "dept", TestConstants.standardAnalyzer );
  Query query = parser.parse( "dept:ITech" );
  org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class );
  hibQuery.setProjection( FullTextQuery.OBJECT_CLASS );
  List<?> result = hibQuery.list();
  assertNotNull( result );
  Object[] projection = (Object[]) result.get( 0 );
  assertNotNull( projection );
  assertEquals( "Wrong projected class", Employee.class, projection[0] );
  //cleanup
  for ( Object element : s.createQuery( "from " + Employee.class.getName() ).list() ) {
    s.delete( element );
  }
  tx.commit();
  s.close();
}

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

@Test
@RequiresDialectFeature(
    value = DialectChecks.SupportLimitAndOffsetCheck.class,
    comment = "dialect does not support offset and limit combo"
)
public void testSimpleSelectWithLimitAndOffset() throws Exception {
  // just checking correctness of param binding code...
  Session session = openSession();
  Transaction t = session.beginTransaction();
  session.createQuery( "from Animal" )
      .setFirstResult( 2 )
      .setMaxResults( 1 )
      .list();
  t.commit();
  session.close();
}

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

@Test
@TestForIssue( jiraKey = "HHH-9356" )
public void testBetweenLiteral() {
  final Session s = openSession();
  s.getTransaction().begin();
  @SuppressWarnings("unchecked")
  final List<Item> result = s.createQuery( "from Item where quantity between 9 and 11" ).list();
  assertEquals( 1, result.size() );
  assertEquals( 10, result.get( 0 ).getQuantity().intValue() );
  s.getTransaction().commit();
  s.close();
}

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

@Test
public void testNamedQuery() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  s.getNamedQuery("userNameIn")
    .setParameterList( "nameList", new Object[] {"1ovthafew", "turin", "xam"} )
    .list();
  t.commit();
  s.close();
}

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

@Test
public void testSearchUnindexClass() throws Exception {
  createTestData();
  QueryParser parser = new QueryParser( "name", TestConstants.stopAnalyzer );
  Query query = parser.parse( "Elephant" );
  FullTextSession s = Search.getFullTextSession( openSession() );
  Transaction tx = s.beginTransaction();
  try {
    org.hibernate.query.Query hibQuery = s.createFullTextQuery( query, String.class );
    hibQuery.list();
    tx.commit();
    fail();
  }
  catch (IllegalArgumentException iae) {
    log.debug( "success" );
    tx.rollback();
  }
  tx = s.beginTransaction();
  org.hibernate.query.Query hibQuery = s.createFullTextQuery( query, Mammal.class );
  assertItsTheElephant( hibQuery.list() );
  tx.commit();
  s.close();
}

相关文章