Refine search
/** * Save or update any entities not in the current hibernate session and then * flush the hibernate session. * * @see org.springframework.batch.item.ItemWriter#write(java.util.List) */ @Override public void write(List<? extends T> items) { doWrite(sessionFactory, items); sessionFactory.getCurrentSession().flush(); if(clearSession) { sessionFactory.getCurrentSession().clear(); } }
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //flush a batch of inserts and release memory: session.flush(); session.clear(); } } tx.commit(); session.close();
@Test public void testAfterDelete() throws Exception { Session s = openSession(); s.beginTransaction(); Foo foo = new Foo(); s.save(foo); s.flush(); s.delete(foo); s.save(foo); s.delete(foo); s.getTransaction().commit(); s.close(); }
@Test public void testOneToManyFetchEager() throws Exception { Branch b = new Branch(); Session s = openSession( ); s.getTransaction().begin(); s.persist( b ); s.flush(); Leaf l = new Leaf(); l.setBranch( b ); s.persist( l ); s.flush(); s.clear(); s.createCriteria( Branch.class ).list(); s.getTransaction().rollback(); s.close(); }
@Test(expected = TransactionRequiredException.class) public void testInsertOutsideActiveTransaction() { Session session = openSession(); // Illegal insertion of entity outside of active transaction. StrTestEntity entity = new StrTestEntity( "data" ); session.persist( entity ); session.flush(); session.close(); }
@Test(expected = PersistenceException.class) @TestForIssue(jiraKey = "HHH-12132") public void testBooleanTypeLegacyBehavior() throws Exception { rebuildSessionFactory( configuration -> { configuration.setProperty( "hibernate.dialect.hana.use_legacy_boolean_type", Boolean.TRUE.toString() ); } ); Session s = openSession(); s.beginTransaction(); BooleanEntity entity = new BooleanEntity(); entity.key = Integer.valueOf( 2 ); entity.bool = Boolean.FALSE; s.persist( entity ); s.flush(); s.getTransaction().commit(); s.clear(); Query<BooleanEntity> query = s.createQuery( "select b from " + ENTITY_NAME + " b where bool = true", BooleanEntity.class ); query.getSingleResult(); }
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<888888; i++ ) { TableA record = new TableA(); record.setXXXX(); session.save(record) if ( i % 50 == 0 ) { //50, same as the JDBC batch size //flush a batch of inserts and release memory: session.flush(); session.clear(); } } tx.commit(); session.close();
protected void cleanupTestData() { // clean up the test data Session s = openSession(); s.beginTransaction(); // User is the non-inverse side... List<User> users = s.createQuery( "from User" ).list(); for ( User user : users ) { s.delete( user ); } s.flush(); s.createQuery( "delete Group" ).executeUpdate(); s.getTransaction().commit(); s.close(); }
@Test public void testOptimisticLockExcludeOnNameProperty() throws Exception { Conductor c = new Conductor(); c.setName( "Bob" ); Session s = openSession(); s.getTransaction().begin(); s.persist( c ); s.flush(); s.clear(); c = ( Conductor ) s.get( Conductor.class, c.getId() ); Long version = c.getVersion(); c.setName( "Don" ); s.flush(); s.clear(); c = ( Conductor ) s.get( Conductor.class, c.getId() ); assertEquals( version, c.getVersion() ); s.getTransaction().rollback(); s.close(); }
@Test(expected = TransactionRequiredException.class) public void testDeleteOutsideActiveTransaction() { Session session = openSession(); // Revision 1 session.getTransaction().begin(); StrTestEntity entity = new StrTestEntity( "data" ); session.persist( entity ); session.getTransaction().commit(); // Illegal removal of entity outside of active transaction. session.delete( entity ); session.flush(); 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 testWriteAndFlushSunnyDayHibernate4() throws Exception { writer.setSessionFactory(factory); when(factory.getCurrentSession()).thenReturn(currentSession); when(currentSession.contains("foo")).thenReturn(true); when(currentSession.contains("bar")).thenReturn(false); currentSession.saveOrUpdate("bar"); currentSession.flush(); currentSession.clear(); List<String> items = Arrays.asList(new String[] { "foo", "bar" }); writer.write(items); }
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //flush a batch of inserts and release memory: session.flush(); session.clear(); } } tx.commit(); session.close();
@Test public void testBagOneToMany() throws Exception { Session s = openSession(); s.beginTransaction(); Baz baz = new Baz(); List list = new ArrayList(); baz.setBazez(list); list.add( new Baz() ); s.save(baz); s.flush(); list.add( new Baz() ); s.flush(); list.add( 0, new Baz() ); s.flush(); s.delete( list.remove(1) ); s.flush(); s.delete(baz); s.getTransaction().commit(); s.close(); }
@Test public void testForeignGenerator() throws Exception { assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "patient_id", metadata() ) ); Person e = new Person(); Session s = openSession( ); s.getTransaction().begin(); s.persist( e ); MedicalHistory d = new MedicalHistory(); d.patient = e; s.persist( d ); s.flush(); s.clear(); d = (MedicalHistory) s.get( MedicalHistory.class, e.id); assertEquals( e.id, d.id ); s.delete( d ); s.delete( d.patient ); s.getTransaction().rollback(); s.close(); }
@Test public void testIndexedCollectionOfElements() throws Exception { Sale sale = new Sale(); Contact contact = new Contact(); contact.setName( "Emmanuel" ); sale.getContacts().add(contact); Session s = openSession( ); s.getTransaction().begin(); s.save( sale ); s.flush(); s.get( Sale.class, sale.getId() ); assertEquals( 1, sale.getContacts().size() ); s.getTransaction().rollback(); s.close(); }
@Test @TestForIssue(jiraKey = "HHH-12132") public void testBooleanTypeDefaultBehavior() throws Exception { rebuildSessionFactory(); Session s = openSession(); s.beginTransaction(); BooleanEntity entity = new BooleanEntity(); entity.key = Integer.valueOf( 1 ); entity.bool = Boolean.TRUE; s.persist( entity ); s.flush(); s.getTransaction().commit(); s.clear(); Query<BooleanEntity> legacyQuery = s.createQuery( "select b from " + ENTITY_NAME + " b where bool = true", BooleanEntity.class ); BooleanEntity retrievedEntity = legacyQuery.getSingleResult(); assertEquals( Integer.valueOf( 1 ), retrievedEntity.key ); assertTrue( retrievedEntity.bool ); }
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //flush a batch of inserts and release memory: session.flush(); session.clear(); } } tx.commit(); session.close();
@Test public void testSaveAddDelete() throws Exception { Session s = openSession(); s.beginTransaction(); Baz baz = new Baz(); Set bars = new HashSet(); baz.setCascadingBars( bars ); s.save( baz ); s.flush(); baz.getCascadingBars().add( new Bar() ); s.delete(baz); s.flush(); s.getTransaction().commit(); s.close(); }