11package com .baeldung .hibernate .proxy ;
22
33import org .hibernate .*;
4+ import org .hibernate .proxy .HibernateProxy ;
45import org .junit .After ;
56import org .junit .Before ;
67import org .junit .Test ;
78
89import static org .junit .Assert .*;
910
1011import java .io .IOException ;
11- import java .util .List ;
1212
1313import static org .junit .Assert .fail ;
1414
1515public class HibernateProxyUnitTest {
1616
17+ private SessionFactory factory ;
18+
1719 private Session session ;
1820
21+ private Company workplace ;
22+
23+ private Employee albert ;
24+
25+ private Employee bob ;
26+
27+ private Employee charlotte ;
28+
1929 @ Before
2030 public void init (){
2131 try {
22- session = HibernateUtil .getSessionFactory ("hibernate.properties" )
23- .openSession ();
32+ factory = HibernateUtil .getSessionFactory ("hibernate.properties" );
33+ session = factory .openSession ();
2434 } catch (HibernateException | IOException e ) {
2535 fail ("Failed to initiate Hibernate Session [Exception:" + e .toString () + "]" );
2636 }
27-
28- Boss boss = new Boss ("Eduard" , "Freud" );
29- session .save (boss );
3037 }
3138
3239 @ After
@@ -36,40 +43,68 @@ public void close(){
3643 }
3744 }
3845
39- @ Test ( expected = NullPointerException . class )
46+ @ Test
4047 public void givenAnInexistentEmployeeId_whenUseGetMethod_thenReturnNull () {
41- Employee employee = session .get (Employee .class , new Long (14 ));
48+ Employee employee = session .get (Employee .class , 14L );
49+ assertNull (employee );
50+ }
51+
52+ @ Test (expected = ObjectNotFoundException .class )
53+ public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenThrowObjectNotFoundException () {
54+ Employee employee = session .load (Employee .class , 999L );
4255 assertNull (employee );
4356 employee .getId ();
4457 }
4558
4659 @ Test
4760 public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy () {
48- Employee employee = session .load (Employee .class , new Long ( 14 ) );
61+ Employee employee = session .load (Employee .class , 14L );
4962 assertNotNull (employee );
63+ assertTrue (employee instanceof HibernateProxy );
5064 }
5165
5266 @ Test
53- public void givenABatchEmployeeList_whenSaveOne_thenSaveTheWholeBatch () {
54- Transaction transaction = session .beginTransaction ();
67+ public void givenThreeEmployees_whenLoadThemWithBatch_thenReturnAllOfThemWithOneQuery () {
68+ Transaction tx = session .beginTransaction ();
5569
56- for (long i = 1 ; i <= 5 ; i ++) {
57- Employee employee = new Employee ();
58- employee .setName ("Employee " + i );
59- session .save (employee );
60- }
70+ //We are saving 3 entities with one flush
71+
72+ this .workplace = new Company ("Bizco" );
73+ session .save (workplace );
74+
75+ this .albert = new Employee (workplace , "Albert" );
76+ session .save (albert );
77+
78+ this .bob = new Employee (workplace , "Bob" );
79+ session .save (bob );
80+
81+ this .charlotte = new Employee (workplace , "Charlotte" );
82+ session .save (charlotte );
6183
62- //After this line is possible to see all the insertions in the logs
6384 session .flush ();
6485 session .clear ();
65- transaction .commit ();
6686
67- transaction = session .beginTransaction ();
87+ tx .commit ();
88+ session = factory .openSession ();
89+
90+ Employee proxyAlbert = session .load (Employee .class , this .albert .getId ());
91+ assertNotNull (proxyAlbert );
92+ assertTrue (proxyAlbert instanceof HibernateProxy );
93+
94+ Employee proxyBob = session .load (Employee .class , this .bob .getId ());
95+ assertNotNull (proxyBob );
96+ assertTrue (proxyBob instanceof HibernateProxy );
97+
98+ Employee proxyCharlotte = session .load (Employee .class , this .charlotte .getId ());
99+ assertNotNull (proxyCharlotte );
100+ assertTrue (proxyCharlotte instanceof HibernateProxy );
68101
69- List <Employee > employeeList = session .createQuery ("from Employee" )
70- .setCacheMode (CacheMode .IGNORE ).getResultList ();
102+ //Fetching from database 3 entities with one call
103+ //Select from log: where employee0_.id in (?, ?, ?)
104+ proxyAlbert .getFirstName ();
71105
72- assertEquals (employeeList .size (), 5 );
73- transaction .commit ();
106+ assertEquals (proxyAlbert , this .albert );
107+ assertEquals (proxyBob , this .bob );
108+ assertEquals (proxyCharlotte , this .charlotte );
74109 }
75110}
0 commit comments