Login
[x]
Log in using an account from:
Fedora Account System
Red Hat Associate
Red Hat Customer
Or login using a Red Hat Bugzilla account
Forgot Password
Login:
Hide Forgot
Create an Account
Red Hat Bugzilla – Attachment 924621 Details for
Bug 1124935
[GSS](6.4.0) 'LazyInitializationException: could not initialize proxy - no Session' during attribute access
[?]
New
Simple Search
Advanced Search
My Links
Browse
Requests
Reports
Current State
Search
Tabular reports
Graphical reports
Duplicates
Other Reports
User Changes
Plotly Reports
Bug Status
Bug Severity
Non-Defaults
|
Product Dashboard
Help
Page Help!
Bug Writing Guidelines
What's new
Browser Support Policy
5.0.4.rh83 Release notes
FAQ
Guides index
User guide
Web Services
Contact
Legal
This site requires JavaScript to be enabled to function correctly, please enable it.
Annotated test case showing expected behavior
LazyOneToOneTest.java (text/x-java-source), 13.78 KB, created by
Gail Badner
on 2014-08-06 21:42:11 UTC
(
hide
)
Description:
Annotated test case showing expected behavior
Filename:
MIME Type:
Creator:
Gail Badner
Created:
2014-08-06 21:42:11 UTC
Size:
13.78 KB
patch
obsolete
>/* > * Hibernate, Relational Persistence for Idiomatic Java > * > * Copyright (c) 2014, 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.onetoone.lazy; > >import java.io.Serializable; >import javax.persistence.Entity; >import javax.persistence.FetchType; >import javax.persistence.GeneratedValue; >import javax.persistence.GenerationType; >import javax.persistence.Id; >import javax.persistence.OneToOne; > >import org.junit.After; >import org.junit.Before; >import org.junit.Test; > >import org.hibernate.Hibernate; >import org.hibernate.LazyInitializationException; >import org.hibernate.Session; >import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; > >import static org.junit.Assert.assertFalse; >import static org.junit.Assert.assertTrue; >import static org.junit.Assert.fail; > >public class LazyOneToOneTest extends BaseCoreFunctionalTestCase { > private Long idTestClass1; > > @Before > public void createData() { > Session s = openSession(); > s.getTransaction().begin(); > > TestClass3 testClass3 = new TestClass3(); > testClass3.setName("test3"); > s.persist(testClass3); > > TestClass2 testClass2 = new TestClass2(); > testClass2.setTitle("test2"); > testClass2.setTestClass3(testClass3); > s.persist(testClass2); > > TestClass1 testClass1 = new TestClass1(); > testClass1.setTitle("test1"); > testClass1.setTestClass2(testClass2); > s.persist(testClass1); > s.getTransaction().commit(); > > idTestClass1 = testClass1.getId(); > } > > @After > public void cleanupData() { > Session s = openSession(); > s.getTransaction().begin(); > TestClass1 testClass1 = (TestClass1) s.get( TestClass1.class, idTestClass1 ); > s.delete( testClass1 ); > s.delete( testClass1.getTestClass2() ); > s.delete( testClass1.getTestClass2().getTestClass3() ); > s.getTransaction().commit(); > s.close(); > > idTestClass1 = null; > } > > @Test > public void testGetLazyOneToOneInitInclNested() { > Session s = openSession(); > s.getTransaction().begin(); > TestClass1 testClass1 = (TestClass1) s.get(TestClass1.class, idTestClass1 ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > Hibernate.initialize( testClass1.getTestClass2() ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2().getTestClass3() ) ); > Hibernate.initialize( testClass1.getTestClass2().getTestClass3() ); > s.getTransaction().commit(); > s.close(); > } > > @Test > public void testUpdateLazyUninitOneToOneInitInclNested() { > Session s = openSession(); > s.getTransaction().begin(); > TestClass1 testClass1 = (TestClass1) s.get(TestClass1.class, idTestClass1 ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > s.getTransaction().commit(); > s.close(); > > s = openSession(); > s.getTransaction().begin(); > // update testClass1 with an uninitialized lazy @OneToOne association (testClass1.testClass2). > // TestClass1.testClass2 does not have cascade configured. > s.update( testClass1 ); > // when an entity is updated, uninitialized to-one associations are reattached. > assertTrue( s.contains( testClass1.getTestClass2() ) ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > // now initialize it... > Hibernate.initialize( testClass1.getTestClass2() ); > // now the session will contain the nested uninitialized association. > assertTrue( s.contains( testClass1.getTestClass2().getTestClass3() ) ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2().getTestClass3() ) ); > // the nested association can be initialized because it is contained in the session. > Hibernate.initialize( testClass1.getTestClass2().getTestClass3() ); > s.getTransaction().commit(); > s.close(); > } > > @Test > public void testUpdateLazyUninitOneToOneInitThenUpdateInitNested() { > Session s = openSession(); > s.getTransaction().begin(); > TestClass1 testClass1 = (TestClass1) s.get(TestClass1.class, idTestClass1 ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > s.getTransaction().commit(); > s.close(); > > s = openSession(); > s.getTransaction().begin(); > // update testClass1 with an uninitialized lazy @OneToOne association (testClass1.testClass2). > // TestClass1.testClass2 does not have cascade configured. > s.update( testClass1 ); > // when an entity is updated, uninitialized to-one associations are reattached. > assertTrue( s.contains( testClass1.getTestClass2() ) ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > // now initialize it... > Hibernate.initialize( testClass1.getTestClass2() ); > // now the session will contain the nested uninitialized association. > assertTrue( s.contains( testClass1.getTestClass2().getTestClass3() ) ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2().getTestClass3() ) ); > s.getTransaction().commit(); > s.close(); > > s = openSession(); > s.getTransaction().begin(); > // testClass1.testClass2 is an initialized proxy; > // because there's no cascade on update and the proxy is initialized, proxy does not get reattached > s.update( testClass1 ); > assertFalse( s.contains( testClass1.getTestClass2() ) ); > assertTrue( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > // testClass1.testClass2.testClass3 is still not initialized and not in the session. > TestClass3 testClass3 = testClass1.getTestClass2().getTestClass3(); > assertFalse( Hibernate.isInitialized( testClass3 ) ); > assertFalse( s.contains( testClass3 ) ); > try { > Hibernate.initialize( testClass3 ); > fail( "should have thrown LazyInitializationException"); > } > catch (LazyInitializationException ex){ > // expected > } > finally { > s.getTransaction().rollback(); > } > s.close(); > > // repeat what was done in the previous block, but this time, call > // Session.update( testClass1.getTestClass2() ) > // before initializing testClass1.getTestClass2().getTestClass3(). > > s = openSession(); > s.getTransaction().begin(); > // testClass1.testClass2 is an initialized proxy; > // because there's no cascade on update and the proxy is initialized, proxy does not get reattached > s.update( testClass1 ); > assertFalse( s.contains( testClass1.getTestClass2() ) ); > assertTrue( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > // now update testClass1.testClass2; it will also add the nested uninitialized association > // to the session. > s.update( testClass1.getTestClass2() ); > assertTrue( s.contains( testClass1.getTestClass2() ) ); > testClass3 = testClass1.getTestClass2().getTestClass3(); > assertTrue( s.contains( testClass3 ) ); > assertFalse( Hibernate.isInitialized( testClass3 )); > // now that the session contains testClass3, it is safe to initialize. > Hibernate.initialize( testClass3 ); > s.getTransaction().commit(); > s.close(); > } > > @Test > public void testUpdateLazyInitOneToOneAndNested() { > Session s = openSession(); > s.getTransaction().begin(); > TestClass1 testClass1 = (TestClass1) s.get(TestClass1.class, idTestClass1 ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > Hibernate.initialize( testClass1.getTestClass2() ); > Hibernate.initialize( testClass1.getTestClass2().getTestClass3() ); > s.getTransaction().commit(); > s.close(); > > s = openSession(); > s.getTransaction().begin(); > s.update( testClass1 ); > assertTrue( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > assertTrue( Hibernate.isInitialized( testClass1.getTestClass2().getTestClass3() ) ); > s.getTransaction().commit(); > s.close(); > } > > @Test > public void testMergeLazyUninitOneToOneInitInclNested() { > Session s = openSession(); > s.getTransaction().begin(); > TestClass1 testClass1 = (TestClass1) s.get(TestClass1.class, idTestClass1 ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > s.getTransaction().commit(); > s.close(); > > s = openSession(); > s.getTransaction().begin(); > // merge testClass1 with an uninitialized lazy @OneToOne association (testClass1.testClass2). > // TestClass1.testClass2 does not have cascade configured. > testClass1 = (TestClass1) s.merge( testClass1 ); > // when an entity is merged, uninitialized to-one associations are reattached. > assertTrue( s.contains( testClass1.getTestClass2() ) ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > // now initialize it... > Hibernate.initialize( testClass1.getTestClass2() ); > // now the session will contain the nested uninitialized association. > assertTrue( s.contains( testClass1.getTestClass2().getTestClass3() ) ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2().getTestClass3() ) ); > // the nested association can be initialized because it is contained in the session. > Hibernate.initialize( testClass1.getTestClass2().getTestClass3() ); > s.getTransaction().commit(); > s.close(); > } > > @Test > public void testMergeLazyUninitOneToOneInitThenMergeInitNested() { > Session s = openSession(); > s.getTransaction().begin(); > TestClass1 testClass1 = (TestClass1) s.get(TestClass1.class, idTestClass1 ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > s.getTransaction().commit(); > s.close(); > > s = openSession(); > s.getTransaction().begin(); > // merge testClass1 with an uninitialized lazy @OneToOne association (testClass1.testClass2). > // TestClass1.testClass2 does not have cascade configured. > testClass1 = (TestClass1) s.merge( testClass1 ); > // when an entity is merged, uninitialized to-one associations are reattached. > assertTrue( s.contains( testClass1.getTestClass2() ) ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > // now initialize it... > Hibernate.initialize( testClass1.getTestClass2() ); > // now the session will contain the nested uninitialized association. > assertTrue( s.contains( testClass1.getTestClass2().getTestClass3() ) ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2().getTestClass3() ) ); > s.getTransaction().commit(); > s.close(); > > s = openSession(); > s.getTransaction().begin(); > // testClass1.testClass2 is an initialized proxy; > // because there's no cascade on merge, the initialized state from testClass1.getTestClass2() will be ignored; > testClass1 = (TestClass1) s.merge( testClass1 ); > // merge result will contain an uninitialized proxy for testClass1.testClass2. > assertTrue( s.contains( testClass1.getTestClass2() ) ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > // initialize testClass1.getTestClass2 > Hibernate.initialize( testClass1.getTestClass2() ); > // initialize testClass1.testClass2.testClass3 > Hibernate.initialize( testClass1.getTestClass2().getTestClass3() ); > s.getTransaction().commit(); > s.close(); > } > > @Test > public void testMergeLazyInitOneToOneAndNested() { > Session s = openSession(); > s.getTransaction().begin(); > TestClass1 testClass1 = (TestClass1) s.get(TestClass1.class, idTestClass1 ); > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > Hibernate.initialize( testClass1.getTestClass2() ); > Hibernate.initialize( testClass1.getTestClass2().getTestClass3() ); > s.getTransaction().commit(); > s.close(); > > s = openSession(); > s.getTransaction().begin(); > testClass1 = (TestClass1) s.merge( testClass1 ); > // merge is not cascaded, so testClass1.testClass2 will be uninitialized. > assertFalse( Hibernate.isInitialized( testClass1.getTestClass2() ) ); > s.getTransaction().commit(); > s.close(); > } > > @Override > protected Class[] getAnnotatedClasses() { > return new Class[] { > TestClass1.class, > TestClass2.class, > TestClass3.class > }; > } > > @Entity > public static class TestClass1 implements Serializable { > @Id > @GeneratedValue(strategy= GenerationType.AUTO) > private Long id; > > private String title; > > @OneToOne(fetch = FetchType.LAZY) > private TestClass2 testClass2; > > public TestClass1() {} > > public Long getId() { > return id; > } > > private void setId(Long id) { > this.id = id; > } > > public String getTitle() { > return title; > } > > public void setTitle(String title) { > this.title = title; > } > > public TestClass2 getTestClass2() { > return testClass2; > } > > public void setTestClass2(TestClass2 testClass2) { > this.testClass2 = testClass2; > } > } > > @Entity > public static class TestClass2 implements Serializable { > @Id > @GeneratedValue(strategy= GenerationType.AUTO) > private Long id; > > private String title; > > @OneToOne(fetch = FetchType.LAZY) > private TestClass3 testClass3; > > public TestClass2() {} > > public Long getId() { > return id; > } > > private void setId(Long id) { > this.id = id; > } > > public String getTitle() { > return title; > } > > public void setTitle(String title) { > this.title = title; > } > > public TestClass3 getTestClass3() { > return testClass3; > } > > public void setTestClass3(TestClass3 testClass3) { > this.testClass3 = testClass3; > } > } > > @Entity > public static class TestClass3 implements Serializable { > @Id > @GeneratedValue(strategy= GenerationType.AUTO) > private Long id; > > private String name; > > public TestClass3() {} > > public Long getId() { > return id; > } > > private void setId(Long id) { > this.id = id; > } > > public String getName() { > return name; > } > > public void setName(String title) { > this.name = title; > } > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 1124935
:
922644
| 924621