diff --git a/persistence-modules/hibernate5/pom.xml b/persistence-modules/hibernate5/pom.xml
index a09669c8b5..c7f08e50d5 100644
--- a/persistence-modules/hibernate5/pom.xml
+++ b/persistence-modules/hibernate5/pom.xml
@@ -83,13 +83,18 @@
jmh-generator-annprocess
${openjdk-jmh.version}
+
+ javax.xml.bind
+ jaxb-api
+ 2.3.0
+
hibernate5
- src/main/resources
+ src/test/resources
true
diff --git a/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java
index ea0af97d5a..48c9b9d5c2 100644
--- a/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java
+++ b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java
@@ -113,6 +113,7 @@ public class HibernateUtil {
metadataSources.addAnnotatedClass(OptimisticLockingCourse.class);
metadataSources.addAnnotatedClass(OptimisticLockingStudent.class);
metadataSources.addAnnotatedClass(OfficeEmployee.class);
+ metadataSources.addAnnotatedClass(Post.class);
Metadata metadata = metadataSources.getMetadataBuilder()
.applyBasicType(LocalDateStringType.INSTANCE)
diff --git a/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Post.java b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Post.java
new file mode 100644
index 0000000000..25e51e35d0
--- /dev/null
+++ b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Post.java
@@ -0,0 +1,59 @@
+package com.baeldung.hibernate.pojo;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name = "posts")
+public class Post {
+
+ @Id
+ @GeneratedValue
+ private int id;
+
+ private String title;
+
+ private String body;
+
+ public Post() { }
+
+ public Post(String title, String body) {
+ this.title = title;
+ this.body = body;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public String getBody() {
+ return body;
+ }
+
+ public void setBody(String body) {
+ this.body = body;
+ }
+
+ @Override
+ public String toString() {
+ return "Post{" +
+ "id=" + id +
+ ", title='" + title + '\'' +
+ ", body='" + body + '\'' +
+ '}';
+ }
+}
diff --git a/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/transaction/PostService.java b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/transaction/PostService.java
new file mode 100644
index 0000000000..5a4eb20079
--- /dev/null
+++ b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/transaction/PostService.java
@@ -0,0 +1,29 @@
+package com.baeldung.hibernate.transaction;
+
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.query.Query;
+
+public class PostService {
+
+
+ private Session session;
+
+ public PostService(Session session) {
+ this.session = session;
+ }
+
+
+ public void updatePost(String title, String body, int id) {
+ Transaction txn = session.beginTransaction();
+ Query updateQuery = session.createQuery("UPDATE Post p SET p.title = ?1, p.body = ?2 WHERE p.id = ?3");
+ updateQuery.setParameter(1, title);
+ updateQuery.setParameter(2, body);
+ updateQuery.setParameter(3, id);
+ updateQuery.executeUpdate();
+ txn.commit();
+ }
+
+
+
+}
diff --git a/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java
index 29ae55b773..e64e836924 100644
--- a/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java
+++ b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java
@@ -74,4 +74,6 @@ public class CustomClassIntegrationTest {
assertEquals("John Smith", result.getEmployeeName());
assertEquals("Sales", result.getDepartmentName());
}
+
+
}
diff --git a/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/transaction/TransactionIntegrationTest.java b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/transaction/TransactionIntegrationTest.java
new file mode 100644
index 0000000000..246a7d59f9
--- /dev/null
+++ b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/transaction/TransactionIntegrationTest.java
@@ -0,0 +1,57 @@
+package com.baeldung.hibernate.transaction;
+
+import com.baeldung.hibernate.HibernateUtil;
+import com.baeldung.hibernate.pojo.Post;
+import com.baeldung.hibernate.transaction.PostService;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+
+public class TransactionIntegrationTest {
+
+ private static PostService postService;
+ private static Session session;
+ private static Logger logger = LoggerFactory.getLogger(TransactionIntegrationTest.class);
+
+ @BeforeClass
+ public static void init() throws IOException {
+ Properties properties = new Properties();
+ properties.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
+ properties.setProperty("hibernate.connection.url", "jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1");
+ properties.setProperty("hibernate.connection.username", "sa");
+ properties.setProperty("hibernate.show_sql", "true");
+ properties.setProperty("jdbc.password", "");
+ properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
+ properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
+ SessionFactory sessionFactory = HibernateUtil.getSessionFactoryByProperties(properties);
+ session = sessionFactory.openSession();
+ postService = new PostService(session);
+ }
+
+ @Test
+ public void givenTitleAndBody_whenRepositoryUpdatePost_thenUpdatePost() {
+
+ Post post = new Post("This is a title", "This is a sample post");
+ session.persist(post);
+
+ String title = "[UPDATE] Java HowTos";
+ String body = "This is an updated posts on Java how-tos";
+ postService.updatePost(title, body, post.getId());
+
+ session.refresh(post);
+
+ assertEquals(post.getTitle(), title);
+ assertEquals(post.getBody(), body);
+ }
+
+
+}