@@ -50,19 +50,65 @@ public void givenAnInexistentEmployeeId_whenUseGetMethod_thenReturnNull() {
5050 }
5151
5252 @ Test (expected = ObjectNotFoundException .class )
53- public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenThrowObjectNotFoundException () {
53+ public void givenAnNonExistentEmployeeId_whenUseLoadMethod_thenThrowObjectNotFoundException () {
5454 Employee employee = session .load (Employee .class , 999L );
55- assertNull (employee );
56- employee .getId ();
55+ assertNotNull (employee );
56+ employee .getFirstName ();
5757 }
5858
5959 @ Test
60- public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy () {
60+ public void givenAnNonExistentEmployeeId_whenUseLoadMethod_thenReturnAProxy () {
6161 Employee employee = session .load (Employee .class , 14L );
6262 assertNotNull (employee );
6363 assertTrue (employee instanceof HibernateProxy );
6464 }
6565
66+ @ Test
67+ public void givenAnEmployeeFromACompany_whenUseLoadMethod_thenCompanyIsAProxy () {
68+ Transaction tx = session .beginTransaction ();
69+
70+ this .workplace = new Company ("Bizco" );
71+ session .save (workplace );
72+
73+ this .albert = new Employee (workplace , "Albert" );
74+ session .save (albert );
75+
76+ session .flush ();
77+ session .clear ();
78+
79+ tx .commit ();
80+ this .session = factory .openSession ();
81+
82+ Employee proxyAlbert = session .load (Employee .class , albert .getId ());
83+ assertTrue (proxyAlbert instanceof HibernateProxy );
84+
85+ // with many-to-one lazy-loading, associations remain proxies
86+ assertTrue (proxyAlbert .getWorkplace () instanceof HibernateProxy );
87+ }
88+
89+ @ Test
90+ public void givenACompanyWithEmployees_whenUseLoadMethod_thenEmployeesAreProxies () {
91+ Transaction tx = session .beginTransaction ();
92+
93+ this .workplace = new Company ("Bizco" );
94+ session .save (workplace );
95+
96+ this .albert = new Employee (workplace , "Albert" );
97+ session .save (albert );
98+
99+ session .flush ();
100+ session .clear ();
101+
102+ tx .commit ();
103+ this .session = factory .openSession ();
104+
105+ Company proxyBizco = session .load (Company .class , workplace .getId ());
106+ assertTrue (proxyBizco instanceof HibernateProxy );
107+
108+ // with one-to-many, associations aren't proxies
109+ assertFalse (proxyBizco .getEmployees ().iterator ().next () instanceof HibernateProxy );
110+ }
111+
66112 @ Test
67113 public void givenThreeEmployees_whenLoadThemWithBatch_thenReturnAllOfThemWithOneQuery () {
68114 Transaction tx = session .beginTransaction ();
0 commit comments