55import java .util .List ;
66import java .util .Set ;
77
8- import javax .imageio .spi .ServiceRegistry ;
9-
108import org .baeldung .persistence .model .Bar ;
119import org .baeldung .persistence .model .Foo ;
1210import org .baeldung .spring .PersistenceConfig ;
1513import org .hibernate .Query ;
1614import org .hibernate .Session ;
1715import org .hibernate .SessionFactory ;
18- import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
19- import org .hibernate .cfg .AvailableSettings ;
20- import org .hibernate .cfg .Configuration ;
2116import org .hibernate .criterion .Order ;
2217import org .junit .After ;
2318import org .junit .Before ;
2419import org .junit .Test ;
2520import org .junit .runner .RunWith ;
21+ import org .springframework .beans .factory .annotation .Autowired ;
2622import org .springframework .test .context .ContextConfiguration ;
2723import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
2824import org .springframework .test .context .support .AnnotationConfigContextLoader ;
3127@ ContextConfiguration (classes = { PersistenceConfig .class }, loader = AnnotationConfigContextLoader .class )
3228@ SuppressWarnings ("unchecked" )
3329public class FooSortingPersistenceServiceTest {
34- private SessionFactory sf ;
35- private Session sess ;
36- private static ServiceRegistry serviceRegistry ;
37- private static Configuration configuration ;
38- private static StandardServiceRegistryBuilder builder ;
30+
31+ @ Autowired
32+ private SessionFactory sessionFactory ;
33+
34+ private Session session ;
3935
4036 @ Before
4137 public void before () {
38+ session = sessionFactory .openSession ();
39+
40+ session .beginTransaction ();
4241
43- final FooSortingPersistenceServiceData fooData = new FooSortingPersistenceServiceData ( );
42+ final FooFixtures fooData = new FooFixtures ( sessionFactory );
4443 fooData .createBars ();
45- configuration = new Configuration ();
46- configuration .setProperty ("hibernate.dialect" , "org.hibernate.dialect.MySQLDialect" );
47- configuration .setProperty ("dialect" , "org.hibernate.dialect.MySQLDialect" );
48- configuration .setProperty (AvailableSettings .DRIVER , "com.mysql.jdbc.Driver" );
49- configuration .setProperty (AvailableSettings .URL , "jdbc:mysql://localhost:3306/HIBERTEST2_TEST" );
50- configuration .setProperty (AvailableSettings .USER , "root" );
51- configuration .setProperty (AvailableSettings .PASS , "" );
52- configuration .setProperty ("hibernate.show_sql" , "true" );
53- builder = new StandardServiceRegistryBuilder ().applySettings (configuration .getProperties ());
54- sf = configuration .addPackage ("org.baeldung.persistence.model" ).addAnnotatedClass (Foo .class ).addAnnotatedClass (Bar .class ).configure ().buildSessionFactory (builder .build ());
55- sess = sf .openSession ();
56- sess .beginTransaction ();
5744 }
5845
5946 @ After
6047 public void after () {
61- sess .getTransaction ().commit ();
48+ session .getTransaction ().commit ();
49+ session .close ();
6250 }
6351
6452 @ Test
6553 public final void whenHQlSortingByOneAttribute_thenPrintSortedResults () {
6654 final String hql = "FROM Foo f ORDER BY f.name" ;
67- final Query query = sess .createQuery (hql );
55+ final Query query = session .createQuery (hql );
6856 final List <Foo > fooList = query .list ();
6957 for (final Foo foo : fooList ) {
7058 System .out .println ("Name: " + foo .getName () + ", Id: " + foo .getId ());
@@ -74,7 +62,7 @@ public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
7462 @ Test
7563 public final void whenHQlSortingByStringNullLast_thenLastNull () {
7664 final String hql = "FROM Foo f ORDER BY f.name NULLS LAST" ;
77- final Query query = sess .createQuery (hql );
65+ final Query query = session .createQuery (hql );
7866 final List <Foo > fooList = query .list ();
7967
8068 assertNull (fooList .get (fooList .toArray ().length - 1 ).getName ());
@@ -86,7 +74,7 @@ public final void whenHQlSortingByStringNullLast_thenLastNull() {
8674 @ Test
8775 public final void whenSortingByStringNullsFirst_thenReturnNullsFirst () {
8876 final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST" ;
89- final Query query = sess .createQuery (hql );
77+ final Query query = session .createQuery (hql );
9078 final List <Foo > fooList = query .list ();
9179 assertNull (fooList .get (0 ).getName ());
9280 for (final Foo foo : fooList ) {
@@ -98,7 +86,7 @@ public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
9886 @ Test
9987 public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults () {
10088 final String hql = "FROM Foo f ORDER BY f.name ASC" ;
101- final Query query = sess .createQuery (hql );
89+ final Query query = session .createQuery (hql );
10290 final List <Foo > fooList = query .list ();
10391 for (final Foo foo : fooList ) {
10492 System .out .println ("Name: " + foo .getName () + ", Id: " + foo .getId ());
@@ -108,7 +96,7 @@ public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSorted
10896 @ Test
10997 public final void whenHQlSortingByMultipleAttributes_thenSortedResults () {
11098 final String hql = "FROM Foo f ORDER BY f.name, f.id" ;
111- final Query query = sess .createQuery (hql );
99+ final Query query = session .createQuery (hql );
112100 final List <Foo > fooList = query .list ();
113101 for (final Foo foo : fooList ) {
114102 System .out .println ("Name: " + foo .getName () + ", Id: " + foo .getId ());
@@ -118,7 +106,7 @@ public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
118106 @ Test
119107 public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults () {
120108 final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC" ;
121- final Query query = sess .createQuery (hql );
109+ final Query query = session .createQuery (hql );
122110 final List <Foo > fooList = query .list ();
123111 for (final Foo foo : fooList ) {
124112 System .out .println ("Name: " + foo .getName () + ", Id: " + foo .getId ());
@@ -127,7 +115,7 @@ public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrint
127115
128116 @ Test
129117 public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults () {
130- final Criteria criteria = sess .createCriteria (Foo .class , "FOO" );
118+ final Criteria criteria = session .createCriteria (Foo .class , "FOO" );
131119 criteria .addOrder (Order .asc ("id" ));
132120 final List <Foo > fooList = criteria .list ();
133121 for (final Foo foo : fooList ) {
@@ -137,7 +125,7 @@ public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
137125
138126 @ Test
139127 public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults () {
140- final Criteria criteria = sess .createCriteria (Foo .class , "FOO" );
128+ final Criteria criteria = session .createCriteria (Foo .class , "FOO" );
141129 criteria .addOrder (Order .asc ("name" ));
142130 criteria .addOrder (Order .asc ("id" ));
143131 final List <Foo > fooList = criteria .list ();
@@ -148,7 +136,7 @@ public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
148136
149137 @ Test
150138 public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast () {
151- final Criteria criteria = sess .createCriteria (Foo .class , "FOO" );
139+ final Criteria criteria = session .createCriteria (Foo .class , "FOO" );
152140 criteria .addOrder (Order .asc ("name" ).nulls (NullPrecedence .LAST ));
153141 final List <Foo > fooList = criteria .list ();
154142 assertNull (fooList .get (fooList .toArray ().length - 1 ).getName ());
@@ -159,7 +147,7 @@ public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
159147
160148 @ Test
161149 public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst () {
162- final Criteria criteria = sess .createCriteria (Foo .class , "FOO" );
150+ final Criteria criteria = session .createCriteria (Foo .class , "FOO" );
163151 criteria .addOrder (Order .desc ("name" ).nulls (NullPrecedence .FIRST ));
164152 final List <Foo > fooList = criteria .list ();
165153 assertNull (fooList .get (0 ).getName ());
@@ -171,7 +159,7 @@ public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
171159 @ Test
172160 public final void whenSortingBars_thenBarsWithSortedFoos () {
173161 final String hql = "FROM Bar b ORDER BY b.id" ;
174- final Query query = sess .createQuery (hql );
162+ final Query query = session .createQuery (hql );
175163 final List <Bar > barList = query .list ();
176164 for (final Bar bar : barList ) {
177165 final Set <Foo > fooSet = bar .getFooSet ();
0 commit comments