Skip to content

Commit b7e89a0

Browse files
committed
Hibernate and H2
1 parent 02dac65 commit b7e89a0

16 files changed

+263
-22
lines changed

pom.xml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Now, we add GWT (gwt-servlet and gwt-user artifacts) and Log4J to our project.
2323
<gwt.version>2.7.0</gwt.version>
2424
<mybatis.version>3.2.1</mybatis.version>
2525
<mybatis-spring.version>1.2.0</mybatis-spring.version>
26+
<hibernate.version>4.3.7.Final</hibernate.version>
2627
<!--
2728
<mysql-connector-java.version>5.1.18</mysql-connector-java.version>
2829
-->
@@ -31,7 +32,11 @@ Now, we add GWT (gwt-servlet and gwt-user artifacts) and Log4J to our project.
3132
<postgresql-connector-java.version>9.3-1102-jdbc3</postgresql-connector-java.version>
3233
-->
3334
<!-- http://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
35+
<!--
3436
<sqlite-connector-java.version>3.8.7</sqlite-connector-java.version>
37+
-->
38+
<!-- http://mvnrepository.com/artifact/com.h2database/h2 -->
39+
<h2.version>1.4.184</h2.version>
3540
<!-- Note: GWT needs at least java 1.6 -->
3641
<maven.compiler.source>1.7</maven.compiler.source>
3742
<maven.compiler.target>1.7</maven.compiler.target>
@@ -50,6 +55,11 @@ Now, we add GWT (gwt-servlet and gwt-user artifacts) and Log4J to our project.
5055
<artifactId>spring-jdbc</artifactId>
5156
<version>${spring.version}</version>
5257
</dependency>
58+
<dependency>
59+
<groupId>org.springframework</groupId>
60+
<artifactId>spring-orm</artifactId>
61+
<version>${spring.version}</version>
62+
</dependency>
5363

5464
<!-- GWT -->
5565
<dependency>
@@ -92,6 +102,12 @@ Now, we add GWT (gwt-servlet and gwt-user artifacts) and Log4J to our project.
92102
<artifactId>mybatis-spring</artifactId>
93103
<version>${mybatis-spring.version}</version>
94104
</dependency>
105+
<dependency>
106+
<groupId>org.hibernate</groupId>
107+
<artifactId>hibernate-core</artifactId>
108+
<version>4.3.7.Final</version>
109+
<type>jar</type>
110+
</dependency>
95111
<!--
96112
<dependency>
97113
<groupId>mysql</groupId>
@@ -103,12 +119,17 @@ Now, we add GWT (gwt-servlet and gwt-user artifacts) and Log4J to our project.
103119
<artifactId>postgresql</artifactId>
104120
<version>${postgresql-connector-java.version}</version>
105121
</dependency>
106-
-->
107122
<dependency>
108123
<groupId>org.xerial</groupId>
109124
<artifactId>sqlite-jdbc</artifactId>
110125
<version>${sqlite-connector-java.version}</version>
111126
</dependency>
127+
-->
128+
<dependency>
129+
<groupId>com.h2database</groupId>
130+
<artifactId>h2</artifactId>
131+
<version>${h2.version}</version>
132+
</dependency>
112133
</dependencies>
113134

114135
<build>

src/main/java/com/hellogwt/client/GreetingService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ public interface GreetingService extends RemoteService {
3636
List<Greeting> getGreetings();
3737

3838
String greet(String name);
39+
40+
String persistenceClassName();
3941
}

src/main/java/com/hellogwt/client/GreetingServiceAsync.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ public interface GreetingServiceAsync {
3232
void getGreetings(AsyncCallback<List<Greeting>> async);
3333

3434
void greet(String name, AsyncCallback<String> callback);
35+
36+
void persistenceClassName(AsyncCallback<String> callback);
3537
}

