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

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

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

Query介绍

[英]Represents an HQL/JPQL query or a compiled Criteria query
[中]表示HQL/JPQL查询或编译条件查询

代码示例

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

  1. /**
  2. * Call setEntity() on a cacheable query - see FORGE-265
  3. */
  4. public List listEventsOfOrganizer(Person organizer) {
  5. Session session = sessionFactory.getCurrentSession();
  6. session.beginTransaction();
  7. Query query = session.createQuery("from Event ev where ev.organizer = :organizer");
  8. query.setCacheable(true);
  9. query.setEntity("organizer", organizer);
  10. List result = query.list();
  11. session.getTransaction().commit();
  12. return result;
  13. }

代码示例来源:origin: citerus/dddsample-core

  1. @Override
  2. public HandlingHistory lookupHandlingHistoryOfCargo(final TrackingId trackingId) {
  3. return new HandlingHistory(getSession().createQuery(
  4. "from HandlingEvent where cargo.trackingId = :tid").
  5. setParameter("tid", trackingId).
  6. list()
  7. );
  8. }

代码示例来源:origin: gocd/gocd

  1. Modification findLatestModification(final MaterialInstance expandedInstance) {
  2. Modifications modifications = cachedModifications(expandedInstance);
  3. if (modifications != null && !modifications.isEmpty()) {
  4. return modifications.get(0);
  5. }
  6. String cacheKey = latestMaterialModificationsKey(expandedInstance);
  7. synchronized (cacheKey) {
  8. Modification modification = (Modification) getHibernateTemplate().execute((HibernateCallback) session -> {
  9. Query query = session.createQuery("FROM Modification WHERE materialId = ? ORDER BY id DESC");
  10. query.setMaxResults(1);
  11. query.setLong(0, expandedInstance.getId());
  12. return query.uniqueResult();
  13. });
  14. goCache.put(cacheKey, new Modifications(modification));
  15. return modification;
  16. }
  17. }

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

  1. /**
  2. * Apply the given name parameter to the given Query object.
  3. * @param queryObject the Query object
  4. * @param paramName the name of the parameter
  5. * @param value the value of the parameter
  6. * @throws HibernateException if thrown by the Query object
  7. */
  8. @Deprecated
  9. @SuppressWarnings({"rawtypes", "deprecation"})
  10. protected void applyNamedParameterToQuery(org.hibernate.Query queryObject, String paramName, Object value)
  11. throws HibernateException {
  12. if (value instanceof Collection) {
  13. queryObject.setParameterList(paramName, (Collection<?>) value);
  14. }
  15. else if (value instanceof Object[]) {
  16. queryObject.setParameterList(paramName, (Object[]) value);
  17. }
  18. else {
  19. queryObject.setParameter(paramName, value);
  20. }
  21. }

代码示例来源:origin: gocd/gocd

  1. @Override
  2. public boolean deleteUsers(List<String> userNames) {
  3. return (Boolean) transactionTemplate.execute((TransactionCallback) status -> {
  4. String queryString = "delete from User where name in (:userNames)";
  5. Query query = sessionFactory.getCurrentSession().createQuery(queryString);
  6. query.setParameterList("userNames", userNames);
  7. query.executeUpdate();
  8. return Boolean.TRUE;
  9. });
  10. }

