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; }
@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();
@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(); }
@Override public Person findByName(String name) { return (Person) this.sessionFactory.getCurrentSession().createQuery( "from Person person where person.name = :name").setParameter("name", name).getSingleResult(); }
@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(); }
public List listEvents() { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); List result = session.createQuery("from Event").setCacheable(true).list(); session.getTransaction().commit(); return result; }
private void cleanupData() { Session session = openSession(); session.beginTransaction(); session.createQuery( "delete EmployeeInfo" ).executeUpdate(); session.createQuery( "delete Employee" ).executeUpdate(); session.getTransaction().commit(); session.close(); }
@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(); }
@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(); }
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();
@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 ); } ); }
@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(); }
@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();
@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(); }
@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(); }
@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(); }
@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(); }
@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(); }
@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(); }
@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(); }