package Ġcom . github . de an 5 3 5 . permission . dao Ċ Ċ import Ġcom . github . de an 5 3 5 . permission . entity . Branch Ċ import Ġcom . github . de an 5 3 5 . permission . entity . User Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . hibernate . Session Ċ import Ġorg . hibernate . graph . Entity Graphs Ċ import Ġorg . hibernate . graph . Graph Parser Ċ import Ġorg . hibernate . stat . Statistics Ċ import Ġorg . junit . jupiter . api . BeforeEach Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . data . repository . findById OrNull Ċ import Ġjavax . persistence . EntityManager Ċ Ċ class ĠJ pa Tests Ġ: ĠAbstract Jpa Test () Ġ{ ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġlateinit Ġvar Ġuser Dao : ĠUser Dao ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġlateinit Ġvar Ġbranch Dao : ĠBranch Dao ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġlateinit Ġvar Ġentity Manager : ĠEntity Manager ĊĊĠĠĠ Ġlateinit Ġvar Ġstatistics : ĠStatistics ĊĊĠĠĠ Ġ@ BeforeEach ĊĠĠĠ Ġfun Ġsetup () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// given ĊĠĠĠĠĠĠĠ Ġval Ġparent Branch Ġ= ĠBranch ( name Ġ= Ġ" head quarter ") ĊĠĠĠĠĠĠĠ Ġbranch Dao . save ( parent Branch ) ĊĠĠĠĠĠĠĠ Ġval Ġbranch 1 Ġ= ĠBranch ( name Ġ= Ġ" Spring ĠFramework Ġ 4 . 0 Ġgoes ĠGA ") ĊĠĠĠĠĠĠĠ Ġval Ġbranch 2 Ġ= ĠBranch ( name Ġ= Ġ" Spring ĠFramework Ġ 5 . 0 Ġgoes ĠGA ") ĊĊĊĠĠĠĠĠĠĠ Ġval Ġuser 1 Ġ= ĠUser ( login Ġ= Ġ" name ", Ġaddress Ġ= Ġ" address ", Ġemail Ġ= Ġ" email ", Ġnotes Ġ= Ġ" notes ") ĊĠĠĠĠĠĠĠ Ġval Ġuser 2 Ġ= ĠUser ( login Ġ= Ġ" name 2 ", Ġaddress Ġ= Ġ" address 2 ", Ġemail Ġ= Ġ" email 2 ", Ġnotes Ġ= Ġ" notes 2 ") ĊĠĠĠĠĠĠĠ Ġuser Dao . save ( user 1 ) ĊĠĠĠĠĠĠĠ Ġuser Dao . save ( user 2 ) ĊĊĠĠĠĠĠĠĠ Ġbranch 1 . users . add ( user 1 ) ĊĠĠĠĠĠĠĠ Ġbranch 1 . users . add ( user 2 ) ĊĠĠĠĠĠĠĠ Ġbranch Dao . save ( parent Branch ) ĊĊĠĠĠĠĠĠĠ Ġbranch 1 . parent Ġ= Ġparent Branch ĊĠĠĠĠĠĠĠ Ġbranch 2 . parent Ġ= Ġparent Branch ĊĠĠĠĠĠĠĠ Ġbranch Dao . save ( branch 1 ) ĊĠĠĠĠĠĠĠ Ġbranch Dao . save ( branch 2 ) ĊĊĠĠĠĠĠĠĠ Ġval Ġsession Ġ= Ġentity Manager . unwrap ( Session :: class . java ) ĊĠĠĠĠĠĠĠ Ġstatistics Ġ= Ġsession . session Factory . statistics ĊĠĠĠĠĠĠĠ Ġsession . clear () ĊĠĠĠĠĠĠĠ Ġstatistics . is Statistics Enabled Ġ= Ġtrue ĊĠĠĠĠĠĠĠ Ġstatistics . clear () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` don 't Ġload Ġchildren Ġload Ġparent Ġbranch , Ġlazy Ġload `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġparent Branch Ġ= Ġbranch Dao . findById OrNull ( 1 L ) !! ĊĠĠĠĠĠĠĠ ĠassertThat ( parent Branch ). isNotNull ĊĠĠĠĠĠĠĠ ĠassertThat ( statistics . prepareStatement Count ). isEqualTo ( 1 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( statistics . entity Load Count ). isEqualTo ( 1 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` entity Ġgraph `() Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġwhen ĊĠĠĠĠĠĠĠ Ġval Ġgraph Ġ= Ġentity Manager . create Entity Graph ( Branch :: class . java ) ĊĠĠĠĠĠĠĠ Ġgraph . addAttribute Nodes (" children ") ĊĠĠĠĠĠĠĠ Ġval Ġparent Branch Ġ= Ġentity Manager . find ( Branch :: class . java , Ġ 1 L , Ġmap Of (" javax . persistence . fetch graph " Ġto Ġgraph )) ĊĊĠĠĠĠĠĠĠ Ġ// Ġthen ĊĠĠĠĠĠĠĠ ĠassertThat ( parent Branch . children . size ). isEqualTo ( 2 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( statistics . prepareStatement Count ). isEqualTo ( 1 ) ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` entity Ġgraph Ġfrom Ġstring `() Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġwhen ĊĠĠĠĠĠĠĠ Ġval Ġgraph Ġ= ĠGraph Parser . parse ( Branch :: class . java , Ġ" children ", Ġentity Manager ) ĊĠĠĠĠĠĠĠ Ġval Ġparent Branch Ġ= Ġentity Manager . find ( Branch :: class . java , Ġ 1 L , Ġmap Of (" javax . persistence . fetch graph " Ġto Ġgraph )) ĊĊĠĠĠĠĠĠĠ Ġ// Ġthen ĊĠĠĠĠĠĠĠ ĠassertThat ( parent Branch . children . size ). isEqualTo ( 2 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( statistics . prepareStatement Count ). isEqualTo ( 1 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( statistics . entity Load Count ). isEqualTo ( 3 ) ĊĊĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// Ġ@ Test ĊĠĠĠ Ġfun Ġ` entity Ġgraph Ġfrom Ġfields `() Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġwhen ĊĠĠĠĠĠĠĠ Ġval Ġgraphs Ġ= ĠlistOf ( Graph Parser . parse ( Branch :: class . java , Ġ" parent ", Ġentity Manager ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠGraph Parser . parse ( Branch :: class . java , Ġ" users ", Ġentity Manager )) ĊĠĠĠĠĠĠĠ Ġval Ġgraph Ġ= ĠEntity Graphs . merge ( entityManager , ĠBranch :: class . java , Ġ* graphs . to Typed Array ()) ĊĊĠĠĠĠĠĠĠ Ġval Ġparent Branch Ġ= Ġentity Manager . find ( Branch :: class . java , Ġ 1 L , Ġmap Of (" javax . persistence . fetch graph " Ġto Ġgraph )) ĊĊĠĠĠĠĠĠĠ Ġ// Ġthen ĊĠĠĠĠĠĠĠ ĠassertThat ( parent Branch . children . size ). isEqualTo ( 2 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( statistics . prepareStatement Count ). isEqualTo ( 1 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( statistics . entity Load Count ). isEqualTo ( 4 ) ĊĠĠĠ Ġ} Ċ } Ċ