|
| 1 | +package com.baeldung.hibernate.bootstrap; |
| 2 | + |
| 3 | +import com.baeldung.hibernate.pojo.Movie; |
| 4 | +import org.hibernate.SessionFactory; |
| 5 | +import org.hibernate.boot.Metadata; |
| 6 | +import org.hibernate.boot.MetadataSources; |
| 7 | +import org.hibernate.boot.registry.BootstrapServiceRegistry; |
| 8 | +import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; |
| 9 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 10 | +import org.hibernate.service.ServiceRegistry; |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertNotNull; |
| 17 | + |
| 18 | +public class BootstrapAPIIntegrationTest { |
| 19 | + |
| 20 | + SessionFactory sessionFactory = null; |
| 21 | + |
| 22 | + @Test |
| 23 | + public void whenServiceRegistryAndMetadata_thenSessionFactory() throws IOException { |
| 24 | + |
| 25 | + BootstrapServiceRegistry bootstrapRegistry = new BootstrapServiceRegistryBuilder() |
| 26 | + .build(); |
| 27 | + |
| 28 | + ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder(bootstrapRegistry) |
| 29 | + // No need for hibernate.cfg.xml file, an hibernate.properties is sufficient. |
| 30 | + //.configure() |
| 31 | + .build(); |
| 32 | + |
| 33 | + MetadataSources metadataSources = new MetadataSources(standardRegistry); |
| 34 | + metadataSources.addAnnotatedClass(Movie.class); |
| 35 | + |
| 36 | + Metadata metadata = metadataSources.getMetadataBuilder().build(); |
| 37 | + |
| 38 | + sessionFactory = metadata.buildSessionFactory(); |
| 39 | + assertNotNull(sessionFactory); |
| 40 | + sessionFactory.close(); |
| 41 | + } |
| 42 | + |
| 43 | + @After |
| 44 | + public void clean() throws IOException { |
| 45 | + sessionFactory.close(); |
| 46 | + } |
| 47 | +} |
0 commit comments