diff --git a/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/hilo/RestaurantOrder.java b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/hilo/RestaurantOrder.java new file mode 100644 index 0000000000..03d7edeae9 --- /dev/null +++ b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/hilo/RestaurantOrder.java @@ -0,0 +1,34 @@ +package com.baeldung.hibernate.hilo; + +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.annotations.Parameter; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class RestaurantOrder { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hilo_sequence_generator") + @GenericGenerator( + name = "hilo_sequence_generator", + strategy = "sequence", + parameters = { + @Parameter(name = "sequence_name", value = "hilo_seqeunce"), + @Parameter(name = "initial_value", value = "1"), + @Parameter(name = "increment_size", value = "3"), + @Parameter(name = "optimizer", value = "hilo") + } + ) + private Long id; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } +} diff --git a/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/hilo/HibernateHiloUnitTest.java b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/hilo/HibernateHiloUnitTest.java new file mode 100644 index 0000000000..9285c30af5 --- /dev/null +++ b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/hilo/HibernateHiloUnitTest.java @@ -0,0 +1,99 @@ +package com.baeldung.hibernate.hilo; + +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.SessionFactoryBuilder; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.service.ServiceRegistry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URL; +import java.util.Properties; + +import static org.junit.Assert.fail; + +public class HibernateHiloUnitTest { + private Session session; + + @Before + public void init() { + try { + configureLogger(); + + ServiceRegistry serviceRegistry = configureServiceRegistry(); + SessionFactory factory = getSessionFactoryBuilder(serviceRegistry).build(); + session = factory.openSession(); + } catch (HibernateException | IOException e) { + fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]"); + } + } + + private void configureLogger() { + BasicConfigurator.configure(); + LogManager.getLogger("org.hibernate").setLevel(Level.ERROR); + LogManager.getLogger("org.hibernate.id.enhanced.SequenceStructure").setLevel(Level.DEBUG); + LogManager.getLogger("org.hibernate.event.internal.AbstractSaveEventListener").setLevel(Level.DEBUG); + LogManager.getLogger("org.hibernate.SQL").setLevel(Level.DEBUG); + } + + + private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) { + MetadataSources metadataSources = new MetadataSources(serviceRegistry); + metadataSources.addAnnotatedClass(RestaurantOrder.class); + Metadata metadata = metadataSources.buildMetadata(); + + return metadata.getSessionFactoryBuilder(); + } + + private static ServiceRegistry configureServiceRegistry() throws IOException { + Properties properties = getProperties(); + + return new StandardServiceRegistryBuilder().applySettings(properties) + .build(); + } + + private static Properties getProperties() throws IOException { + Properties properties = new Properties(); + URL propertiesURL = getPropertiesURL(); + + try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) { + properties.load(inputStream); + } + + return properties; + } + + private static URL getPropertiesURL() { + return Thread.currentThread() + .getContextClassLoader() + .getResource("hibernate-hilo.properties"); + } + + @Test + public void givenHiLoAlgorithm_when9EntitiesArePersisted_Then3callsToDBForNextValueShouldBeMade() { + Transaction transaction = session.beginTransaction(); + + for (int i = 0; i < 9; i++) { + session.persist(new RestaurantOrder()); + session.flush(); + } + + transaction.commit(); + } + + @After + public void cleanup() { + session.close(); + } +} diff --git a/persistence-modules/hibernate5/src/test/resources/hibernate-hilo.properties b/persistence-modules/hibernate5/src/test/resources/hibernate-hilo.properties new file mode 100644 index 0000000000..60d487c1bd --- /dev/null +++ b/persistence-modules/hibernate5/src/test/resources/hibernate-hilo.properties @@ -0,0 +1,10 @@ +hibernate.connection.driver_class=org.h2.Driver +hibernate.connection.url=jdbc:h2:mem:hilo_db;DB_CLOSE_DELAY=-1 +hibernate.connection.username=sa +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=create-drop +hibernate.c3p0.min_size=5 +hibernate.c3p0.max_size=20 +hibernate.c3p0.acquire_increment=5 +hibernate.c3p0.timeout=1800