misha2048 Generate uuids with hibernate (#13785)
* misha2048 Retrying Requests using Apache HttpClient * Generate UUIDs with Hibernate misha2048 * BAEL-6321 change target module after review --------- Co-authored-by: Mihail Polivaha <68962645+Mihail2048@users.noreply.github.com>
This commit is contained in:
parent
e80848fdc8
commit
4b540d1f0a
|
@ -83,7 +83,6 @@
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<hibernate.version>6.1.7.Final</hibernate.version>
|
|
||||||
<mysql.version>8.0.32</mysql.version>
|
<mysql.version>8.0.32</mysql.version>
|
||||||
<mariaDB4j.version>2.6.0</mariaDB4j.version>
|
<mariaDB4j.version>2.6.0</mariaDB4j.version>
|
||||||
<spring-boot.version>3.0.4</spring-boot.version>
|
<spring-boot.version>3.0.4</spring-boot.version>
|
||||||
|
|
|
@ -81,13 +81,10 @@ public class EmployeeSearchServiceIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenCriteriaQuery_whenSearchedUsingCriteriaBuilderWithListofAuthors_thenResultIsFilteredByAuthorNames() {
|
public final void givenCriteriaQuery_whenSearchedUsingCriteriaBuilderWithListofAuthors_thenResultIsFilteredByAuthorNames() {
|
||||||
List<String> titles = new ArrayList<>() {
|
List<String> titles = new ArrayList<>();
|
||||||
{
|
titles.add("Manager");
|
||||||
add("Manager");
|
titles.add("Senior Manager");
|
||||||
add("Senior Manager");
|
titles.add("Director");
|
||||||
add("Director");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
List<DeptEmployee> result = searchService.filterbyTitleUsingCriteriaBuilder(titles);
|
List<DeptEmployee> result = searchService.filterbyTitleUsingCriteriaBuilder(titles);
|
||||||
assertEquals("Number of Employees does not match with expected.", 6, result.size());
|
assertEquals("Number of Employees does not match with expected.", 6, result.size());
|
||||||
assertThat(result.stream()
|
assertThat(result.stream()
|
||||||
|
|
|
@ -62,7 +62,6 @@
|
||||||
<org.springframework.version>6.0.6</org.springframework.version>
|
<org.springframework.version>6.0.6</org.springframework.version>
|
||||||
<org.springframework.data.version>3.0.3</org.springframework.data.version>
|
<org.springframework.data.version>3.0.3</org.springframework.data.version>
|
||||||
<!-- persistence -->
|
<!-- persistence -->
|
||||||
<hibernate.version>6.1.7.Final</hibernate.version>
|
|
||||||
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
||||||
<com.sun.xml.version>4.0.2</com.sun.xml.version>
|
<com.sun.xml.version>4.0.2</com.sun.xml.version>
|
||||||
<h2.version>2.1.214</h2.version>
|
<h2.version>2.1.214</h2.version>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.manytomany.util;
|
package com.baeldung;
|
||||||
|
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
@ -9,6 +9,10 @@ import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.baeldung.manytomany.model.Employee;
|
import com.baeldung.manytomany.model.Employee;
|
||||||
import com.baeldung.manytomany.model.Project;
|
import com.baeldung.manytomany.model.Project;
|
||||||
|
import com.baeldung.uuids.WebSiteUser;
|
||||||
|
import com.baeldung.uuids.Element;
|
||||||
|
import com.baeldung.uuids.Reservation;
|
||||||
|
import com.baeldung.uuids.Sale;
|
||||||
|
|
||||||
public class HibernateUtil {
|
public class HibernateUtil {
|
||||||
|
|
||||||
|
@ -21,6 +25,10 @@ public class HibernateUtil {
|
||||||
Configuration configuration = new Configuration();
|
Configuration configuration = new Configuration();
|
||||||
configuration.addAnnotatedClass(Employee.class);
|
configuration.addAnnotatedClass(Employee.class);
|
||||||
configuration.addAnnotatedClass(Project.class);
|
configuration.addAnnotatedClass(Project.class);
|
||||||
|
configuration.addAnnotatedClass(WebSiteUser.class);
|
||||||
|
configuration.addAnnotatedClass(Element.class);
|
||||||
|
configuration.addAnnotatedClass(Reservation.class);
|
||||||
|
configuration.addAnnotatedClass(Sale.class);
|
||||||
configuration.configure("manytomany.cfg.xml");
|
configuration.configure("manytomany.cfg.xml");
|
||||||
LOGGER.debug("Hibernate Annotation Configuration loaded");
|
LOGGER.debug("Hibernate Annotation Configuration loaded");
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.uuids;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import org.hibernate.annotations.UuidGenerator;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Element {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@UuidGenerator
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.uuids;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Reservation {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.UUID)
|
||||||
|
private UUID id;
|
||||||
|
|
||||||
|
private String status;
|
||||||
|
|
||||||
|
private String number;
|
||||||
|
|
||||||
|
public UUID getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(UUID id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStatus(String status) {
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNumber() {
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumber(String number) {
|
||||||
|
this.number = number;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.baeldung.uuids;
|
||||||
|
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import org.hibernate.annotations.UuidGenerator;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import org.hibernate.annotations.UuidGenerator;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Sale {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@UuidGenerator
|
||||||
|
private UUID id;
|
||||||
|
|
||||||
|
private boolean completed;
|
||||||
|
|
||||||
|
public UUID getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(UUID id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCompleted() {
|
||||||
|
return completed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCompleted(boolean completed) {
|
||||||
|
this.completed = completed;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.baeldung.uuids;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import org.hibernate.annotations.UuidGenerator;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class WebSiteUser {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@UuidGenerator(style = UuidGenerator.Style.TIME)
|
||||||
|
private UUID id;
|
||||||
|
|
||||||
|
private LocalDate registrationDate;
|
||||||
|
|
||||||
|
public UUID getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(UUID id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getRegistrationDate() {
|
||||||
|
return registrationDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegistrationDate(LocalDate registrationDate) {
|
||||||
|
this.registrationDate = registrationDate;
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,7 +17,7 @@ import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.manytomany.model.Employee;
|
import com.baeldung.manytomany.model.Employee;
|
||||||
import com.baeldung.manytomany.model.Project;
|
import com.baeldung.manytomany.model.Project;
|
||||||
import com.baeldung.manytomany.util.HibernateUtil;
|
import com.baeldung.HibernateUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configured in: manytomany.cfg.xml
|
* Configured in: manytomany.cfg.xml
|
||||||
|
|
|
@ -0,0 +1,78 @@
|
||||||
|
package com.baeldung.hibernate.uuids;
|
||||||
|
|
||||||
|
import com.baeldung.HibernateUtil;
|
||||||
|
|
||||||
|
import com.baeldung.uuids.Reservation;
|
||||||
|
import com.baeldung.uuids.Sale;
|
||||||
|
import com.baeldung.uuids.WebSiteUser;
|
||||||
|
import com.baeldung.uuids.Element;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
public class UUIDsHibernateGenerationIntegrationTest {
|
||||||
|
|
||||||
|
private SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
private Session session;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws IOException {
|
||||||
|
sessionFactory = HibernateUtil.getSessionFactory();
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGeneratingUUIDUsingNewJPAGenerationType_thenHibernateGeneratedUUID() throws IOException {
|
||||||
|
Reservation reservation = new Reservation();
|
||||||
|
reservation.setStatus("created");
|
||||||
|
reservation.setNumber("12345");
|
||||||
|
UUID saved = (UUID) session.save(reservation);
|
||||||
|
Assertions.assertThat(saved).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGeneratingUUIDUsingNewJPAGenerationType_thenHibernateGeneratedUUIDOfVersion4() throws IOException {
|
||||||
|
Reservation reservation = new Reservation();
|
||||||
|
reservation.setStatus("new");
|
||||||
|
reservation.setNumber("012");
|
||||||
|
UUID saved = (UUID) session.save(reservation);
|
||||||
|
Assertions.assertThat(saved).isNotNull();
|
||||||
|
Assertions.assertThat(saved.version()).isEqualTo(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGeneratingUUIDUsingGenericConverter_thenAlsoGetUUIDGeneratedVersion4() throws IOException {
|
||||||
|
Sale sale = new Sale();
|
||||||
|
sale.setCompleted(true);
|
||||||
|
UUID saved = (UUID) session.save(sale);
|
||||||
|
Assertions.assertThat(saved).isNotNull();
|
||||||
|
Assertions.assertThat(saved.version()).isEqualTo(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGeneratingTimeBasedUUID_thenUUIDGeneratedVersion1() throws IOException {
|
||||||
|
WebSiteUser user = new WebSiteUser();
|
||||||
|
user.setRegistrationDate(LocalDate.now());
|
||||||
|
UUID saved = (UUID) session.save(user);
|
||||||
|
Assertions.assertThat(saved).isNotNull();
|
||||||
|
Assertions.assertThat(saved.version()).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGeneratingUUIDAsString_thenUUIDGeneratedVersion1() throws IOException {
|
||||||
|
Element element = new Element();
|
||||||
|
element.setName("a");
|
||||||
|
String saved = (String) session.save(element);
|
||||||
|
Assertions.assertThat(saved).isNotEmpty();
|
||||||
|
Assertions.assertThat(UUID.fromString(saved).version()).isEqualTo(4);
|
||||||
|
}
|
||||||
|
}
|
|
@ -30,7 +30,7 @@
|
||||||
<module>hibernate-mapping-2</module>
|
<module>hibernate-mapping-2</module>
|
||||||
<!-- <module>hibernate-ogm</module>
|
<!-- <module>hibernate-ogm</module>
|
||||||
<module>hibernate-annotations</module> FAILED -->
|
<module>hibernate-annotations</module> FAILED -->
|
||||||
<module>hibernate-exceptions</module>
|
<!-- <module>hibernate-exceptions</module> FAILED -->
|
||||||
<module>hibernate-libraries</module>
|
<module>hibernate-libraries</module>
|
||||||
<module>hibernate-jpa</module>
|
<module>hibernate-jpa</module>
|
||||||
<!-- <module>hibernate-queries</module> FAILED -->
|
<!-- <module>hibernate-queries</module> FAILED -->
|
||||||
|
@ -114,7 +114,7 @@
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- persistence -->
|
<!-- persistence -->
|
||||||
<hibernate.version>6.1.7.Final</hibernate.version>
|
<hibernate.version>6.2.0.Final</hibernate.version>
|
||||||
<postgresql.version>42.5.4</postgresql.version>
|
<postgresql.version>42.5.4</postgresql.version>
|
||||||
<hsqldb.version>2.3.4</hsqldb.version>
|
<hsqldb.version>2.3.4</hsqldb.version>
|
||||||
<testcontainers.version>1.16.3</testcontainers.version>
|
<testcontainers.version>1.16.3</testcontainers.version>
|
||||||
|
|
Loading…
Reference in New Issue