Skip to content

Commit 942b186

Browse files
committed
Hibernate/JPA Sorting Repo Compiled in Eclipse
1 parent 08aa917 commit 942b186

15 files changed

Lines changed: 222 additions & 91 deletions

File tree

‎spring-hibernate4/.project‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
<arguments>
3131
</arguments>
3232
</buildCommand>
33+
<buildCommand>
34+
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
35+
<arguments>
36+
</arguments>
37+
</buildCommand>
3338
</buildSpec>
3439
<natures>
3540
<nature>org.springframework.ide.eclipse.core.springnature</nature>
@@ -39,5 +44,6 @@
3944
<nature>org.eclipse.m2e.core.maven2Nature</nature>
4045
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
4146
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
47+
<nature>org.hibernate.eclipse.console.hibernateNature</nature>
4248
</natures>
4349
</projectDescription>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
default.configuration=
2+
eclipse.preferences.version=1
3+
hibernate3.enabled=true
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3+
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4+
<!-- Generated 29-abr-2014 15:39:59 by Hibernate Tools 3.4.0.CR1 -->
5+
<hibernate-mapping>
6+
<class name="org.baeldung.persistence.model.Bar" table="BAR">
7+
<id name="id" type="int">
8+
<column name="ID" />
9+
<generator class="assigned" />
10+
</id>
11+
<set name="fooSet" table="FOO" inverse="false" lazy="true" order-by="NAME DESC">
12+
<key>
13+
<column name="BAR_ID" />
14+
</key>
15+
<one-to-many class="org.baeldung.persistence.model.Foo" />
16+
</set>
17+
<property name="name" type="java.lang.String">
18+
<column name="NAME" />
19+
</property>
20+
</class>
21+
</hibernate-mapping>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,24 @@ public Bar(final String name) {
3636
public Set<Foo> getFooSet() {
3737
return fooSet;
3838
}
39-
40-
public void setFooList(Set<Foo> fooSet) {
39+
40+
public void setFooSet(final Set<Foo> fooSet) {
4141
this.fooSet = fooSet;
4242
}
4343

4444
public int getId() {
4545
return this.id;
4646
}
4747

48-
public void setId(int id) {
48+
public void setId(final int id) {
4949
this.id = id;
5050
}
5151

5252
public String getName() {
5353
return this.name;
5454
}
5555

56-
public void setName(String name) {
56+
public void setName(final String name) {
5757
this.name = name;
5858
}
5959
//
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3+
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4+
<!-- Generated 23-abr-2014 18:00:30 by Hibernate Tools 3.4.0.CR1 -->
5+
<hibernate-mapping>
6+
<class name="org.baeldung.persistence.model.Foo" table="FOO">
7+
<id name="id" type="int">
8+
<column name="ID" />
9+
<generator class="native" />
10+
</id>
11+
<property name="name" type="java.lang.String">
12+
<column name="NAME" />
13+
</property>
14+
<many-to-one name="bar" class="org.baeldung.persistence.model.Bar" fetch="join" >
15+
<column name="BAR_ID" />
16+
</many-to-one>
17+
18+
19+
20+
</class>
21+
</hibernate-mapping>

‎spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,61 @@
22

33
import java.io.Serializable;
44

5-
import javax.persistence.Column;
65
import javax.persistence.Entity;
76
import javax.persistence.GeneratedValue;
87
import javax.persistence.GenerationType;
98
import javax.persistence.Id;
9+
import javax.persistence.JoinColumn;
10+
import javax.persistence.ManyToOne;
11+
12+
import org.hibernate.annotations.Fetch;
13+
import org.hibernate.annotations.FetchMode;
1014

1115
@Entity
1216
public class Foo implements Serializable {
1317

18+
private static final long serialVersionUID = 1L;
19+
20+
public Foo() {
21+
super();
22+
23+
}
1424
@Id
1525
@GeneratedValue(strategy = GenerationType.AUTO)
1626
private long id;
17-
18-
@Column(nullable = false)
1927
private String name;
28+
@ManyToOne(targetEntity = Bar.class)
29+
@JoinColumn(name = "BAR_ID")
30+
@Fetch(FetchMode.JOIN)
31+
private Bar bar = new Bar();
2032

21-
public Foo() {
22-
super();
33+
public Bar getBar() {
34+
return bar;
2335
}
2436

25-
public Foo(final String name) {
26-
super();
37+
public void setBar(final Bar bar) {
38+
this.bar = bar;
39+
}
2740

28-
this.name = name;
41+
public int getBar_Id() {
42+
return bar_Id;
43+
}
44+
45+
public void setBar_Id(final int bar_Id) {
46+
this.bar_Id = bar_Id;
2947
}
3048

31-
// API
49+
private int bar_Id;
3250

3351
public long getId() {
3452
return id;
3553
}
36-
3754
public void setId(final long id) {
3855
this.id = id;
3956
}
40-
4157
public String getName() {
4258
return name;
4359
}
44-
4560
public void setName(final String name) {
4661
this.name = name;
4762
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!DOCTYPE hibernate-configuration PUBLIC
4+
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
5+
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
6+
7+
8+
<hibernate-configuration>
9+
<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/HIBERTEST2_TEST</property>
13+
<property name="connection.username">root</property>
14+
<property name="connection.password"></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>
36+
37+
</hibernate-configuration>

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

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
package org.baeldung.persistence.service;
22

3-
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
3+
import static org.junit.Assert.assertNull;
44

5-
import org.baeldung.persistence.model.Foo;
6-
import org.baeldung.persistence.service.IFooService;
7-
import org.baeldung.spring.PersistenceConfig;
8-
import org.junit.Ignore;
9-
import org.junit.Test;
10-
import org.junit.runner.RunWith;
11-
import org.springframework.beans.factory.annotation.Autowired;
12-
import org.springframework.dao.DataAccessException;
13-
import org.springframework.dao.DataIntegrityViolationException;
14-
import org.springframework.dao.InvalidDataAccessApiUsageException;
15-
import org.springframework.test.context.ContextConfiguration;
16-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17-
import org.springframework.test.context.support.AnnotationConfigContextLoader;
18-
import static org.junit.Assert.*;
19-
import org.junit.BeforeClass;
20-
import org.junit.Test;
21-
import org.junit.After;
225
import java.util.List;
236
import java.util.Set;
7+
8+
import org.baeldung.persistence.model.Bar;
9+
import org.baeldung.persistence.model.Foo;
10+
import org.baeldung.spring.PersistenceConfig;
2411
import org.hibernate.Criteria;
2512
import org.hibernate.NullPrecedence;
2613
import org.hibernate.Query;
@@ -29,8 +16,14 @@
2916
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
3017
import org.hibernate.cfg.Configuration;
3118
import org.hibernate.criterion.Order;
32-
import com.cc.example.hibernate.Foo;
33-
import com.cc.example.hibernate.Bar;
19+
import org.junit.After;
20+
import org.junit.BeforeClass;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.springframework.test.context.ContextConfiguration;
24+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
25+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
26+
3427

3528
@RunWith(SpringJUnit4ClassRunner.class)
3629
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@@ -81,7 +74,6 @@ public final void whenHQlSortingByStringNullLast_thenLastNull() {
8174
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
8275
}
8376
sess.getTransaction().commit();
84-
8577
}
8678

8779
@Test
@@ -96,7 +88,6 @@ public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
9688

9789
}
9890
sess.getTransaction().commit();
99-
10091
}
10192

10293
@Test
@@ -108,10 +99,9 @@ public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSorted
10899
for (final Foo foo : fooList) {
109100
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
110101

111-
);
102+
);
112103
}
113104
sess.getTransaction().commit();
114-
115105
}
116106

117107
@Test
@@ -123,10 +113,9 @@ public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
123113
for (final Foo foo : fooList) {
124114
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
125115

126-
);
116+
);
127117
}
128118
sess.getTransaction().commit();
129-
130119
}
131120

