public Configuration(BootstrapServiceRegistry serviceRegistry) { this.bootstrapServiceRegistry = serviceRegistry; this.metadataSources = new MetadataSources( serviceRegistry ); reset(); }
/** * Create a new LocalSessionFactoryBuilder for the given DataSource. * @param dataSource the JDBC DataSource that the resulting Hibernate SessionFactory should be using * (may be {@code null}) * @param resourceLoader the ResourceLoader to load application classes from */ public LocalSessionFactoryBuilder(@Nullable DataSource dataSource, ResourceLoader resourceLoader) { this(dataSource, resourceLoader, new MetadataSources( new BootstrapServiceRegistryBuilder().applyClassLoader(resourceLoader.getClassLoader()).build())); }
/** * Determine the Hibernate {@link MetadataSources} to use. * <p>Can also be externally called to initialize and pre-populate a {@link MetadataSources} * instance which is then going to be used for {@link SessionFactory} building. * @return the MetadataSources to use (never {@code null}) * @since 4.3 * @see LocalSessionFactoryBuilder#LocalSessionFactoryBuilder(DataSource, ResourceLoader, MetadataSources) */ public MetadataSources getMetadataSources() { this.metadataSourcesAccessed = true; if (this.metadataSources == null) { BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder(); if (this.resourcePatternResolver != null) { builder = builder.applyClassLoader(this.resourcePatternResolver.getClassLoader()); } if (this.hibernateIntegrators != null) { for (Integrator integrator : this.hibernateIntegrators) { builder = builder.applyIntegrator(integrator); } } this.metadataSources = new MetadataSources(builder.build()); } return this.metadataSources; }
private Metadata buildMappings(StandardServiceRegistry registry) { return new MetadataSources( registry ) .addAnnotatedClass( MyEntity.class ) .buildMetadata(); }
public static SessionFactoryImplementor buildStandardSessionFactory(Consumer<StandardServiceRegistryBuilder> additionalSettings) { final StandardServiceRegistryBuilder ssrb = getStandardServiceRegistryBuilder(); additionalSettings.accept( ssrb ); final StandardServiceRegistry ssr = ssrb.build(); return (SessionFactoryImplementor) new MetadataSources( ssr ).buildMetadata().buildSessionFactory(); }
private SessionFactory buildSessionFactory() { Map settings = new HashMap(); settings.putAll( Environment.getProperties() ); TestingJtaBootstrap.prepare( settings ); settings.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" ); final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySettings( settings ).build(); return new MetadataSources( ssr ) .addAnnotatedClass( TestEntity.class ) .buildMetadata() .buildSessionFactory(); }
@Before public void setUp() throws Exception { ssr = new StandardServiceRegistryBuilder() .applySetting( Environment.HBM2DDL_AUTO, "none" ) .build(); output = File.createTempFile( "update_script", ".sql" ); output.deleteOnExit(); metadata = (MetadataImplementor) new MetadataSources( ssr ) .addAnnotatedClass( TestEntity.class ) .buildMetadata(); metadata.validate(); }
@Before public void setUp() throws Exception { ssr = new StandardServiceRegistryBuilder() .applySetting( Environment.HBM2DDL_AUTO, "none" ) .build(); output = File.createTempFile( "update_script", ".sql" ); output.deleteOnExit(); metadata = (MetadataImplementor) new MetadataSources( ssr ) .addAnnotatedClass( TestEntity.class ) .buildMetadata(); metadata.validate(); }
@Test public void testWithoutCustomNamingStrategy() throws Exception { new MetadataSources( serviceRegistry ) .addAnnotatedClass( Address.class ) .addAnnotatedClass( Person.class ) .buildMetadata(); } }
@Test public void testHbmXmlHandling() { final Metadata metadata = new MetadataSources( ssr ) .addResource( "org/hibernate/test/namingstrategy/synchronizedTables/mapping.hbm.xml" ) .getMetadataBuilder() .applyPhysicalNamingStrategy( TestingPhysicalNamingStrategy.INSTANCE ) .build(); verify( metadata.getEntityBinding( DynamicEntity.class.getName() ) ); }
@Before public void setUp(){ ssr = new StandardServiceRegistryBuilder().build(); metadata = new MetadataSources( ssr ) .addAnnotatedClass( User.class ) .buildMetadata(); }
@Before public void setUp() { serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() ); metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ) .addResource( "org/hibernate/test/schemaupdate/mapping.hbm.xml" ) .buildMetadata(); metadata.validate(); new SchemaExport().drop( EnumSet.of( TargetType.DATABASE, TargetType.STDOUT ), metadata ); }
@Test @TestForIssue( jiraKey = "HHH-9485" ) public void testInheritedAttributeOverridingEntity() { Metadata metadata = new MetadataSources( standardServiceRegistry ) .addAnnotatedClass( C.class ) .addAnnotatedClass( D.class ) .buildMetadata(); ( (MetadataImplementor) metadata ).validate(); }
@Test @TestForIssue( jiraKey = "HHH-9485" ) public void testInheritedAttributeOverridingMappedsuperclass() { Metadata metadata = new MetadataSources( standardServiceRegistry ) .addAnnotatedClass( A.class ) .addAnnotatedClass( B.class ) .buildMetadata(); ( (MetadataImplementor) metadata ).validate(); }
@Before public void setUp() { serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() ); metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ) .addAnnotatedClass( MyEntity.class ).buildMetadata(); metadata.validate(); }
@Test public void testBindingEntityWithIdAndFormula() { try { new MetadataSources( ssr ) .addAnnotatedClass( EntityWithIdAndFormula.class ) .buildMetadata(); fail( "Expecting failure from invalid mapping" ); } catch (CannotForceNonNullableException e) { assertThat( e.getMessage(), startsWith( "Identifier property [" ) ); } }
@Test public void testMissingSuper() { try { Metadata metadata = new MetadataSources( serviceRegistry ) .addResource( getBaseForMappings() + "extendshbm/Customer.hbm.xml" ) .addResource( getBaseForMappings() + "extendshbm/Employee.hbm.xml" ) .buildMetadata(); fail( "Should not be able to build sessionFactory without a Person" ); } catch ( HibernateException e ) { } }
private void verifyPkNameUsed(String mappingResource, String expectedName) { final Metadata metadata = new MetadataSources( ssr ) .addResource( mappingResource ) .buildMetadata(); final JournalingSchemaToolingTarget target = new JournalingSchemaToolingTarget(); new SchemaCreatorImpl( ssr ).doCreation( metadata, false, target ); assertTrue( "Expected foreign-key name [" + expectedName + "] not seen in schema creation output", target.containedText( expectedName ) ); }
@Test public void testEntityNamesWithPackage() { Metadata metadata = new MetadataSources( serviceRegistry ) .addResource( getBaseForMappings() + "extendshbm/packageentitynames.hbm.xml" ) .buildMetadata(); assertNotNull( metadata.getEntityBinding( "EntityHasName" ) ); assertNotNull( metadata.getEntityBinding( "EntityCompany" ) ); }
@Test public void testAllSeparateInOne() { Metadata metadata = new MetadataSources( serviceRegistry ) .addResource( getBaseForMappings() + "extendshbm/allseparateinone.hbm.xml" ) .buildMetadata(); assertNotNull( metadata.getEntityBinding( "org.hibernate.test.extendshbm.Customer" ) ); assertNotNull( metadata.getEntityBinding( "org.hibernate.test.extendshbm.Person" ) ); assertNotNull( metadata.getEntityBinding( "org.hibernate.test.extendshbm.Employee" ) ); }