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:
Mikhail Polivakha 2023-04-20 17:30:56 +03:00 committed by GitHub
parent e80848fdc8
commit 4b540d1f0a
11 changed files with 240 additions and 15 deletions

View File

@ -83,7 +83,6 @@
</dependencies>
<properties>
<hibernate.version>6.1.7.Final</hibernate.version>
<mysql.version>8.0.32</mysql.version>
<mariaDB4j.version>2.6.0</mariaDB4j.version>
<spring-boot.version>3.0.4</spring-boot.version>
@ -92,4 +91,4 @@
<logback.version>1.4.6</logback.version>
</properties>
</project>
</project>

View File

@ -81,13 +81,10 @@ public class EmployeeSearchServiceIntegrationTest {
@Test
public final void givenCriteriaQuery_whenSearchedUsingCriteriaBuilderWithListofAuthors_thenResultIsFilteredByAuthorNames() {
List<String> titles = new ArrayList<>() {
{
add("Manager");
add("Senior Manager");
add("Director");
}
};
List<String> titles = new ArrayList<>();
titles.add("Manager");
titles.add("Senior Manager");
titles.add("Director");
List<DeptEmployee> result = searchService.filterbyTitleUsingCriteriaBuilder(titles);
assertEquals("Number of Employees does not match with expected.", 6, result.size());
assertThat(result.stream()

View File

@ -62,7 +62,6 @@
<org.springframework.version>6.0.6</org.springframework.version>
<org.springframework.data.version>3.0.3</org.springframework.data.version>
<!-- persistence -->
<hibernate.version>6.1.7.Final</hibernate.version>
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
<com.sun.xml.version>4.0.2</com.sun.xml.version>
<h2.version>2.1.214</h2.version>

View File

@ -1,4 +1,4 @@
package com.baeldung.manytomany.util;
package com.baeldung;
import org.hibernate.SessionFactory;
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.Project;
import com.baeldung.uuids.WebSiteUser;
import com.baeldung.uuids.Element;
import com.baeldung.uuids.Reservation;
import com.baeldung.uuids.Sale;
public class HibernateUtil {
@ -21,6 +25,10 @@ public class HibernateUtil {
Configuration configuration = new Configuration();
configuration.addAnnotatedClass(Employee.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");
LOGGER.debug("Hibernate Annotation Configuration loaded");

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -17,7 +17,7 @@ import org.junit.Test;
import com.baeldung.manytomany.model.Employee;
import com.baeldung.manytomany.model.Project;
import com.baeldung.manytomany.util.HibernateUtil;
import com.baeldung.HibernateUtil;
/**
* Configured in: manytomany.cfg.xml

View File

@ -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);
}
}

View File

@ -30,7 +30,7 @@
<module>hibernate-mapping-2</module>
<!-- <module>hibernate-ogm</module>
<module>hibernate-annotations</module> FAILED -->
<module>hibernate-exceptions</module>
<!-- <module>hibernate-exceptions</module> FAILED -->
<module>hibernate-libraries</module>
<module>hibernate-jpa</module>
<!-- <module>hibernate-queries</module> FAILED -->
@ -114,10 +114,10 @@
<properties>
<!-- persistence -->
<hibernate.version>6.1.7.Final</hibernate.version>
<hibernate.version>6.2.0.Final</hibernate.version>
<postgresql.version>42.5.4</postgresql.version>
<hsqldb.version>2.3.4</hsqldb.version>
<testcontainers.version>1.16.3</testcontainers.version>
</properties>
</project>
</project>