Skip to content

Commit ce91d77

Browse files
author
eugenp
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents 0741945 + a973834 commit ce91d77

20 files changed

Lines changed: 488 additions & 496 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: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
package org.baeldung.persistence.model;
22

33
import java.io.Serializable;
4-
import java.util.List;
4+
import java.util.Set;
55

6-
import javax.persistence.Column;
6+
import javax.persistence.CascadeType;
77
import javax.persistence.Entity;
8+
import javax.persistence.FetchType;
89
import javax.persistence.GeneratedValue;
910
import javax.persistence.GenerationType;
1011
import javax.persistence.Id;
12+
import javax.persistence.NamedQuery;
13+
import javax.persistence.OneToMany;
14+
15+
import org.hibernate.annotations.OrderBy;
16+
17+
import com.google.common.collect.Sets;
1118

1219
@Entity
20+
@NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b")
1321
public class Bar implements Serializable {
1422

1523
@Id
1624
@GeneratedValue(strategy = GenerationType.AUTO)
17-
private long id;
25+
private int id;
1826

19-
@Column(nullable = false)
2027
private String name;
2128

22-
private List<Foo> foos;
29+
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
30+
@OrderBy(clause = "NAME DESC")
31+
private Set<Foo> fooSet = Sets.newHashSet();
2332

2433
public Bar() {
2534
super();
@@ -33,11 +42,19 @@ public Bar(final String name) {
3342

3443
// API
3544

36-
public long getId() {
45+
public Set<Foo> getFooSet() {
46+
return fooSet;
47+
}
48+
49+
public void setFooSet(final Set<Foo> fooSet) {
50+
this.fooSet = fooSet;
51+
}
52+
53+
public int getId() {
3754
return id;
3855
}
3956

40-
public void setId(final long id) {
57+
public void setId(final int id) {
4158
this.id = id;
4259
}
4360

@@ -49,10 +66,6 @@ public void setName(final String name) {
4966
this.name = name;
5067
}
5168

52-
public List<Foo> getFooList() {
53-
return foos;
54-
}
55-
5669
//
5770

5871
@Override
@@ -83,7 +96,7 @@ public boolean equals(final Object obj) {
8396
@Override
8497
public String toString() {
8598
final StringBuilder builder = new StringBuilder();
86-
builder.append("Foo [name=").append(name).append("]");
99+
builder.append("Bar [name=").append(name).append("]");
87100
return builder.toString();
88101
}
89102

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
</class>
19+
</hibernate-mapping>

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
import javax.persistence.GeneratedValue;
88
import javax.persistence.GenerationType;
99
import javax.persistence.Id;
10+
import javax.persistence.JoinColumn;
11+
import javax.persistence.ManyToOne;
12+
13+
import org.hibernate.annotations.Fetch;
14+
import org.hibernate.annotations.FetchMode;
1015

1116
@Entity
1217
public class Foo implements Serializable {
@@ -18,17 +23,29 @@ public class Foo implements Serializable {
1823
@Column(nullable = false)
1924
private String name;
2025

26+
@ManyToOne(targetEntity = Bar.class)
27+
@JoinColumn(name = "BAR_ID")
28+
@Fetch(FetchMode.JOIN)
29+
private Bar bar = new Bar();
30+
2131
public Foo() {
2232
super();
2333
}
2434

2535
public Foo(final String name) {
2636
super();
27-
2837
this.name = name;
2938
}
3039

31-
// API
40+
//
41+
42+
public Bar getBar() {
43+
return bar;
44+
}
45+
46+
public void setBar(final Bar bar) {
47+
this.bar = bar;
48+
}
3249

3350
public long getId() {
3451
return id;
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/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>
36+
37+
</hibernate-configuration>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.persistence;
2+
3+
import org.baeldung.persistence.hibernate.FooPaginationPersistenceIntegrationTest;
4+
import org.baeldung.persistence.hibernate.FooSortingPersistenceServiceTest;
5+
import org.baeldung.persistence.service.FooServiceBasicPersistenceIntegrationTest;
6+
import org.baeldung.persistence.service.FooServicePersistenceIntegrationTest;
7+
import org.baeldung.persistence.service.ParentServicePersistenceIntegrationTest;
8+
import org.junit.runner.RunWith;
9+
import org.junit.runners.Suite;
10+
11+
@RunWith(Suite.class)
12+
@Suite.SuiteClasses({// @formatter:off
13+
FooServiceBasicPersistenceIntegrationTest.class
14+
,FooPaginationPersistenceIntegrationTest.class
15+
,FooServicePersistenceIntegrationTest.class
16+
,ParentServicePersistenceIntegrationTest.class
17+
,FooSortingPersistenceServiceTest.class
18+
}) // @formatter:on
19+
public class IntegrationTestSuite {
20+
//
21+
}

spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePaginationPersistenceIntegrationTest.java renamed to spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.baeldung.persistence.service;
1+
package org.baeldung.persistence.hibernate;
22

33
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
44
import static org.hamcrest.Matchers.hasSize;
@@ -8,6 +8,7 @@
88
import java.util.List;
99

1010
import org.baeldung.persistence.model.Foo;
11+
import org.baeldung.persistence.service.IFooService;
1112
import org.baeldung.spring.PersistenceConfig;
1213
import org.hibernate.Criteria;
1314
import org.hibernate.Query;
@@ -30,7 +31,7 @@
3031

3132
@RunWith(SpringJUnit4ClassRunner.class)
3233
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
33-
public class FooServicePaginationPersistenceIntegrationTest {
34+
public class FooPaginationPersistenceIntegrationTest {
3435

3536
@Autowired
3637
private SessionFactory sessionFactory;

0 commit comments

Comments
 (0)