alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (TestLazyPropertyCustomTypeExecutable.java)

This example Hibernate source code file (TestLazyPropertyCustomTypeExecutable.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

abstractexecutable, exception, exception, iterator, problematic, problematic, session, session, string, string, testlazypropertycustomtypeexecutable, util

The Hibernate TestLazyPropertyCustomTypeExecutable.java source code

package org.hibernate.test.instrument.cases;
import java.util.Iterator;
import junit.framework.Assert;
import org.hibernate.Session;
import org.hibernate.bytecode.instrumentation.internal.FieldInterceptionHelper;

import org.hibernate.test.instrument.domain.Problematic;

/**
 * {@inheritDoc}
 *
 * @author Steve Ebersole
 */
public class TestLazyPropertyCustomTypeExecutable extends AbstractExecutable {

	protected String[] getResources() {
		return new String[] { "org/hibernate/test/instrument/domain/Problematic.hbm.xml" };
	}

	public void execute() throws Exception {
		Session s = getFactory().openSession();
		Problematic p = new Problematic();
		try {
			s.beginTransaction();
			p.setName( "whatever" );
			p.setBytes( new byte[] { 1, 0, 1, 1, 0 } );
			s.save( p );
			s.getTransaction().commit();
		} catch (Exception e) {
			s.getTransaction().rollback();
			throw e;
		} finally {
			s.close();
		}

		// this access should be ok because p1 is not a lazy proxy 
		s = getFactory().openSession();
		try {
			s.beginTransaction();
			Problematic p1 = (Problematic) s.get( Problematic.class, p.getId() );
			Assert.assertTrue( FieldInterceptionHelper.isInstrumented( p1 ) );
			p1.getRepresentation();
			s.getTransaction().commit();
		} catch (Exception e) {
			s.getTransaction().rollback();
			throw e;
		} finally {
			s.close();
		}
		
		s = getFactory().openSession();
		try {
			s.beginTransaction();
			Problematic p1 = (Problematic) s.createQuery( "from Problematic" ).setReadOnly(true ).list().get( 0 );
			p1.getRepresentation();
			s.getTransaction().commit();
		} catch (Exception e) {
			s.getTransaction().rollback();
			throw e;
		} finally {
			s.close();
		}
		
		s = getFactory().openSession();
		try {
			s.beginTransaction();
			Problematic p1 = (Problematic) s.load( Problematic.class, p.getId() );
			Assert.assertFalse( FieldInterceptionHelper.isInstrumented( p1 ) );
			p1.setRepresentation( p.getRepresentation() );
			s.getTransaction().commit();
		} catch (Exception e) {
			s.getTransaction().rollback();
			throw e;
		} finally {
			s.close();
		}
	}

	protected void cleanup() {
		Session s = getFactory().openSession();
		s.beginTransaction();
		Iterator itr = s.createQuery( "from Problematic" ).list().iterator();
		while ( itr.hasNext() ) {
			Problematic p = (Problematic) itr.next();
			s.delete( p );
		}
		s.getTransaction().commit();
		s.close();
	}
}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate TestLazyPropertyCustomTypeExecutable.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.