diff --git a/persistence-modules/hibernate-sessionfactory/pom.xml b/persistence-modules/hibernate-sessionfactory/pom.xml new file mode 100644 index 000000000000..6549f8bad67b --- /dev/null +++ b/persistence-modules/hibernate-sessionfactory/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + hibernate-sessionfactory + 0.0.1-SNAPSHOT + hibernate-sessionfactory + + + com.baeldung + persistence-modules + 1.0.0-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter-jdbc + 3.2.4 + + + + org.hibernate.orm + hibernate-core + 6.4.4.Final + + + com.h2database + h2 + 2.2.224 + runtime + + + org.springframework.boot + spring-boot-starter-test + 3.2.4 + test + + + + diff --git a/persistence-modules/hibernate-sessionfactory/src/main/java/com/baeldung/HibernateSessionFactoryDemoApplication.java b/persistence-modules/hibernate-sessionfactory/src/main/java/com/baeldung/HibernateSessionFactoryDemoApplication.java new file mode 100644 index 000000000000..993036ab3c39 --- /dev/null +++ b/persistence-modules/hibernate-sessionfactory/src/main/java/com/baeldung/HibernateSessionFactoryDemoApplication.java @@ -0,0 +1,20 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; +import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration; + +@SpringBootApplication( + exclude = { + HibernateJpaAutoConfiguration.class, + JpaRepositoriesAutoConfiguration.class + } +) +@ComponentScan(basePackages="com.baeldung") +public class HibernateSessionFactoryDemoApplication { + public static void main(String[] args) { + SpringApplication.run(HibernateSessionFactoryDemoApplication.class, args); + } +} diff --git a/persistence-modules/hibernate-sessionfactory/src/main/java/com/baeldung/config/HibernateConfig.java b/persistence-modules/hibernate-sessionfactory/src/main/java/com/baeldung/config/HibernateConfig.java new file mode 100644 index 000000000000..065d0b59535e --- /dev/null +++ b/persistence-modules/hibernate-sessionfactory/src/main/java/com/baeldung/config/HibernateConfig.java @@ -0,0 +1,32 @@ +package com.baeldung.config; + +import javax.sql.DataSource; +import org.hibernate.SessionFactory; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import java.util.HashMap; +import java.util.Map; + +@Configuration +public class HibernateConfig { + + @Bean + SessionFactory sessionFactory(DataSource dataSource) { + StandardServiceRegistry registry = + new StandardServiceRegistryBuilder() + .applySetting("hibernate.connection.datasource", dataSource) + .applySetting("hibernate.hbm2ddl.auto", "create-drop") + .applySetting("hibernate.show_sql", true) + .build(); + + MetadataSources sources = + new MetadataSources(registry); + + return sources.buildMetadata().buildSessionFactory(); + } + +} diff --git a/persistence-modules/hibernate-sessionfactory/src/main/resources/application.properties b/persistence-modules/hibernate-sessionfactory/src/main/resources/application.properties new file mode 100644 index 000000000000..351b92f8cfff --- /dev/null +++ b/persistence-modules/hibernate-sessionfactory/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=Hibernate SessionFactory Demo \ No newline at end of file diff --git a/persistence-modules/hibernate-sessionfactory/src/test/java/com/baeldung/HibernateSessionFactoryDemoApplicationTests.java b/persistence-modules/hibernate-sessionfactory/src/test/java/com/baeldung/HibernateSessionFactoryDemoApplicationTests.java new file mode 100644 index 000000000000..5a4e2b643e67 --- /dev/null +++ b/persistence-modules/hibernate-sessionfactory/src/test/java/com/baeldung/HibernateSessionFactoryDemoApplicationTests.java @@ -0,0 +1,13 @@ +package com.baeldung; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class HibernateSessionFactoryDemoApplicationTests { + + @Test + void contextLoads() { + } + +} \ No newline at end of file diff --git a/persistence-modules/hibernate-sessionfactory/src/test/java/com/baeldung/SessionFactoryUnitTest.java b/persistence-modules/hibernate-sessionfactory/src/test/java/com/baeldung/SessionFactoryUnitTest.java new file mode 100644 index 000000000000..dc5b07d038f9 --- /dev/null +++ b/persistence-modules/hibernate-sessionfactory/src/test/java/com/baeldung/SessionFactoryUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@SpringBootTest +public class SessionFactoryUnitTest { + + @Autowired + private SessionFactory sessionFactory; + + @Test + void givenSessionFactory_whenOpeningSession_thenSessionIsCreated() { + + try (Session session = sessionFactory.openSession()) { + assertNotNull(session); + } + } +} \ No newline at end of file diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 6d1a4325eb6c..01cb3da41aec 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -146,6 +146,7 @@ spring-boot-persistence-5 hibernate-annotations-2 hibernate-reactive + hibernate-sessionfactory spring-data-envers spring-persistence-simple