@Test public void testCriteriaRestrictionOnKeyManyToOne() { Session s = openSession(); s.beginTransaction(); s.createQuery( "from Order o where o.customer.name = 'Acme'" ).list(); Criteria criteria = s.createCriteria( Order.class ); criteria.createCriteria( "customer" ).add( Restrictions.eq( "name", "Acme" ) ); criteria.list(); s.getTransaction().commit(); s.close(); }
private void deleteData() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); for ( Object entity : s.createQuery( "from Componentizable" ).list() ) { s.delete( entity ); } t.commit(); s.close(); }
public Long createAndStorePerson(Person person) { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Long personId = (Long)session.save(person); session.getTransaction().commit(); return personId; }
@Override public Spell findByName(String name) { Transaction tx = null; Spell result = null; try (Session session = getSessionFactory().openSession()) { tx = session.beginTransaction(); Criteria criteria = session.createCriteria(persistentClass); criteria.add(Restrictions.eq("name", name)); result = (Spell) criteria.uniqueResult(); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } throw e; } return result; } }
public void after() { if (!ManagedSessionContext.hasBind(sessionFactory)) { return; } final Session currentSession = sessionFactory.getCurrentSession(); if (currentSession.isOpen()) { currentSession.close(); } ManagedSessionContext.unbind(sessionFactory); }
@Override public void delete(final Object entity, @Nullable final LockMode lockMode) throws DataAccessException { executeWithNativeSession(session -> { checkWriteOperationAllowed(session); if (lockMode != null) { session.buildLockRequest(new LockOptions(lockMode)).lock(entity); } session.delete(entity); return null; }); }
@TestForIssue(jiraKey = "HHH-4838") @Test public void testNaturalKeyLookupWithConstraint() { Session s = openSession(); Transaction newTx = s.getTransaction(); newTx.begin(); A a1 = new A(); a1.setName( "name1" ); s.persist( a1 ); newTx.commit(); newTx = s.beginTransaction(); getCriteria( s ).add( Restrictions.isNull( "singleD" ) ).uniqueResult(); // put query-result into cache A a2 = new A(); a2.setName( "xxxxxx" ); s.persist( a2 ); newTx.commit(); // Invalidates space A in UpdateTimeStamps region newTx = s.beginTransaction(); Assert.assertTrue( s.getSessionFactory().getStatistics().isStatisticsEnabled() ); s.getSessionFactory().getStatistics().clear(); // should not produce a hit in StandardQuery cache region because there is a constraint getCriteria( s ).add( Restrictions.isNull( "singleD" ) ).uniqueResult(); Assert.assertEquals( 0, s.getSessionFactory().getStatistics().getQueryCacheHitCount() ); s.createQuery( "delete from A" ).executeUpdate(); newTx.commit(); // Shutting down the application s.close(); }
@Test public void testDeclarativeMix() throws Exception { Configuration cfg = new Configuration(); cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" ); cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" ); SessionFactory sf = cfg.buildSessionFactory(); assertNotNull( sf ); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); Query q = s.createQuery( "from Boat" ); assertEquals( 0, q.list().size() ); q = s.createQuery( "from Plane" ); assertEquals( 0, q.list().size() ); tx.commit(); s.close(); sf.close(); } @Test
/** * Call setEntity() on a cacheable query - see FORGE-265 */ public List listEventsOfOrganizer(Person organizer) { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Query query = session.createQuery("from Event ev where ev.organizer = :organizer"); query.setCacheable(true); query.setEntity("organizer", organizer); List result = query.list(); session.getTransaction().commit(); return result; }
/** * Use a Criteria query - see FORGE-247 */ public List listEventsWithCriteria() { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); List result = session.createCriteria(Event.class) .setCacheable(true) .list(); session.getTransaction().commit(); return result; }
@Test public void testEnablingJoinFetchProfileAgainstSelfReferentialAssociation() { Session s = openSession(); s.beginTransaction(); s.enableFetchProfile( Employee.FETCH_PROFILE_TREE ); s.createCriteria( Employee.class ) .add( Restrictions.isNull( "manager" ) ) .list(); s.getTransaction().commit(); s.close(); } }
Transaction tx = s.beginTransaction(); s.persist( france ); s.persist( c1 ); s.persist( singleManyToOne ); tx.commit(); s.close(); s.getSessionFactory().getCache().evictNaturalIdRegions(); Statistics stats = sessionFactory().getStatistics(); stats.setStatisticsEnabled( true ); tx = s.beginTransaction(); Criteria criteria = s.createCriteria( NaturalIdOnManyToOne.class ); criteria.add( Restrictions.naturalId().set( "citizen", c1 ) ); criteria.setCacheable( true ); List results = criteria.list(); assertEquals( 1, results.size() ); assertEquals( "NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount() ); tx.rollback(); s.close();
@Test public void testDiscriminatorFiltering() throws Exception { if ( ( getDialect() instanceof HSQLDialect ) ) return; Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery("from C1 c1 left join c1.c2s c2").list(); s.createCriteria(C1.class).createCriteria("c2s").list(); t.commit(); s.close(); }
public SyntaxChecker checkList() { Session s = openSession(); s.beginTransaction(); Query query = s.createQuery( hql ); preparer.prepare( query ); query.list(); s.getTransaction().commit(); s.close(); return this; }
@Test public void testPackageQueries() throws Exception { Session s = openSession(); Transaction tx = s.beginTransaction(); Plane p = new Plane(); s.persist( p ); Query q = s.getNamedQuery( "plane.getAll" ); assertEquals( 1, q.list().size() ); s.delete( q.list().get( 0 ) ); tx.commit(); s.close(); }
@After public void cleanUpData() { Session s = openSession(); s.beginTransaction(); s.delete( s.get( Store.class, 1 ) ); s.delete( s.get( Product.class, "007" ) ); s.getTransaction().commit(); s.close(); }
@Test public void testGetResultSize() throws Exception { Session s = openSession(); FullTextSession session = Search.getFullTextSession( s ); Transaction tx = s.beginTransaction(); QueryDescriptor query = ElasticsearchQueries.fromJson( "{ 'query': { 'match' : { 'abstract' : 'Hibernate' } } }" ); FullTextQuery fullTextQuery = session.createFullTextQuery( query, ScientificArticle.class ); assertThat( fullTextQuery.getResultSize() ).isEqualTo( 4 ); tx.commit(); s.close(); }
@Test public void testIdentityColumnGeneratedIds() { Session s = openSession(); s.beginTransaction(); MyEntity myEntity = new MyEntity( "test" ); Long id = ( Long ) s.save( myEntity ); assertNotNull( "identity column did not force immediate insert", id ); assertEquals( id, myEntity.getId() ); s.delete( myEntity ); s.getTransaction().commit(); s.close(); }
public String addProductImage(final String productId, final String filename){ Session session = sessionFactory.openSession(); session.beginTransaction(); ProductImage image = new ProductImage(); image.setProductId(Long.parseLong(productId)); image.setPath(filename); try { String res = session.save(image).toString(); session.getTransaction().commit(); return res; } catch (HibernateException e) { System.out.print(e.getMessage()); session.getTransaction().rollback(); } finally { session.close(); } return ""; }