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 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); }
@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(); }
private Componentizable setupComponentData() throws SQLException { Session session = sessionFactory().openSession(); session.beginTransaction(); Componentizable c = new Componentizable(); c.setNickName( "Flacky" ); Component component = new Component(); component.setName("flakky comp"); SubComponent subComponent = new SubComponent(); subComponent.setSubName("subway"); component.setSubComponent( subComponent ); c.setComponent( component ); session.save( c ); session.getTransaction().commit(); session.clear(); return c; }
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();
@Test public void testMappedSuperclassAndSecondaryTable() throws Exception { Session s = openSession( ); s.getTransaction().begin(); C c = new C(); c.setAge( 12 ); c.setCreateDate( new Date() ); c.setName( "Bob" ); s.persist( c ); s.flush(); s.clear(); c= (C) s.get( C.class, c.getId() ); assertNotNull( c.getCreateDate() ); assertNotNull( c.getName() ); s.getTransaction().rollback(); s.close(); }
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 testTargetOnEmbedded() throws Exception { Session s = openSession(); s.getTransaction().begin(); Luggage l = new LuggageImpl(); l.setHeight( 12 ); l.setWidth( 12 ); Owner o = new OwnerImpl(); o.setName( "Emmanuel" ); l.setOwner( o ); s.persist( l ); s.flush(); s.clear(); l = (Luggage) s.get(LuggageImpl.class, ( (LuggageImpl) l).getId() ); assertEquals( "Emmanuel", l.getOwner().getName() ); s.getTransaction().rollback(); s.close(); }
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();
@SuppressWarnings("deprecation") @Test public void testWriteAndFlushSunnyDayHibernate3() throws Exception { this.writer.setSessionFactory(this.factory); when(this.currentSession.contains("foo")).thenReturn(true); when(this.currentSession.contains("bar")).thenReturn(false); this.currentSession.saveOrUpdate("bar"); this.currentSession.flush(); this.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 @TestForIssue(jiraKey = "HHH-7781") public void testPaginationWithCastOperator() { doInHibernate( this::sessionFactory, session -> { for ( int i = 40; i < 50; i++ ) { session.persist( new Product2( i, "Kit" + i ) ); } session.flush(); session.clear(); List<Object[]> list = session.createQuery( "select p.id, cast(p.id as string) as string_id from Product2 p order by p.id" ) .setFirstResult( 1 ).setMaxResults( 2 ).list(); assertEquals( 2, list.size() ); assertArrayEquals( new Object[] { 41, "41" }, list.get( 0 ) ); assertArrayEquals( new Object[] { 42, "42" }, list.get( 1 ) ); } ); }
@Test public void testIdInEmbeddableSuperclass() throws Exception { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); FirTree chrismasTree = new FirTree(); s.persist( chrismasTree ); tx.commit(); s.clear(); tx = s.beginTransaction(); chrismasTree = ( FirTree ) s.get( FirTree.class, chrismasTree.getId() ); assertNotNull( chrismasTree ); s.delete( chrismasTree ); tx.commit(); s.close(); }
@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 testTemporalKeyMap() throws Exception { Session s = openSession(); Transaction tx = s.beginTransaction(); Atmosphere atm = new Atmosphere(); atm.colorPerDate.put( new Date(1234567000), "red" ); s.persist( atm ); s.flush(); s.clear(); atm = (Atmosphere) s.get( Atmosphere.class, atm.id ); assertEquals( 1, atm.colorPerDate.size() ); final Date date = atm.colorPerDate.keySet().iterator().next(); final long diff = new Date( 1234567000 ).getTime() - date.getTime(); assertTrue( "24h diff max", diff >= 0 && diff < 24*60*60*1000 ); tx.rollback(); s.close(); }
@Test @TestForIssue(jiraKey = "HHH-13147") public void testPersistingCachedEntityWithIdentityBasedIdentifier() { doInHibernate( this::sessionFactory, session -> { SomeEntity entity = new SomeEntity(); session.persist( entity ); entity.setName( "foo" ); session.persist( entity ); session.flush(); session.clear(); } ); }
@Test public void testPropertyOverriding() throws Exception { Furniture fur = new Furniture(); fur.weight = 3; Session s = openSession(); s.persist( fur ); Transaction tx = s.beginTransaction(); tx.commit(); s.clear(); tx = s.beginTransaction(); fur = ( Furniture ) s.get( Furniture.class, fur.getId() ); assertEquals( 5, fur.weight ); s.delete( fur ); tx.commit(); s.close(); }