Merge pull request #5947 from eelhazati/master

Hibernate 5 bootstrap API Test.
This commit is contained in:
Loredana Crusoveanu 2018-12-24 18:10:49 +02:00 committed by GitHub
commit 45c9532cfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package com.baeldung.hibernate.bootstrap;
import com.baeldung.hibernate.pojo.Movie;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertNotNull;
public class BootstrapAPIIntegrationTest {
SessionFactory sessionFactory = null;
@Test
public void whenServiceRegistryAndMetadata_thenSessionFactory() throws IOException {
BootstrapServiceRegistry bootstrapRegistry = new BootstrapServiceRegistryBuilder()
.build();
ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder(bootstrapRegistry)
// No need for hibernate.cfg.xml file, an hibernate.properties is sufficient.
//.configure()
.build();
MetadataSources metadataSources = new MetadataSources(standardRegistry);
metadataSources.addAnnotatedClass(Movie.class);
Metadata metadata = metadataSources.getMetadataBuilder().build();
sessionFactory = metadata.buildSessionFactory();
assertNotNull(sessionFactory);
sessionFactory.close();
}
@After
public void clean() throws IOException {
sessionFactory.close();
}
}