FileDocCategorySizeDatePackage
IdentifierPropertyReferencesTest.javaAPI DocHibernate 3.2.56740Tue Dec 12 16:22:26 GMT 2006org.hibernate.test.idprops

IdentifierPropertyReferencesTest

public class IdentifierPropertyReferencesTest extends org.hibernate.junit.functional.FunctionalTestCase
{@inheritDoc}
author
Steve Ebersole

Fields Summary
Constructors Summary
public IdentifierPropertyReferencesTest(String name)

		super( name );
	
Methods Summary
private longextractCount(org.hibernate.Session s, java.lang.String hql)

		return extractCount( s.createQuery( hql ) );
	
private longextractCount(org.hibernate.Query query)

		return ( ( Long ) query.list().get( 0 ) ).longValue();
	
private longextractCount(org.hibernate.Criteria crit)

		return ( ( Integer ) crit.list().get( 0 ) ).intValue();
	
public java.lang.String[]getMappings()

		return new String[] { "idprops/Mapping.hbm.xml" };
	
public static junit.framework.Testsuite()

		return new FunctionalTestClassTestSuite( IdentifierPropertyReferencesTest.class );
	
public voidtestCriteriaIdPropertyReferences()

		Session s = openSession();
		s.beginTransaction();
		Person p = new Person( new Long(1), "steve", 123 );
		s.save( p );
		Order o = new Order( new Long(1), p );
		LineItem l = new LineItem( o, "my-product", 2 );
		l.setId( "456" );
		s.save( o );
		s.getTransaction().commit();
		s.close();

		s = openSession();
		s.beginTransaction();

		Criteria crit = s.createCriteria( Person.class );
		crit.setProjection( Projections.rowCount() );
		crit.add( Restrictions.eq( "id", new Integer(123) ) );
		long count = extractCount( crit );
		assertEquals( "Person by id prop (non-identifier)", 1, count );

		crit = s.createCriteria( Person.class );
		crit.setProjection( Projections.rowCount() );
		crit.add( Restrictions.eq( "pk", new Long(1) ) );
		count = extractCount( crit );
		assertEquals( "Person by pk prop (identifier)", 1, count );

		crit = s.createCriteria( Order.class );
		crit.setProjection( Projections.rowCount() );
		crit.add(  Restrictions.eq( "number", new Long(1) ) );
		count = extractCount( crit );
		assertEquals( "Order by number prop (named identifier)", 1, count );

		crit = s.createCriteria( Order.class );
		crit.setProjection( Projections.rowCount() );
		crit.add(  Restrictions.eq( "id", new Long(1) ) );
		count = extractCount( crit );
		assertEquals( "Order by id prop (virtual identifier)", 1, count );

		crit = s.createCriteria( LineItem.class );
		crit.setProjection( Projections.rowCount() );
		crit.add(  Restrictions.eq( "id", "456" ) );
		count = extractCount( crit );
		assertEquals( "LineItem by id prop (non-identifier", 1, count );

		if ( getDialect().supportsRowValueConstructorSyntax() ) {
			crit = s.createCriteria( LineItem.class );
			crit.setProjection( Projections.rowCount() );
			crit.add( Restrictions.eq( "pk", new LineItemPK( o, "my-product" ) ) );
			count = extractCount( crit );
			assertEquals( "LineItem by pk prop (named composite identifier)", 1, count );
		}

		crit = s.createCriteria( Order.class );
		crit.setProjection( Projections.rowCount() );
		crit.createAlias( "orderee", "p" ).add( Restrictions.eq( "p.id", new Integer(1) ) );
		count = extractCount( crit );
		assertEquals( 0, count );

		crit = s.createCriteria( Order.class );
		crit.setProjection( Projections.rowCount() );
		crit.createAlias( "orderee", "p" ).add( Restrictions.eq( "p.pk", new Long(1) ) );
		count = extractCount( crit );
		assertEquals( 1, count );

		crit = s.createCriteria( Order.class );
		crit.setProjection( Projections.rowCount() );
		crit.createAlias( "orderee", "p" ).add( Restrictions.eq( "p.id", new Integer(123) ) );
		count = extractCount( crit );
		assertEquals( 1, count );

		crit = s.createCriteria( LineItem.class );
		crit.setProjection( Projections.rowCount() );
		crit.add( Restrictions.eq( "pk.order.id", new Long(1) ) );
		count = extractCount( crit );
		assertEquals( 1, count );

		crit = s.createCriteria( LineItem.class );
		crit.setProjection( Projections.rowCount() );
		crit.add( Restrictions.eq( "pk.order.number", new Long(1) ) );
		count = extractCount( crit );
		assertEquals( 1, count );

		s.delete( o );
		s.delete( p );
		s.getTransaction().commit();
		s.close();
	
public voidtestHqlIdPropertyReferences()

		Session s = openSession();
		s.beginTransaction();
		Person p = new Person( new Long(1), "steve", 123 );
		s.save( p );
		Order o = new Order( new Long(1), p );
		LineItem l = new LineItem( o, "my-product", 2 );
		l.setId( "456" );
		s.save( o );
		s.getTransaction().commit();
		s.close();

		s = openSession();
		s.beginTransaction();

		long count = extractCount( s, "select count(*) from Person p where p.id = 123" );
		assertEquals( "Person by id prop (non-identifier)", 1, count );
		count = extractCount( s, "select count(*) from Person p where p.pk = 1" );
		assertEquals( "Person by pk prop (identifier)", 1, count );

		count = extractCount( s, "select count(*) from Order o where o.id = 1" );
		assertEquals( "Order by number prop (named identifier)", 1, count );
		count = extractCount( s, "select count(*) from Order o where o.number = 1" );
		assertEquals( "Order by id prop (virtual identifier)", 1, count );

		count = extractCount( s, "select count(*) from LineItem l where l.id = '456'" );
		assertEquals( "LineItem by id prop (non-identifier", 1, count );

		if ( getDialect().supportsRowValueConstructorSyntax() ) {
			Query q = s.createQuery( "select count(*) from LineItem l where l.pk = (:order, :product)" )
					.setEntity( "order", o )
					.setString( "product", "my-product" );
			count = extractCount( q );
			assertEquals( "LineItem by pk prop (named composite identifier", 1, count );
		}

		count = extractCount( s, "select count(*) from Order o where o.orderee.id = 1" );
		assertEquals( 0, count );
		count = extractCount( s, "select count(*) from Order o where o.orderee.pk = 1" );
		assertEquals( 1, count );
		count = extractCount( s, "select count(*) from Order o where o.orderee.id = 123" );
		assertEquals( 1, count );

		count = extractCount( s, "select count(*) from LineItem l where l.pk.order.id = 1" );
		assertEquals( 1, count );
		count = extractCount( s, "select count(*) from LineItem l where l.pk.order.number = 1" );
		assertEquals( 1, count );
		count = extractCount( s, "select count(*) from LineItem l where l.pk.order.orderee.pk = 1" );
		assertEquals( 1, count );

		s.delete( o );
		s.delete( p );
		s.getTransaction().commit();
		s.close();