132121
@Test
@@ -139,7 +128,6 @@ public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrint
139128
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
140129
}
141130
sess.getTransaction().commit();
142-
143131
}
144132

145133
@Test
@@ -152,7 +140,6 @@ public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
152140
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
153141
}
154142
sess.getTransaction().commit();
155-
156143
}
157144

158145
@Test
@@ -166,7 +153,6 @@ public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
166153
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
167154
}
168155
sess.getTransaction().commit();
169-
170156
}
171157

172158
@Test
@@ -178,10 +164,8 @@ public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
178164
assertNull(fooList.get(fooList.toArray().length - 1).getName());
179165
for (final Foo foo : fooList) {
180166
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
181-
182167
}
183168
sess.getTransaction().commit();
184-
185169
}
186170

187171
@Test
@@ -206,7 +190,7 @@ public final void whenSortingBars_thenBarsWithSortedFoos() {
206190
final Query query = sess.createQuery(hql);
207191
final List<Bar> barList = query.list();
208192
for (final Bar bar : barList) {
209-
final Set<Foo> fooSet = bar.getFooList();
193+
final Set<Foo> fooSet = bar.getFooSet();
210194
System.out.println("Bar Id:" + bar.getId());
211195
for (final Foo foo : fooSet) {
212196
System.out.println("FooName:" + foo.getName());

‎spring-jpa/.classpath‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@
2222
<attribute name="maven.pomderived" value="true"/>
2323
</attributes>
2424
</classpathentry>
25-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
26-
<attributes>
27-
<attribute name="maven.pomderived" value="true"/>
28-
</attributes>
29-
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
3026
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
3127
<attributes>
3228
<attribute name="maven.pomderived" value="true"/>

‎spring-jpa/.project‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
<arguments>
3131
</arguments>
3232
</buildCommand>
33+
<buildCommand>
34+
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
35+
<arguments>
36+
</arguments>
37+
</buildCommand>
3338
</buildSpec>
3439
<natures>
3540
<nature>org.springframework.ide.eclipse.core.springnature</nature>
@@ -39,5 +44,6 @@
3944
<nature>org.eclipse.m2e.core.maven2Nature</nature>
4045
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
4146
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
47+
<nature>org.hibernate.eclipse.console.hibernateNature</nature>
4248
</natures>
4349
</projectDescription>

0 commit comments

Comments
 (0)