Code example for Session

Methods: clearclosedeleteflushgetbeginTransactiongetTransaction

0
		mainSession.flush();
		mainSession.getTransaction().commit();
		mainSession.close();
 
		mainSession = openSession();
		mainSession.beginTransaction();
		doc = ( Document ) mainSession.get( entityName, doc.getId() );
 
		Session otherSession = openSession();
		otherSession.beginTransaction();
		Document otherDoc = ( Document ) otherSession.get( entityName, doc.getId() );
		otherDoc.setSummary( "my other summary" ); 
		otherSession.flush();
		otherSession.getTransaction().commit();
		otherSession.close();
 
		try { 
			mainSession.delete( doc );
			mainSession.flush();
			fail( "expecting opt lock failure" ); 
		} 
		catch ( StaleObjectStateException e ) {
			// expected 
		} 
		catch( StaleStateException expected ) {
			// expected result (if using versioned batching)... 
		} 
		catch( JDBCException e ) {
			// SQLServer will report this condition via a SQLException 
			// when using its SNAPSHOT transaction isolation... 
			if ( ! ( getDialect() instanceof SQLServerDialect && e.getErrorCode() == 3960 ) ) {
				throw e;
			} 
			else { 
				// it seems to "lose track" of the transaction as well... 
				mainSession.getTransaction().rollback();
				mainSession.beginTransaction();
			} 
		} 
		mainSession.clear();
		mainSession.getTransaction().commit();
		mainSession.close();
 
		mainSession = openSession();
		mainSession.beginTransaction();
		doc = ( Document ) mainSession.load( entityName, doc.getId() );
		mainSession.delete( entityName, doc );
Stop searching for code, let great code find you!  Add Codota to your java IDE