Skip to content

Commit a973834

Browse files
author
eugenp
committed
small cleanup work
1 parent 1e9eefc commit a973834

2 files changed

Lines changed: 140 additions & 150 deletions

File tree

‎spring-hibernate4/src/test/java/hibernate.cfg.xml‎

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,31 @@
77

88
<hibernate-configuration>
99
<session-factory>
10-
<!-- Database connection settings -->
11-
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12-
<property name="connection.url">jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true</property>
13-
<property name="connection.username">tutorialuser</property>
14-
<property name="connection.password">tutorialmy5ql</property>
15-
16-
<!-- JDBC connection pool (use the built-in) -->
17-
<property name="connection.pool_size">1</property>
18-
19-
<!-- SQL dialect -->
20-
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
21-
22-
<!-- Enable Hibernate's automatic session context management -->
23-
<property name="current_session_context_class">thread</property>
24-
25-
<!-- Disable the second-level cache -->
26-
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
27-
28-
<!-- Echo all executed SQL to stdout -->
29-
<property name="show_sql">true</property>
30-
31-
<!-- Drop and re-create the database schema on startup -->
32-
33-
<mapping resource="org//baeldung//persistence//model//Foo.hbm.xml"/>
34-
<mapping resource="org//baeldung//persistence//model//Bar.hbm.xml"/>
35-
</session-factory>
10+
<!-- Database connection settings -->
11+
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12+
<property name="connection.url">jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true</property>
13+
<property name="connection.username">tutorialuser</property>
14+
<property name="connection.password">tutorialmy5ql</property>
15+
16+
<!-- JDBC connection pool (use the built-in) -->
17+
<property name="connection.pool_size">1</property>
18+
19+
<!-- SQL dialect -->
20+
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
21+
22+
<!-- Enable Hibernate's automatic session context management -->
23+
<property name="current_session_context_class">thread</property>
24+
25+
<!-- Disable the second-level cache -->
26+
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
27+
28+
<!-- Echo all executed SQL to stdout -->
29+
<property name="show_sql">true</property>
30+
31+
<!-- Drop and re-create the database schema on startup -->
32+
33+
<mapping resource="org//baeldung//persistence//model//Foo.hbm.xml" />
34+
<mapping resource="org//baeldung//persistence//model//Bar.hbm.xml" />
35+
</session-factory>
3636

3737
</hibernate-configuration>
Lines changed: 114 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package org.baeldung.persistence.hibernate;
22

3+
import static org.junit.Assert.assertNull;
4+
35
import java.util.List;
46
import java.util.Set;
57

68
import org.baeldung.persistence.model.Bar;
79
import org.baeldung.persistence.model.Foo;
810
import org.baeldung.spring.PersistenceConfig;
11+
import org.hibernate.Criteria;
12+
import org.hibernate.NullPrecedence;
913
import org.hibernate.Query;
1014
import org.hibernate.Session;
1115
import org.hibernate.SessionFactory;
1216
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
1317
import org.hibernate.cfg.Configuration;
18+
import org.hibernate.criterion.Order;
1419
import org.junit.After;
1520
import org.junit.Before;
1621
import org.junit.Test;
@@ -21,12 +26,13 @@
2126

