Package org.hibernate.test.propertyref.component.partial

Source Code of org.hibernate.test.propertyref.component.partial.PartialComponentPropertyRefTest

//$Id: PartialComponentPropertyRefTest.java 20731 2010-09-28 06:47:19Z stliu $
package org.hibernate.test.propertyref.component.partial;

import junit.framework.Test;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.junit.functional.FunctionalTestCase;
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;

/**
* @author Gavin King
*/
public class PartialComponentPropertyRefTest extends FunctionalTestCase {
 
  public PartialComponentPropertyRefTest(String name) {
    super( name );
  }

  public String[] getMappings() {
    return new String[] { "propertyref/component/partial/Mapping.hbm.xml" };
  }

  public static Test suite() {
    return new FunctionalTestClassTestSuite( PartialComponentPropertyRefTest.class );
  }
 
  public void testComponentPropertyRef() {
    Session s = openSession();
    s.beginTransaction();
    Person p = new Person();
    p.setIdentity( new Identity() );
    Account a = new Account();
    a.setNumber("123-12345-1236");
    a.setOwner(p);
    p.getIdentity().setName("Gavin");
    p.getIdentity().setSsn("123-12-1234");

    s.persist(p);
    s.persist(a);
    s.getTransaction().commit();
    s.close();
   
    s = openSession();
    s.beginTransaction();
   
    a = (Account) s.createQuery("from Account a left join fetch a.owner").uniqueResult();
    assertTrue( Hibernate.isInitialized( a.getOwner() ) );
    assertNotNull( a.getOwner() );
    assertEquals( "Gavin", a.getOwner().getIdentity().getName() );
    s.clear();
   
    a = (Account) s.get(Account.class, "123-12345-1236");
    assertFalse( Hibernate.isInitialized( a.getOwner() ) );
    assertNotNull( a.getOwner() );
    assertEquals( "Gavin", a.getOwner().getIdentity().getName() );
    assertTrue( Hibernate.isInitialized( a.getOwner() ) );
   
    s.clear();
   
    getSessions().evict(Account.class);
    getSessions().evict(Person.class);
   
    a = (Account) s.get(Account.class, "123-12345-1236");
    assertTrue( Hibernate.isInitialized( a.getOwner() ) );
    assertNotNull( a.getOwner() );
    assertEquals( "Gavin", a.getOwner().getIdentity().getName() );
    assertTrue( Hibernate.isInitialized( a.getOwner() ) );
   
    s.delete( a );
    s.delete( a.getOwner() );
    s.getTransaction().commit();
    s.close();
  }
}
TOP

Related Classes of org.hibernate.test.propertyref.component.partial.PartialComponentPropertyRefTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.