Skip to content

Commit 9f0d14b

Browse files
author
Eugen
committed
Merge pull request eugenp#8 from egmp777/master
Hibernate/JPA Tests Compiled in Eclipse and Synched
2 parents a5c1d77 + 79f00d9 commit 9f0d14b

15 files changed

Lines changed: 177 additions & 43 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: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public final void whenHQlSortingByStringNullLast_thenLastNull() {
7373
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
7474
}
7575
sess.getTransaction().commit();
76-
7776
}
7877

7978
@Test
@@ -88,7 +87,6 @@ public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
8887

8988
}
9089
sess.getTransaction().commit();
91-
9290
}
9391

9492
@Test
@@ -100,10 +98,9 @@ public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSorted
10098
for (final Foo foo : fooList) {
10199
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
102100

103-
);
101+
);
104102
}
105103
sess.getTransaction().commit();
106-
107104
}
108105

109106
@Test
@@ -115,10 +112,9 @@ public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
115112
for (final Foo foo : fooList) {
116113
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
117114

118-
);
115+
);
119116
}
120117
sess.getTransaction().commit();
121-
122118
}
123119

124120
@Test
@@ -131,7 +127,6 @@ public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrint
131127
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
132128
}
133129
sess.getTransaction().commit();
134-
135130
}
136131

137132
@Test
@@ -144,7 +139,6 @@ public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
144139
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
145140
}
146141
sess.getTransaction().commit();
147-
148142
}
149143

150144
@Test
@@ -158,7 +152,6 @@ public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
158152
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
159153
}
160154
sess.getTransaction().commit();
161-
162155
}
163156

164157
@Test
@@ -170,10 +163,8 @@ public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
170163
assertNull(fooList.get(fooList.toArray().length - 1).getName());
171164
for (final Foo foo : fooList) {
172165
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
173-
174166
}
175167
sess.getTransaction().commit();
176-
177168
}
178169

179170
@Test
@@ -198,7 +189,7 @@ public final void whenSortingBars_thenBarsWithSortedFoos() {
198189
final Query query = sess.createQuery(hql);
199190
final List<Bar> barList = query.list();
200191
for (final Bar bar : barList) {
201-
final Set<Foo> fooSet = bar.getFooList();
192+
final Set<Foo> fooSet = bar.getFooSet();
202193
System.out.println("Bar Id:" + bar.getId());
203194
for (final Foo foo : fooSet) {
204195
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)