/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2006-2011, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package org.hibernate.test.lazyonetoone; import java.util.Date; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.bytecode.instrumentation.internal.FieldInterceptionHelper; import org.junit.Test; import org.hibernate.testing.Skip; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; /** * @author Gavin King */ @Skip( condition = LazyOneToOneTest.DomainClassesInstrumentedMatcher.class, message = "Test domain classes were not instrumented" ) public class LazyOneToOneTest extends BaseCoreFunctionalTestCase { public String[] getMappings() { return new String[] { "lazyonetoone/Person.hbm.xml" }; } public void configure(Configuration cfg) { cfg.setProperty(Environment.MAX_FETCH_DEPTH, "2"); cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false"); } @Test public void testLazy() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Person p = new Person("Gavin"); Person p2 = new Person("Emmanuel"); Employee e = new Employee(p); new Employment(e, "JBoss"); Employment old = new Employment(e, "IFA"); old.setEndDate( new Date() ); s.persist(p); s.persist(p2); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); p = (Person) s.createQuery("from Person where name='Gavin'").uniqueResult(); //assertFalse( Hibernate.isPropertyInitialized(p, "employee") ); assertSame( p.getEmployee().getPerson(), p ); assertTrue( Hibernate.isInitialized( p.getEmployee().getEmployments() ) ); assertEquals( p.getEmployee().getEmployments().size(), 1 ); p2 = (Person) s.createQuery("from Person where name='Emmanuel'").uniqueResult(); assertNull( p2.getEmployee() ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); p = (Person) s.get(Person.class, "Gavin"); //assertFalse( Hibernate.isPropertyInitialized(p, "employee") ); assertSame( p.getEmployee().getPerson(), p ); assertTrue( Hibernate.isInitialized( p.getEmployee().getEmployments() ) ); assertEquals( p.getEmployee().getEmployments().size(), 1 ); p2 = (Person) s.get(Person.class, "Emmanuel"); assertNull( p2.getEmployee() ); s.delete(p2); s.delete(old); s.delete(p); t.commit(); s.close(); } public static class DomainClassesInstrumentedMatcher implements Skip.Matcher { @Override public boolean isMatch() { return FieldInterceptionHelper.isInstrumented( Person.class ); } } }