5
5
import java .util .List ;
6
6
import java .util .Set ;
7
7
8
- import javax .imageio .spi .ServiceRegistry ;
9
-
10
8
import org .baeldung .persistence .model .Bar ;
11
9
import org .baeldung .persistence .model .Foo ;
12
10
import org .baeldung .spring .PersistenceConfig ;
15
13
import org .hibernate .Query ;
16
14
import org .hibernate .Session ;
17
15
import org .hibernate .SessionFactory ;
18
- import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
19
- import org .hibernate .cfg .AvailableSettings ;
20
- import org .hibernate .cfg .Configuration ;
21
16
import org .hibernate .criterion .Order ;
22
17
import org .junit .After ;
23
18
import org .junit .Before ;
24
19
import org .junit .Test ;
25
20
import org .junit .runner .RunWith ;
21
+ import org .springframework .beans .factory .annotation .Autowired ;
26
22
import org .springframework .test .context .ContextConfiguration ;
27
23
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
28
24
import org .springframework .test .context .support .AnnotationConfigContextLoader ;
31
27
@ ContextConfiguration (classes = { PersistenceConfig .class }, loader = AnnotationConfigContextLoader .class )
32
28
@ SuppressWarnings ("unchecked" )
33
29
public 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 ;
39
35
40
36
@ Before
41
37
public void before () {
38
+ session = sessionFactory .openSession ();
39
+
40
+ session .beginTransaction ();
42
41
43
- final FooSortingPersistenceServiceData fooData = new FooSortingPersistenceServiceData ( );
42
+ final FooFixtures fooData = new FooFixtures ( sessionFactory );
44
43
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 ();
57
44
}
58
45
59
46
@ After
60
47
public void after () {
61
- sess .getTransaction ().commit ();
48
+ session .getTransaction ().commit ();
49
+ session .close ();
62
50
}
63
51
64
52
@ Test
65
53
public final void whenHQlSortingByOneAttribute_thenPrintSortedResults () {
66
54
final String hql = "FROM Foo f ORDER BY f.name" ;
67
- final Query query = sess .createQuery (hql );
55
+ final Query query = session .createQuery (hql );
68
56
final List <Foo > fooList = query .list ();
69
57
for (final Foo foo : fooList ) {
70
58
System .out .println ("Name: " + foo .getName () + ", Id: " + foo .getId ());
@@ -74,7 +62,7 @@ public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
74
62
@ Test
75
63
public final void whenHQlSortingByStringNullLast_thenLastNull () {
76
64
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 );
78
66
final List <Foo > fooList = query .list ();
79
67
80
68
assertNull (fooList .get (fooList .toArray ().length - 1 ).getName ());
@@ -86,7 +74,7 @@ public final void whenHQlSortingByStringNullLast_thenLastNull() {
86
74
@ Test
87
75
public final void whenSortingByStringNullsFirst_thenReturnNullsFirst () {
88
76
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 );
90
78
final List <Foo > fooList = query .list ();
91
79
assertNull (fooList .get (0 ).getName ());
92
80
for (final Foo foo : fooList ) {
@@ -98,7 +86,7 @@ public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
98
86
@ Test
99
87
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults () {
100
88
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 );
102
90
final List <Foo > fooList = query .list ();
103
91
for (final Foo foo : fooList ) {
104
92
System .out .println ("Name: " + foo .getName () + ", Id: " + foo .getId ());
@@ -108,7 +96,7 @@ public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSorted
108
96
@ Test
109
97
public final void whenHQlSortingByMultipleAttributes_thenSortedResults () {
110
98
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 );
112
100
final List <Foo > fooList = query .list ();
113
101
for (final Foo foo : fooList ) {
114
102
System .out .println ("Name: " + foo .getName () + ", Id: " + foo .getId ());
@@ -118,7 +106,7 @@ public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
118
106
@ Test
119
107
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults () {
120
108
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 );
122
110
final List <Foo > fooList = query .list ();
123
111
for (final Foo foo : fooList ) {
124
112
System .out .println ("Name: " + foo .getName () + ", Id: " + foo .getId ());
@@ -127,7 +115,7 @@ public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrint
127
115
128
116
@ Test
129
117
public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults () {
130
- final Criteria criteria = sess .createCriteria (Foo .class , "FOO" );
118
+ final Criteria criteria = session .createCriteria (Foo .class , "FOO" );
131
119
criteria .addOrder (Order .asc ("id" ));
132
120
final List <Foo > fooList = criteria .list ();
133
121
for (final Foo foo : fooList ) {
@@ -137,7 +125,7 @@ public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
137
125
138
126
@ Test
139
127
public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults () {
140
- final Criteria criteria = sess .createCriteria (Foo .class , "FOO" );
128
+ final Criteria criteria = session .createCriteria (Foo .class , "FOO" );
141
129
criteria .addOrder (Order .asc ("name" ));
142
130
criteria .addOrder (Order .asc ("id" ));
143
131
final List <Foo > fooList = criteria .list ();
@@ -148,7 +136,7 @@ public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
148
136
149
137
@ Test
150
138
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast () {
151
- final Criteria criteria = sess .createCriteria (Foo .class , "FOO" );
139
+ final Criteria criteria = session .createCriteria (Foo .class , "FOO" );
152
140
criteria .addOrder (Order .asc ("name" ).nulls (NullPrecedence .LAST ));
153
141
final List <Foo > fooList = criteria .list ();
154
142
assertNull (fooList .get (fooList .toArray ().length - 1 ).getName ());
@@ -159,7 +147,7 @@ public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
159
147
160
148
@ Test
161
149
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst () {
162
- final Criteria criteria = sess .createCriteria (Foo .class , "FOO" );
150
+ final Criteria criteria = session .createCriteria (Foo .class , "FOO" );
163
151
criteria .addOrder (Order .desc ("name" ).nulls (NullPrecedence .FIRST ));
164
152
final List <Foo > fooList = criteria .list ();
165
153
assertNull (fooList .get (0 ).getName ());
@@ -171,7 +159,7 @@ public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
171
159
@ Test
172
160
public final void whenSortingBars_thenBarsWithSortedFoos () {
173
161
final String hql = "FROM Bar b ORDER BY b.id" ;
174
- final Query query = sess .createQuery (hql );
162
+ final Query query = session .createQuery (hql );
175
163
final List <Bar > barList = query .list ();
176
164
for (final Bar bar : barList ) {
177
165
final Set <Foo > fooSet = bar .getFooSet ();
0 commit comments