Skip to content

Commit 79f00d9

Browse files
committed
Compiled in Eclipse and synched
2 parents 942b186 + a5c1d77 commit 79f00d9

3 files changed

Lines changed: 22 additions & 32 deletions

File tree

‎spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2525
import org.springframework.test.context.support.AnnotationConfigContextLoader;
2626

27-
2827
@RunWith(SpringJUnit4ClassRunner.class)
2928
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
3029

‎spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55

66
import javax.persistence.CascadeType;
7+
import javax.persistence.Column;
78
import javax.persistence.Entity;
89
import javax.persistence.FetchType;
910
import javax.persistence.GeneratedValue;
@@ -14,43 +15,40 @@
1415

1516
@Entity
1617
public class Bar implements Serializable {
17-
1818
private static final long serialVersionUID = 1L;
1919

20+
2021
@Id
2122
@GeneratedValue(strategy = GenerationType.AUTO)
22-
private int id;
23+
private long id;
24+
25+
@Column(nullable = false)
26+
private String name;
27+
2328

2429
@OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
2530
@OrderBy("name ASC")
2631
List<Foo> fooList;
2732

28-
private String name;
2933

30-
public Bar(){
34+
public Bar() {
3135
super();
3236
}
3337

34-
public Bar(final String name){
38+
public Bar(final String name) {
3539
super();
36-
this.name = name;
37-
}
38-
39-
//API
4040

41-
public List<Foo> getFooList() {
42-
return fooList;
41+
this.name = name;
4342
}
4443

45-
public void setFooList(final List<Foo> fooList) {
46-
this.fooList = fooList;
47-
}
44+
// API
4845

49-
public int getId() {
46+
public long getId() {
5047
return id;
5148
}
5249

53-
public void setId(final int id) {
50+
public void setId(final long id) {
51+
5452
this.id = id;
5553
}
5654

@@ -62,6 +60,14 @@ public void setName(final String name) {
6260
this.name = name;
6361
}
6462

63+
public List<Foo> getFooList() {
64+
return fooList;
65+
}
66+
67+
public void setFooList(final List<Foo> fooList) {
68+
this.fooList = fooList;
69+
}
70+
6571
//
6672

6773
@Override

‎spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public class FooServiceSortingTests {
2929

3030
@BeforeClass
3131
public static void before() {
32-
3332
emf = Persistence.createEntityManagerFactory("punit");
3433
entityManager = emf.createEntityManager();
3534
entityTransaction = entityManager.getTransaction();
@@ -39,43 +38,36 @@ public static void before() {
3938

4039
@Test
4140
public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() {
42-
4341
final String jql = "Select f from Foo as f order by f.id";
4442
final Query sortQuery = entityManager.createQuery(jql);
4543
final List<Foo> fooList = sortQuery.getResultList();
4644
for (final Foo foo : fooList) {
4745
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
4846
}
49-
5047
}
5148

5249
@Test
5350
public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() {
54-
5551
final String jql = "Select f from Foo as f order by f.id desc";
5652
final Query sortQuery = entityManager.createQuery(jql);
5753
final List<Foo> fooList = sortQuery.getResultList();
5854
for (final Foo foo : fooList) {
5955
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
6056
}
61-
6257
}
6358

6459
@Test
6560
public final void whenSortingByTwoAttributes_thenPrintSortedResult() {
66-
6761
final String jql = "Select f from Foo as f order by f.name asc, f.id desc";
6862
final Query sortQuery = entityManager.createQuery(jql);
6963
final List<Foo> fooList = sortQuery.getResultList();
7064
for (final Foo foo : fooList) {
7165
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
7266
}
73-
7467
}
7568

7669
@Test
7770
public final void whenSortingFooByBar_thenBarsSorted() {
78-
7971
final String jql = "Select f from Foo as f order by f.name, f.bar.id";
8072
final Query barJoinQuery = entityManager.createQuery(jql);
8173
final List<Foo> fooList = barJoinQuery.getResultList();
@@ -86,7 +78,6 @@ public final void whenSortingFooByBar_thenBarsSorted() {
8678

8779
@Test
8880
public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
89-
9081
final String jql = "Select b from Bar as b order by b.id";
9182
final Query barQuery = entityManager.createQuery(jql);
9283
final List<Bar> barList = barQuery.getResultList();
@@ -96,12 +87,10 @@ public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
9687
System.out.println("FooName:" + foo.getName());
9788
}
9889
}
99-
10090
}
10191

10292
@Test
10393
public final void whenSortingByStringNullLast_thenLastNull() {
104-
10594
final String jql = "Select f from Foo as f order by f.name desc NULLS LAST";
10695
final Query sortQuery = entityManager.createQuery(jql);
10796
final List<Foo> fooList = sortQuery.getResultList();
@@ -113,7 +102,6 @@ public final void whenSortingByStringNullLast_thenLastNull() {
113102

114103
@Test
115104
public final void whenSortingByStringNullFirst_thenFirstNull() {
116-
117105
final Foo nullNameFoo = new Foo();
118106
nullNameFoo.setName(null);
119107

@@ -137,7 +125,6 @@ public final void whenSortingByStringNullFirst_thenFirstNull() {
137125

138126
@Test
139127
public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
140-
141128
criteriaBuilder = entityManager.getCriteriaBuilder();
142129
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
143130
final Root<Foo> from = criteriaQuery.from(Foo.class);
@@ -148,12 +135,10 @@ public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
148135
for (final Foo foo : fooList) {
149136
System.out.println("Name:" + foo.getName() + "--------Id:" + foo.getId());
150137
}
151-
152138
}
153139

154140
@Test
155141
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() {
156-
157142
criteriaBuilder = entityManager.getCriteriaBuilder();
158143
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
159144
final Root<Foo> from = criteriaQuery.from(Foo.class);

0 commit comments

Comments
 (0)