2227
@RunWith(SpringJUnit4ClassRunner.class)
2328
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
29+
@SuppressWarnings("unchecked")
2430
public class FooSortingPersistenceServiceTest {
2531
private SessionFactory sf;
2632
private Session sess;
2733

2834
@Before
29-
public void before() {
35+
public final void before() {
3036
final Configuration configuration = new Configuration().configure();
3137
final StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
3238
sf = configuration.buildSessionFactory(builder.build());
@@ -36,129 +42,115 @@ public void before() {
3642

3743
@After
3844
public void after() {
39-
//
45+
sess.getTransaction().commit();
46+
}
47+
48+
@Test
49+
public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
50+
final String hql = "FROM Foo f ORDER BY f.name";
51+
final Query query = sess.createQuery(hql);
52+
final List<Foo> fooList = query.list();
53+
for (final Foo foo : fooList) {
54+
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
55+
}
56+
}
57+
58+
@Test
59+
public final void whenHQlSortingByStringNullLast_thenLastNull() {
60+
final String hql = "FROM Foo f ORDER BY f.name NULLS LAST";
61+
final Query query = sess.createQuery(hql);
62+
final List<Foo> fooList = query.list();
63+
64+
assertNull(fooList.get(fooList.toArray().length - 1).getName());
65+
for (final Foo foo : fooList) {
66+
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
67+
}
4068
}
4169

42-
/* @Test
43-
public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
44-
final String hql = "FROM Foo f ORDER BY f.name";
45-
final Query query = sess.createQuery(hql);
46-
final List<Foo> fooList = query.list();
47-
for (final Foo foo : fooList) {
48-
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
49-
}
50-
sess.getTransaction().commit();
51-
}
52-
53-
@Test
54-
public final void whenHQlSortingByStringNullLast_thenLastNull() {
55-
final String hql = "FROM Foo f ORDER BY f.name NULLS LAST";
56-
final Query query = sess.createQuery(hql);
57-
final List<Foo> fooList = query.list();
58-
assertNull(fooList.get(fooList.toArray().length - 1).getName());
59-
for (final Foo foo : fooList) {
60-
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
61-
}
62-
sess.getTransaction().commit();
63-
}
64-
65-
@Test
66-
public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
67-
final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST";
68-
final Query query = sess.createQuery(hql);
69-
final List<Foo> fooList = query.list();
70-
assertNull(fooList.get(0).getName());
71-
for (final Foo foo : fooList) {
72-
System.out.println("Name:" + foo.getName());
73-
74-
}
75-
sess.getTransaction().commit();
76-
}
77-
78-
@Test
79-
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
80-
final String hql = "FROM Foo f ORDER BY f.name ASC";
81-
final Query query = sess.createQuery(hql);
82-
final List<Foo> fooList = query.list();
83-
for (final Foo foo : fooList) {
84-
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
85-
86-
);
87-
}
88-
sess.getTransaction().commit();
89-
}
90-
91-
@Test
92-
public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
93-
final String hql = "FROM Foo f ORDER BY f.name, f.id";
94-
final Query query = sess.createQuery(hql);
95-
final List<Foo> fooList = query.list();
96-
for (final Foo foo : fooList) {
97-
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
98-
99-
);
100-
}
101-
sess.getTransaction().commit();
102-
}
103-
104-
@Test
105-
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() {
106-
final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC";
107-
final Query query = sess.createQuery(hql);
108-
final List<Foo> fooList = query.list();
109-
for (final Foo foo : fooList) {
110-
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
111-
}
112-
sess.getTransaction().commit();
113-
}
114-
115-
@Test
116-
public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
117-
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
118-
criteria.addOrder(Order.asc("id"));
119-
final List<Foo> fooList = criteria.list();
120-
for (final Foo foo : fooList) {
121-
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
122-
}
123-
sess.getTransaction().commit();
124-
}
125-
126-
@Test
127-
public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
128-
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
129-
criteria.addOrder(Order.asc("name"));
130-
criteria.addOrder(Order.asc("id"));
131-
final List<Foo> fooList = criteria.list();
132-
for (final Foo foo : fooList) {
133-
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
134-
}
135-
sess.getTransaction().commit();
136-
}
137-
138-
@Test
139-
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
140-
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
141-
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
142-
final List<Foo> fooList = criteria.list();
143-
assertNull(fooList.get(fooList.toArray().length - 1).getName());
144-
for (final Foo foo : fooList) {
145-
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
146-
}
147-
sess.getTransaction().commit();
148-
}
149-
150-
@Test
151-
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
152-
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
153-
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
154-
final List<Foo> fooList = criteria.list();
155-
assertNull(fooList.get(0).getName());
156-
for (final Foo foo : fooList) {
157-
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
158-
159-
}
160-
sess.getTransaction().commit();
161-
}*/
70+
@Test
71+
public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
72+
final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST";
73+
final Query query = sess.createQuery(hql);
74+
final List<Foo> fooList = query.list();
75+
assertNull(fooList.get(0).getName());
76+
for (final Foo foo : fooList) {
77+
System.out.println("Name:" + foo.getName());
78+
79+
}
80+
}
81+
82+
@Test
83+
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
84+
final String hql = "FROM Foo f ORDER BY f.name ASC";
85+
final Query query = sess.createQuery(hql);
86+
final List<Foo> fooList = query.list();
87+
for (final Foo foo : fooList) {
88+
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
89+
}
90+
}
91+
92+
@Test
93+
public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
94+
final String hql = "FROM Foo f ORDER BY f.name, f.id";
95+
final Query query = sess.createQuery(hql);
96+
final List<Foo> fooList = query.list();
97+
for (final Foo foo : fooList) {
98+
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
99+
}
100+
}
101+
102+
@Test
103+
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() {
104+
final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC";
105+
final Query query = sess.createQuery(hql);
106+
final List<Foo> fooList = query.list();
107+
for (final Foo foo : fooList) {
108+
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
109+
}
110+
}
111+
112+
@Test
113+
public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
114+
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
115+
criteria.addOrder(Order.asc("id"));
116+
final List<Foo> fooList = criteria.list();
117+
for (final Foo foo : fooList) {
118+
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
119+
}
120+
}
121+
122+
@Test
123+
public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
124+
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
125+
criteria.addOrder(Order.asc("name"));
126+
criteria.addOrder(Order.asc("id"));
127+
final List<Foo> fooList = criteria.list();
128+
for (final Foo foo : fooList) {
129+
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
130+
}
131+
}
132+
133+
@Test
134+
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
135+
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
136+
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
137+
final List<Foo> fooList = criteria.list();
138+
assertNull(fooList.get(fooList.toArray().length - 1).getName());
139+
for (final Foo foo : fooList) {
140+
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
141+
}
142+
}
143+
144+
@Test
145+
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
146+
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
147+
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
148+
final List<Foo> fooList = criteria.list();
149+
assertNull(fooList.get(0).getName());
150+
for (final Foo foo : fooList) {
151+
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
152+
}
153+
}
162154

163155
@Test
164156
public final void whenSortingBars_thenBarsWithSortedFoos() {
@@ -170,10 +162,8 @@ public final void whenSortingBars_thenBarsWithSortedFoos() {
170162
System.out.println("Bar Id:" + bar.getId());
171163
for (final Foo foo : fooSet) {
172164
System.out.println("FooName:" + foo.getName());
173-
174165
}
175166
}
176-
sess.getTransaction().commit();
177167
}
178168

179169
}

0 commit comments

Comments
 (0)