BAEL-5985 Hibernate @CreationTimestamp and @UpdateTimestamp (#13690)

* BAEL-5985 Hibernate @CreationTimestamp and @UpdateTimestamp

* BAEL-5985 Remove unnecessary imports

* BAEL-5985 Swap order in assertEquals

---------

Co-authored-by: Mariusz Kaczmarczyk <to@mariu.sh>
This commit is contained in:
mariudotsh 2023-04-04 09:54:37 +02:00 committed by GitHub
parent 28c8893b38
commit e9a2b58924
2 changed files with 173 additions and 0 deletions

View File

@ -0,0 +1,57 @@
package com.baeldung.hibernate.creationupdatetimestamp.model;
import java.time.Instant;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
@Entity
public class Book {
@Id
@GeneratedValue
private Long id;
private String title;
@CreationTimestamp
private Instant createdOn;
@UpdateTimestamp
private Instant lastUpdatedOn;
public Book() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Instant getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Instant createdOn) {
this.createdOn = createdOn;
}
public Instant getLastUpdatedOn() {
return lastUpdatedOn;
}
public void setLastUpdatedOn(Instant lastUpdatedOn) {
this.lastUpdatedOn = lastUpdatedOn;
}
}

View File

@ -0,0 +1,116 @@
package com.baeldung.hibernate.creationupdatetimestamp;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.time.Instant;
import org.h2.Driver;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.service.ServiceRegistry;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.baeldung.hibernate.creationupdatetimestamp.model.Book;
class HibernateCreationUpdateTimestampIntegrationTest {
private static SessionFactory sessionFactory;
private Session session;
@BeforeAll
public static void beforeTests() {
Configuration configuration = new Configuration().addAnnotatedClass(Book.class)
.setProperty("hibernate.dialect", H2Dialect.class.getName())
.setProperty("hibernate.connection.driver_class", Driver.class.getName())
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test")
.setProperty("hibernate.connection.username", "sa")
.setProperty("hibernate.connection.password", "")
.setProperty("hibernate.hbm2ddl.auto", "update");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
.build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
@Test
void whenCreatingEntity_ThenCreatedOnIsSet() {
session = sessionFactory.openSession();
session.beginTransaction();
Book book = new Book();
session.save(book);
session.getTransaction()
.commit();
session.close();
assertNotNull(book.getCreatedOn());
}
@Test
void whenCreatingEntity_ThenCreatedOnAndLastUpdatedOnAreBothSet() {
session = sessionFactory.openSession();
session.beginTransaction();
Book book = new Book();
session.save(book);
session.getTransaction()
.commit();
session.close();
assertNotNull(book.getCreatedOn());
assertNotNull(book.getLastUpdatedOn());
}
@Test
void whenCreatingEntity_ThenCreatedOnAndLastUpdatedOnAreNotEqual() {
session = sessionFactory.openSession();
session.beginTransaction();
Book book = new Book();
session.save(book);
session.getTransaction()
.commit();
session.close();
assertNotEquals(book.getCreatedOn(), book.getLastUpdatedOn());
}
@Test
void whenUpdatingEntity_ThenLastUpdatedOnIsUpdatedAndCreatedOnStaysTheSame() {
session = sessionFactory.openSession();
session.beginTransaction();
Book book = new Book();
session.save(book);
session.flush();
Instant createdOnAfterCreation = book.getCreatedOn();
Instant lastUpdatedOnAfterCreation = book.getLastUpdatedOn();
String newName = "newName";
book.setTitle(newName);
session.getTransaction()
.commit();
session.close();
Instant createdOnAfterUpdate = book.getCreatedOn();
Instant lastUpdatedOnAfterUpdate = book.getLastUpdatedOn();
assertEquals(newName, book.getTitle());
assertNotNull(createdOnAfterUpdate);
assertNotNull(lastUpdatedOnAfterUpdate);
assertEquals(createdOnAfterCreation, createdOnAfterUpdate);
assertNotEquals(lastUpdatedOnAfterCreation, lastUpdatedOnAfterUpdate);
}
@AfterAll
static void afterTests() {
sessionFactory.close();
}
}