代码示例来源:origin: gocd/gocd

  1. @Override
  2. public List<Plugin> getAllPlugins() {
  3. return (List<Plugin>) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
  4. Query query = sessionFactory.getCurrentSession().createQuery("FROM " + Plugin.class.getSimpleName());
  5. query.setCacheable(true);
  6. return query.list();
  7. });
  8. }

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

  1. q.setMaxResults( 10 );
  2. assertTrue( q.list().size()==3 );
  3. q = s.createQuery("from Simple s");
  4. q.setMaxResults( 1 );
  5. assertTrue( q.list().size()==1 );
  6. q = s.createQuery("from Simple s");
  7. assertTrue( q.list().size() == 3 );
  8. q = s.createQuery("from Simple s where s.name = ?");
  9. q.setString( 0, "Simple 1" );
  10. assertTrue( q.list().size()==1 );
  11. q = s.createQuery("from Simple s where s.name = ? and upper(s.name) = ?");
  12. q.setString(1, "SIMPLE 1");
  13. q.setString( 0, "Simple 1" );
  14. q.setFirstResult(0);
  15. assertTrue( q.iterate().hasNext() );
  16. q = s.createQuery("from Simple s where s.name = :foo and upper(s.name) = :bar or s.count=:count or s.count=:count + 1");
  17. q.setParameter( "bar", "SIMPLE 1" );
  18. q.setString( "foo", "Simple 1" );
  19. q.setInteger("count", 69);
  20. q.setFirstResult(0);
  21. assertTrue( q.iterate().hasNext() );
  22. q = s.createQuery("select s.id from Simple s");
  23. q.setFirstResult(1);
  24. q.setMaxResults( 2 );
  25. iter = q.iterate();
  26. int i=0;
  27. while ( iter.hasNext() ) {
  28. q.setParameter("s", simple);

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

  1. q.setCacheRegion("foo");
  2. q.setCacheable(true);
  3. q.setString(0, "Simple 1");
  4. assertTrue( q.list().size()==1 );
  5. assertTrue( q.list().size()==1 );
  6. assertTrue( q.list().size()==1 );
  7. q = s.createQuery("from Simple s where s.name=:name");
  8. q.setCacheRegion("foo");
  9. q.setCacheable(true);
  10. q.setString("name", "Simple 1");
  11. assertTrue( q.list().size()==1 );
  12. simple = (Simple) q.list().get(0);
  13. q.setString("name", "Simple 2");
  14. assertTrue( q.list().size()==0 );
  15. assertTrue( q.list().size()==0 );
  16. simple.setName("Simple 2");
  17. assertTrue( q.list().size()==1 );
  18. assertTrue( q.list().size()==1 );
  19. t.commit();
  20. t.commit();
  21. q.setCacheRegion("foo");
  22. q.setCacheable(true);
  23. q.setString(0, "Simple 1");
  24. assertTrue( q.list().size()==0 );
  25. assertTrue( q.list().size()==0 );

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

  1. @Test
  2. public void test2ndLevelSubClass() throws Exception {
  3. Session s = openSession();
  4. Transaction tx = s.beginTransaction();
  5. A320b a = new A320b();
  6. a.setJavaEmbeddedVersion( "Elephant" );
  7. a.setNbrOfSeats( 300 );
  8. s.persist( a );
  9. tx.commit();
  10. s.close();
  11. s = openSession();
  12. tx = s.beginTransaction();
  13. Query q = s.createQuery( "from " + A320.class.getName() + " as a where a.javaEmbeddedVersion = :version" );
  14. q.setString( "version", "Elephant" );
  15. List a320s = q.list();
  16. assertNotNull( a320s );
  17. assertEquals( 1, a320s.size() );
  18. tx.commit();
  19. s.close();
  20. }

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

  1. @Test
  2. public void testDeclarativeMix() throws Exception {
  3. Configuration cfg = new Configuration();
  4. cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
  5. cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
  6. SessionFactory sf = cfg.buildSessionFactory();
  7. assertNotNull( sf );
  8. Session s = sf.openSession();
  9. Transaction tx = s.beginTransaction();
  10. Query q = s.createQuery( "from Boat" );
  11. assertEquals( 0, q.list().size() );
  12. q = s.createQuery( "from Plane" );
  13. assertEquals( 0, q.list().size() );
  14. tx.commit();
  15. s.close();
  16. sf.close();
  17. }
  18. @Test

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

  1. @Test
  2. public void testDeleteSingleValue() {
  3. final String name = "Name";
  4. final String lastName = "LastName";
  5. final String fullName = name + " " + lastName;
  6. final FromEntity fromEntity = createFrom( name, lastName );
  7. final DestinationEntity destinationEntity = createDestination( fromEntity, fullName );
  8. Session session = openSession();
  9. session.getTransaction().begin();
  10. Query delete = session.getNamedQuery( "DestinationEntity.delete" );
  11. delete.setParameterList( "ids", Collections.singletonList( destinationEntity.id ) );
  12. int executeUpdate = delete.executeUpdate();
  13. assertEquals( 1, executeUpdate );
  14. session.getTransaction().commit();
  15. session.close();
  16. session = openSession();
  17. DestinationEntity get = (DestinationEntity) session.get( DestinationEntity.class, destinationEntity.id );
  18. session.close();
  19. assertNull( get );
  20. }

代码示例来源:origin: org.jbpm/jbpm-bam

  1. @SuppressWarnings("unchecked")
  2. public static List<NodeInstanceLog> findNodeInstances(long processInstanceId, String nodeId) {
  3. Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  4. session.beginTransaction();
  5. List<NodeInstanceLog> result = session.createQuery(
  6. "from NodeInstanceLog as log where log.processInstanceId = ? and log.nodeId = ?")
  7. .setLong(0, processInstanceId)
  8. .setString(1, nodeId).list();
  9. session.getTransaction().commit();
  10. return result;
  11. }

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

  1. @Test
  2. public void testCharLengthFunction() {
  3. final Session s = openSession();
  4. s.getTransaction().begin();
  5. Query query = session.createQuery( "select char_length('123456') from Product" );
  6. assertEquals(6, ((Number) query.uniqueResult()).intValue());
  7. s.getTransaction().commit();
  8. s.close();
  9. }

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

  1. list = q.list();
  2. if ( !(getDialect() instanceof SAPDBDialect) ) assertTrue( list.size()==2 );
  3. nameList.add( "Bar" );
  4. nameList.add( "Bar Two" );
  5. q.setParameterList( "nameList", nameList );
  6. q.setParameter( "stringVal", "a string" );
  7. list = q.list();
  8. if ( !(getDialect() instanceof SAPDBDialect) ) assertTrue( list.size()==2 );
  9. q.setParameterList("nameList", (Collection)null);
  10. fail("Should throw a QueryException when passing a null!");
  11. Object result = q.uniqueResult();
  12. q.setString( "name", "Bar%" );
  13. list = q.list();
  14. assertTrue( list.size()==1 );
  15. q.setInteger( "valueX", bar.getX() + 1 );
  16. list = q.list();
  17. assertTrue( ((Bar) list.get( 0 )).getX() == bar.getX() );
  18. q.setInteger( "valueX", bar2.getX() + 1 );
  19. list = q.list();
  20. assertTrue( ((Bar)list.get(0)).getX() == bar2.getX());
  21. txn.commit();

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

  1. public SyntaxChecker checkList() {
  2. Session s = openSession();
  3. s.beginTransaction();
  4. Query query = s.createQuery( hql );
  5. preparer.prepare( query );
  6. query.list();
  7. s.getTransaction().commit();
  8. s.close();
  9. return this;
  10. }

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

  1. assertFalse( s.isDefaultReadOnly() );
  2. Transaction t = s.beginTransaction();
  3. s.save( cOrig );
  4. checkContainer( cOrig, expectedInitializedObjects, expectedReadOnlyObjects, s );
  5. t.commit();
  6. .setMaxResults( 1 )
  7. .list();
  8. .setMaxResults( 1 )
  9. .list();
  10. .setMaxResults( 1 )
  11. .list();
  12. t.commit();
  13. t.commit();

代码示例来源:origin: org.ikasan/ikasan-connector-basefiletransfer

  1. Session session = this.sessionFactory.openSession();
  2. try
  3. Query query = session.createQuery(hibernateQuery.toString());
  4. query.setParameter(CLIENT_ID, clientId);
  5. query.setParameter(CREATED_DATE_TIME, cal.getTime().getTime());
  6. query.setMaxResults(maxRows);
  7. fileFilters = query.list();
  8. if (fileFilters != null)
  9. session.delete(fileFilter);
  10. session.flush();
  11. sb.append(hibernateQuery.toString());
  12. sb.append("].");
  13. logger.error(sb.toString() + " " + e.getMessage(), e);
  14. throw e;

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

  1. @Test
  2. public void testPSCache() throws Exception {
  3. Session s = openSession();
  4. Transaction txn = s.beginTransaction();
  5. for ( int i=0; i<10; i++ ) s.save( new Foo() );
  6. Query q = s.createQuery("from Foo");
  7. q.setMaxResults(2);
  8. q.setFirstResult(5);
  9. assertTrue( q.list().size()==2 );
  10. q = s.createQuery("from Foo");
  11. assertTrue( q.list().size()==10 );
  12. assertTrue( q.list().size()==10 );
  13. q.setMaxResults(3);
  14. q.setFirstResult(3);
  15. assertTrue( q.list().size()==3 );
  16. q = s.createQuery("from Foo");
  17. assertTrue( q.list().size()==10 );
  18. txn.commit();
  19. s.close();
  20. s = openSession();
  21. txn = s.beginTransaction();
  22. q = s.createQuery("from Foo");
  23. assertTrue( q.list().size()==10 );
  24. q.setMaxResults(5);
  25. assertTrue( q.list().size()==5 );
  26. doDelete( s, "from Foo" );
  27. txn.commit();
  28. s.close();
  29. }

代码示例来源:origin: sakaiproject/sakai

  1. public List<StudentGradingSummaryData> getStudentGradingSummaryData(final Long publishedAssessmentId, final String agentIdString) {
  2. final HibernateCallback<List<StudentGradingSummaryData>> hcb = session -> {
  3. Query q = session.createQuery(
  4. "select s " +
  5. "from StudentGradingSummaryData s " +
  6. "where s.publishedAssessmentId = :id and s.agentId = :agent");
  7. q.setLong("id", publishedAssessmentId);
  8. q.setString("agent", agentIdString);
  9. return q.list();
  10. };
  11. return getHibernateTemplate().execute(hcb);
  12. }

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

  1. @Test
  2. public void testHql() {
  3. Session session = openSession();
  4. session.beginTransaction();
  5. Query qry = session.createQuery( "from Door" );
  6. qry.getLockOptions().setLockMode( LockMode.PESSIMISTIC_WRITE );
  7. qry.setFirstResult( 2 );
  8. qry.setMaxResults( 2 );
  9. @SuppressWarnings("unchecked") List<Door> results = qry.list();
  10. assertEquals( 2, results.size() );
  11. for ( Door door : results ) {
  12. assertEquals( LockMode.PESSIMISTIC_WRITE, session.getCurrentLockMode( door ) );
  13. }
  14. session.getTransaction().commit();
  15. session.close();
  16. }

相关文章