public Long createAndStorePerson(Person person) { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Long personId = (Long)session.save(person); session.getTransaction().commit(); return personId; }
public final Serializable doInsideTransaction(Session s, java.util.function.Supplier<Serializable> sp) { s.getTransaction().begin(); try { final Serializable result = sp.get(); s.getTransaction().commit(); return result; } catch (Exception e) { if ( s.getTransaction().getStatus() == TransactionStatus.ACTIVE ) { s.getTransaction().rollback(); } throw e; } } }
@Override protected void cleanupTest() throws Exception { try (Session session = openSession()) { session.getTransaction().begin(); try { session.createQuery( "delete from MyEntity" ).executeUpdate(); session.getTransaction().commit(); } catch (Exception e) { if ( session.getTransaction().isActive() ) { session.getTransaction().rollback(); } throw e; } } }
@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; } }
private void rollbackTransaction(UnitOfWork unitOfWork, Session session) { if (!unitOfWork.transactional()) { return; } final Transaction txn = session.getTransaction(); if (txn != null && txn.getStatus().canRollback()) { txn.rollback(); } }
private void commitTransaction(UnitOfWork unitOfWork, Session session) { if (!unitOfWork.transactional()) { return; } final Transaction txn = session.getTransaction(); if (txn != null && txn.getStatus().canRollback()) { txn.commit(); } }
@Test public void testPersistEmbeddedWithNullEmbeddedList() throws Exception { final Session session = openSession(); Transaction transaction = session.beginTransaction(); AccountWithPhone wombatSoftware = new AccountWithPhone( "1", "Mobile account 1" ); wombatSoftware.setPhoneNumber( null ); session.persist( wombatSoftware ); transaction.commit(); session.clear(); transaction = session.beginTransaction(); AccountWithPhone loadedUser = (AccountWithPhone) session.get( AccountWithPhone.class, wombatSoftware.getId() ); assertThat( loadedUser ).as( "Cannot load persisted object with nested embeddables" ).isNotNull(); // It is not null because of the list of elements assertThat( loadedUser.getPhoneNumber() ).isNotNull(); assertThat( loadedUser.getPhoneNumber().getMain() ).isNull(); assertThat( loadedUser.getPhoneNumber().getAlternatives() ).isEmpty(); session.delete( loadedUser ); transaction.commit(); session.clear(); transaction = session.beginTransaction(); assertThat( session.get( AccountWithPhone.class, wombatSoftware.getId() ) ).isNull(); transaction.commit(); session.close(); }
@Override protected void cleanupTest() throws Exception { final Session s = openSession(); s.getTransaction().begin(); s.createQuery( "delete from Product" ).executeUpdate(); s.getTransaction().commit(); s.close(); }
@After public void removeCloudAndSnowflakes() { Session session = sessions.openSession(); Transaction transaction = session.beginTransaction(); if ( cloud != null ) { Cloud cloudToDelete = (Cloud) session.get( Cloud.class, cloud.getId() ); for ( SnowFlake current : cloudToDelete.getProducedSnowFlakes() ) { session.delete( current ); } for ( SnowFlake current : cloudToDelete.getBackupSnowFlakes() ) { session.delete( current ); } session.delete( cloudToDelete ); } transaction.commit(); session.close(); assertThat( TestHelper.getNumberOfEntities( sessions ) ).isEqualTo( 0 ); assertThat( TestHelper.getNumberOfAssociations( sessions ) ).isEqualTo( 0 ); }
public Account getAccount(Long accountId) { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Account account = (Account)session.load(Account.class, accountId); session.getTransaction().commit(); return account; }
private void insertTestEntity(String name) { final TestEntity entity = new TestEntity(); entity.setName( name ); try (Session s = openSession()) { session.getTransaction().begin(); try { s.save( entity ); s.getTransaction().commit(); } catch (Exception e) { if ( s.getTransaction().isActive() ) { s.getTransaction().rollback(); } throw e; } } }
@Test public void testPersist() { Session s = openSession(); s.getTransaction().begin(); s.persist( b ); s.getTransaction().commit(); s.close(); check(); }
@Test @TestForIssue(jiraKey = "OGM-917") public void canRetrieveListOfEntityWithSingleColumnEmbeddableId() throws Exception { Session session = openSession(); Transaction transaction = session.beginTransaction(); // given session.persist( new SingleBoardComputer( new SingleBoardComputerPk( "sbc-1" ), "Raspberry Pi" ) ); session.persist( new SingleBoardComputer( new SingleBoardComputerPk( "sbc-2" ), "BeagleBone" ) ); transaction.commit(); session.clear(); transaction = session.beginTransaction(); // when @SuppressWarnings("unchecked") List<SingleBoardComputer> computers = session.createQuery( "From SingleBoardComputer" ).list(); // then assertThat( computers ).onProperty( "name" ).containsOnly( "Raspberry Pi", "BeagleBone" ); for ( SingleBoardComputer sbc : computers ) { session.delete( sbc ); } transaction.commit(); session.close(); }
@Test public void testSimpleEntityMassIndexing() throws Exception { { Session session = openSession(); Transaction transaction = session.beginTransaction(); Insurance insurance = new Insurance(); insurance.setName( "Insurance Corporation" ); session.persist( insurance ); transaction.commit(); session.clear(); session.close(); } { purgeAll( Insurance.class ); startAndWaitMassIndexing( Insurance.class ); } { FullTextSession session = Search.getFullTextSession( openSession() ); QueryBuilder queryBuilder = session.getSearchFactory().buildQueryBuilder().forEntity( Insurance.class ).get(); Query luceneQuery = queryBuilder.keyword().wildcard().onField( "name" ).matching( "ins*" ).createQuery(); Transaction transaction = session.beginTransaction(); @SuppressWarnings("unchecked") List<Insurance> list = session.createFullTextQuery( luceneQuery ).list(); assertThat( list ).hasSize( 1 ); assertThat( list.get( 0 ).getName() ).isEqualTo( "Insurance Corporation" ); transaction.commit(); session.clear(); session.close(); } }
@Test public void testNumericFieldQuery() throws Exception { Session s = openSession(); FullTextSession session = Search.getFullTextSession( s ); Transaction tx = s.beginTransaction(); QueryDescriptor query = ElasticsearchQueries.fromJson( "{ 'query': { 'range' : { 'wordCount' : { 'gte' : 8, 'lt' : 10 } } } }" ); List<?> result = session.createFullTextQuery( query ).list(); assertThat( result ).extracting( "title" ).containsExactlyInAnyOrder( "Latest in ORM", "ORM for beginners" ); tx.commit(); s.close(); }
@SuppressWarnings("unchecked") private void searchSong(Session session) throws Exception { FullTextSession fullTextSession = Search.getFullTextSession( session ); Transaction tx = session.beginTransaction(); QueryParser parser = new QueryParser( XMPDM.ARTIST.getName(), TestConstants.standardAnalyzer ); Query query = parser.parse( "Emmanuel" ); List<Song> result = fullTextSession.createFullTextQuery( query ).list(); assertEquals( "Emmanuel is not an artist", 0, result.size() ); query = parser.parse( "Hardy" ); result = fullTextSession.createFullTextQuery( query ).list(); assertEquals( "Hardy is the artist", 1, result.size() ); tx.commit(); }
@Test public void testMapKeyType() throws Exception { Matrix m = new Matrix(); m.getMvalues().put( 1, 1.1f ); Session s = openSession(); Transaction tx = s.beginTransaction(); s.persist( m ); s.flush(); s.clear(); m = (Matrix) s.get( Matrix.class, m.getId() ); assertEquals( 1.1f, m.getMvalues().get( 1 ), 0.01f ); tx.rollback(); s.close(); }
@After public void cleanup() { Session s = openSession(); s.getTransaction().begin(); Parent parent = s.get( Parent.class, parentId ); parent.getChildren().clear(); s.delete( parent ); s.getTransaction().commit(); s.close(); parentId = null; }
public List listEvents() { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); List result = session.createQuery("from Event").setCacheable(true).list(); session.getTransaction().commit(); return result; }
private DestinationEntity createDestination(FromEntity fromEntity, String fullName) { final DestinationEntity destinationEntity = new DestinationEntity( fromEntity, fullName ); Session session = openSession(); session.getTransaction().begin(); session.save( destinationEntity ); session.getTransaction().commit(); session.close(); return destinationEntity; }