Skip to content

Commit 66f20f1

Browse files
SeunMattmaibin
authored andcommitted
added example code for BAEL-2083 (eugenp#6486)
* added example code for BAEL-2083 * updated example code for BAEL-2083
1 parent 88ca903 commit 66f20f1

6 files changed

Lines changed: 154 additions & 1 deletion

File tree

‎persistence-modules/hibernate5/pom.xml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,18 @@
8383
<artifactId>jmh-generator-annprocess</artifactId>
8484
<version>${openjdk-jmh.version}</version>
8585
</dependency>
86+
<dependency>
87+
<groupId>javax.xml.bind</groupId>
88+
<artifactId>jaxb-api</artifactId>
89+
<version>2.3.0</version>
90+
</dependency>
8691
</dependencies>
8792

8893
<build>
8994
<finalName>hibernate5</finalName>
9095
<resources>
9196
<resource>
92-
<directory>src/main/resources</directory>
97+
<directory>src/test/resources</directory>
9398
<filtering>true</filtering>
9499
</resource>
95100
</resources>

‎persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
113113
metadataSources.addAnnotatedClass(OptimisticLockingCourse.class);
114114
metadataSources.addAnnotatedClass(OptimisticLockingStudent.class);
115115
metadataSources.addAnnotatedClass(OfficeEmployee.class);
116+
metadataSources.addAnnotatedClass(Post.class);
116117

117118
Metadata metadata = metadataSources.getMetadataBuilder()
118119
.applyBasicType(LocalDateStringType.INSTANCE)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
import javax.persistence.Table;
7+
8+
@Entity
9+
@Table(name = "posts")
10+
public class Post {
11+
12+
@Id
13+
@GeneratedValue
14+
private int id;
15+
16+
private String title;
17+
18+
private String body;
19+
20+
public Post() { }
21+
22+
public Post(String title, String body) {
23+
this.title = title;
24+
this.body = body;
25+
}
26+
27+
public int getId() {
28+
return id;
29+
}
30+
31+
public void setId(int id) {
32+
this.id = id;
33+
}
34+
35+
public String getTitle() {
36+
return title;
37+
}
38+
39+
public void setTitle(String title) {
40+
this.title = title;
41+
}
42+
43+
public String getBody() {
44+
return body;
45+
}
46+
47+
public void setBody(String body) {
48+
this.body = body;
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return "Post{" +
54+
"id=" + id +
55+
", title='" + title + '\'' +
56+
", body='" + body + '\'' +
57+
'}';
58+
}
59+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.hibernate.transaction;
2+
3+
import org.hibernate.Session;
4+
import org.hibernate.Transaction;
5+
import org.hibernate.query.Query;
6+
7+
public class PostService {
8+
9+
10+
private Session session;
11+
12+
public PostService(Session session) {
13+
this.session = session;
14+
}
15+
16+
17+
public void updatePost(String title, String body, int id) {
18+
Transaction txn = session.beginTransaction();
19+
Query updateQuery = session.createQuery("UPDATE Post p SET p.title = ?1, p.body = ?2 WHERE p.id = ?3");
20+
updateQuery.setParameter(1, title);
21+
updateQuery.setParameter(2, body);
22+
updateQuery.setParameter(3, id);
23+
updateQuery.executeUpdate();
24+
txn.commit();
25+
}
26+
27+
28+
29+
}

‎persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@ public void whenResultTransformerOnQuery_ThenListOfResultIsReturned() {
7474
assertEquals("John Smith", result.getEmployeeName());
7575
assertEquals("Sales", result.getDepartmentName());
7676
}
77+
78+
7779
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.hibernate.transaction;
2+
3+
import com.baeldung.hibernate.HibernateUtil;
4+
import com.baeldung.hibernate.pojo.Post;
5+
import com.baeldung.hibernate.transaction.PostService;
6+
import org.hibernate.Session;
7+
import org.hibernate.SessionFactory;
8+
import org.junit.BeforeClass;
9+
import org.junit.Test;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import java.io.FileInputStream;
14+
import java.io.IOException;
15+
import java.util.Properties;
16+
17+
import static org.junit.Assert.assertEquals;
18+
19+
public class TransactionIntegrationTest {
20+
21+
private static PostService postService;
22+
private static Session session;
23+
private static Logger logger = LoggerFactory.getLogger(TransactionIntegrationTest.class);
24+
25+
@BeforeClass
26+
public static void init() throws IOException {
27+
Properties properties = new Properties();
28+
properties.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
29+
properties.setProperty("hibernate.connection.url", "jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1");
30+
properties.setProperty("hibernate.connection.username", "sa");
31+
properties.setProperty("hibernate.show_sql", "true");
32+
properties.setProperty("jdbc.password", "");
33+
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
34+
properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
35+
SessionFactory sessionFactory = HibernateUtil.getSessionFactoryByProperties(properties);
36+
session = sessionFactory.openSession();
37+
postService = new PostService(session);
38+
}
39+
40+
@Test
41+
public void givenTitleAndBody_whenRepositoryUpdatePost_thenUpdatePost() {
42+
43+
Post post = new Post("This is a title", "This is a sample post");
44+
session.persist(post);
45+
46+
String title = "[UPDATE] Java HowTos";
47+
String body = "This is an updated posts on Java how-tos";
48+
postService.updatePost(title, body, post.getId());
49+
50+
session.refresh(post);
51+
52+
assertEquals(post.getTitle(), title);
53+
assertEquals(post.getBody(), body);
54+
}
55+
56+
57+
}

0 commit comments

Comments
 (0)