src/main/java/com/hellogwt/client/HelloGWTWidget.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,21 @@ public void onSuccess(Void result) {
9191
FlexTable greetingsFlexTable;
9292
@UiField
9393
SpanElement statusMessage;
94+
@UiField
95+
SpanElement persistenceClassName;
9496

9597
public HelloGWTWidget() {
9698
initWidget(uiBinder.createAndBindUi(this));
99+
greetingService.persistenceClassName(new AsyncCallback<String>() {
100+
@Override
101+
public void onFailure(Throwable caught) {
102+
statusMessage.setInnerHTML(clientMsg.error() + caught.getLocalizedMessage());
103+
}
104+
@Override
105+
public void onSuccess(String result) {
106+
persistenceClassName.setInnerHTML(result);
107+
}
108+
});
97109
refreshGreetingsTable();
98110
}
99111

src/main/java/com/hellogwt/client/HelloGWTWidget.ui.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ removing the package prefix "com.hellogwt.client.".
7474
<span ui:field="statusMessage">
7575
<ui:msg key="defaultStatusMessage" meaning="Default status message.">Application is loaded.</ui:msg>
7676
</span>
77+
<span ui:field="persistenceClassName"></span>
7778
<p>
7879
<a href="?locale=fr">français</a>,
7980
<a href="?locale=en">English</a>

src/main/java/com/hellogwt/server/GreetingServiceImpl.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,32 @@
1616
package com.hellogwt.server;
1717

1818
import com.hellogwt.client.GreetingService;
19-
import com.hellogwt.server.persistence.GreetingMapper;
19+
import com.hellogwt.server.persistence.GreetingPersistence;
2020
import com.hellogwt.shared.domain.Greeting;
2121
import java.util.List;
2222
import org.springframework.beans.factory.annotation.Autowired;
2323
import org.springframework.stereotype.Service;
2424

2525
/**
2626
* A class that implements GreetingService interface in com.hellogwt.server
27-
* package. Don't forget to annotate it with @Service. Class GreetingServiceImpl
27+
* package.
28+
*
29+
* Don't forget to annotate it with @Service. Class GreetingServiceImpl
2830
* implements method greet(). In our case the method takes one String as an
2931
* argument, builds another String and returns it. Implements all declared CRUD
3032
* methods in service GreetingServiceImpl using created mapper.
33+
*
34+
* @Transactional annotation provided by Spring:
35+
* when a method is annotated by this annotation, Spring will inject
36+
* transaction support code into the method - thus we don’t have two write any
37+
* code to handle transaction explicitly.
3138
*/
3239
@Service("greetingService")
3340
public class GreetingServiceImpl implements GreetingService {
3441

3542
@Autowired
36-
private GreetingMapper greetingMapper;
37-
43+
private GreetingPersistence greetingMapper;
44+
3845
@Override
3946
public Greeting getGreeting(String text) {
4047
return greetingMapper.getGreeting(text);
@@ -64,4 +71,9 @@ public List<Greeting> getGreetings() {
6471
public String greet(String name) {
6572
return "Hello, " + name + "!";
6673
}
74+
75+
@Override
76+
public String persistenceClassName() {
77+
return (greetingMapper instanceof com.hellogwt.server.persistence.GreetingMapper ? "MyBatis": "Hibernate");
78+
}
6779
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Hibernate class to handle Greeting.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.hellogwt.server.persistence;
17+
18+
import org.springframework.stereotype.Repository;
19+
import com.hellogwt.shared.domain.Greeting;
20+
import java.util.List;
21+
import org.hibernate.Criteria;
22+
import org.hibernate.Query;
23+
import org.hibernate.Session;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.orm.hibernate4.HibernateTemplate;
26+
import org.springframework.transaction.annotation.Transactional;
27+
28+
@Repository
29+
public class GreetingDAO implements GreetingPersistence {
30+
31+
@Autowired
32+
private HibernateTemplate hibernateTemplate;
33+
34+
@Override
35+
@Transactional(readOnly=true)
36+
public Greeting getGreeting(String text) {
37+
if (getCurrentSession() == null) {
38+
throw new NullPointerException("getCurrentSession() is null !");
39+
}
40+
List<Greeting> greetingList = null;
41+
try {
42+
Query q = getCurrentSession().createQuery("from Greeting as greeting where greeting.text=:text");
43+
q.setParameter("text", text);
44+
greetingList = (List<Greeting>) q.list();
45+
} catch (Exception e) {
46+
e.printStackTrace();
47+
}
48+
return greetingList.size() > 0 ? greetingList.get(0) : null;
49+
}
50+
51+
@Override
52+
@Transactional(readOnly=false)
53+
public void addGreeting(String author, String text) {
54+
Greeting greeting = new Greeting();
55+
greeting.setAuthor(author);
56+
greeting.setText(text);
57+
hibernateTemplate.save(greeting);
58+
}
59+
60+
@Override
61+
@Transactional(readOnly=false)
62+
public void updateGreeting(String author, String text) {
63+
Greeting greeting = getGreeting(text);
64+
if (greeting != null) {
65+
greeting.setAuthor(author);
66+
hibernateTemplate.save(greeting);
67+
}
68+
}
69+
70+
@Override
71+
@Transactional(readOnly=false)
72+
public void deleteGreeting(String text) {
73+
Greeting greeting = getGreeting(text);
74+
if (greeting != null) {
75+
getCurrentSession().delete(greeting);
76+
}
77+
}
78+
79+
@Override
80+
@Transactional(readOnly=true)
81+
public List<Greeting> getGreetings() {
82+
return (List<Greeting>) getCurrentSession()
83+
.createCriteria(Greeting.class)
84+
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
85+
.list();
86+
}
87+
88+
private Session getCurrentSession() {
89+
if (hibernateTemplate.getSessionFactory() == null) {
90+
throw new NullPointerException("getSessionFactory() is null !");
91+
}
92+
return hibernateTemplate.getSessionFactory().getCurrentSession();
93+
}
94+
}

src/main/java/com/hellogwt/server/persistence/GreetingMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.apache.ibatis.annotations.Param;
2323
import org.apache.ibatis.annotations.Select;
2424

25-
public interface GreetingMapper {
25+
public interface GreetingMapper extends GreetingPersistence {
2626

2727
@Select("SELECT * FROM greetings WHERE text = #{text}")
2828
Greeting getGreeting(@Param("text") String text);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Common interface to test Hibernate and MyBatis.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.hellogwt.server.persistence;
17+
18+
import com.hellogwt.shared.domain.Greeting;
19+
import java.util.List;
20+
21+
/**
22+
* Common interface for Hibernate and MyBatis DAO/Mapper
23+
*/
24+
public interface GreetingPersistence {
25+
public Greeting getGreeting(String text);
26+
public void addGreeting(String author, String text);
27+
public void updateGreeting(String author, String text);
28+
public void deleteGreeting(String text);
29+
public List<Greeting> getGreetings();
30+
}

src/main/java/com/hellogwt/shared/domain/Greeting.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
*
44
* Class Greeting is used by server and client parts of application,
55
* so it should be created in com.hellogwt.shared.domain package.
6+
*
7+
* It is mandatory that this standard Java POJO implements java.io.Serializable
8+
* or com.google.gwt.user.client.rpc.IsSerializable and has a no-argument
9+
* constructor!
610
*
711
* Licensed under the Apache License, Version 2.0 (the "License");
812
* you may not use this file except in compliance with the License.
@@ -19,9 +23,18 @@
1923
package com.hellogwt.shared.domain;
2024

2125
import java.io.Serializable;
26+
import javax.persistence.Entity;
27+
import javax.persistence.GeneratedValue;
28+
import javax.persistence.GenerationType;
29+
import javax.persistence.Id;
30+
import javax.persistence.Table;
2231

32+
@Entity
33+
@Table(name= "greetings")
2334
public class Greeting implements Serializable {
2435

36+
@Id
37+
@GeneratedValue(strategy = GenerationType.IDENTITY)
2538
private Integer id;
2639
private String author;
2740
private String text;

0 commit comments

Comments
 (0)