added example code for BAEL-2083 (#6486)
* added example code for BAEL-2083 * updated example code for BAEL-2083
This commit is contained in:
parent
88ca90327c
commit
66f20f1441
|
@ -83,13 +83,18 @@
|
|||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${openjdk-jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>hibernate5</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<directory>src/test/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -74,4 +74,6 @@ public class CustomClassIntegrationTest {
|
|||
assertEquals("John Smith", result.getEmployeeName());
|
||||
assertEquals("Sales", result.getDepartmentName());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue