Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User userA = (User)session.get(User.class, 1101); transaction.commit(); session.close(); // Since session is closed, userA is detached. session = sessionFactory.openSession(); transaction = session.beginTransaction(); User userB = (User)session.get(User.class, 1101); //Now here, userB represents the same persistent row as userA. //When an attempt to reattach userA occurs, an exception is thrown session.update(userA); transaction.commit(); session.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 testJoinToSecondaryTable() throws Exception { Session s = openSession(); Transaction tx = s.beginTransaction(); Life life = new Life(); life.duration = 15; life.fullDescription = "Long long description"; s.persist( life ); tx.commit(); s.close(); }
@Test public void testQuery() { Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery("from LineItem ol where ol.order.id.customerId = 'C111'").list(); t.commit(); s.close(); }
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();
Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User userA = (User)session.get(User.class, 1101); transaction.commit(); session.close(); //userA is now detached as session is closed. session = sessionFactory.openSession(); transaction = session.beginTransaction(); User userB = (User)session.get(User.class, 1101); User userC = (User)session.merge(userA); if (userB == userC) { System.out.println("Reattched user is equal"); } transaction.commit(); session.close();
@Test public void testSearchedCaseStatementFixture() { Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery( "select case when p.name = 'Steve' then 'x' else 'y' end from Person p" ) .list(); t.commit(); 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();
Session sess = factory.openSession(); Transaction tx; try { tx = sess.beginTransaction(); //do some work ... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.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();
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); String hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName"; // or String hqlUpdate = "update Customer set name = :newName where name = :oldName"; int updatedEntities = s.createQuery( hqlUpdate ) .setString( "newName", newName ) .setString( "oldName", oldName ) .executeUpdate(); tx.commit(); session.close();
private ParentWithCollection createParentWithNullChildren(String parentName) { Session s = openSession(); Transaction tx = s.beginTransaction(); ParentWithCollection parent = createParent( parentName ); s.save( parent ); tx.commit(); s.close(); return parent; }
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();
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); String hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName"; // or String hqlUpdate = "update Customer set name = :newName where name = :oldName"; int updatedEntities = s.createQuery( hqlUpdate ) .setString( "newName", newName ) .setString( "oldName", oldName ) .executeUpdate(); tx.commit(); session.close();
@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(); }
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();
Session session = factory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); ... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); }finally { session.close(); }
@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(); }