@Test public void testWithCustomNamingStrategy() throws Exception { new MetadataSources( serviceRegistry ) .addAnnotatedClass(Address.class) .addAnnotatedClass(Person.class) .getMetadataBuilder() .applyPhysicalNamingStrategy( new DummyNamingStrategy() ) .build(); }
private Metadata buildMappings(StandardServiceRegistry registry) { return new MetadataSources( registry ) .addAnnotatedClass( MyEntity.class ) .buildMetadata(); }
@Test public void testNonExistentOrmVersion() { try { new MetadataSources() .addResource( "org/hibernate/test/annotations/xml/ejb3/orm5.xml" ) .buildMetadata(); fail( "Expecting failure due to unsupported xsd version" ); } catch ( InvalidMappingException expected ) { } catch ( UnsupportedOrmXsdVersionException expected ) { } } }
private static class JpaEntityNotFoundDelegate implements EntityNotFoundDelegate, Serializable { /** * Singleton access */ public static final JpaEntityNotFoundDelegate INSTANCE = new JpaEntityNotFoundDelegate(); public void handleEntityNotFound(String entityName, Serializable id) { throw new EntityNotFoundException( "Unable to find " + entityName + " with id " + id ); } }
protected void applyMetadataSources(MetadataSources sources) { for ( String mapping : getMappings() ) { sources.addResource( getBaseForMappings() + mapping ); } for ( Class annotatedClass : getAnnotatedClasses() ) { sources.addAnnotatedClass( annotatedClass ); } for ( String annotatedPackage : getAnnotatedPackages() ) { sources.addPackage( annotatedPackage ); } for ( String ormXmlFile : getXmlFiles() ) { sources.addInputStream( Thread.currentThread().getContextClassLoader().getResourceAsStream( ormXmlFile ) ); } }
@Test @TestForIssue( jiraKey = "HHH-9908" ) public void testCollectionJoinTableNamingBase() { // really the same as the JPA compliant tests; here we just pick up the default ImplicitNamingStrategy final MetadataSources metadataSources = new MetadataSources(); try { metadataSources.addAnnotatedClass( Input.class ); metadataSources.addAnnotatedClass( Ptx.class ); final Metadata metadata = metadataSources.getMetadataBuilder() .build(); assertSameTableUsed( metadata ); } finally { ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry(); if(metaServiceRegistry instanceof BootstrapServiceRegistry ) { BootstrapServiceRegistryBuilder.destroy( metaServiceRegistry ); } } }
@Test public void testIt() { MetadataSources metadataSources = new MetadataSources() .addResource("org/hibernate/test/hbm/version/Mappings.hbm.xml"); try { metadataSources.buildMetadata(); } finally { ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry(); if(metaServiceRegistry instanceof BootstrapServiceRegistry ) { BootstrapServiceRegistryBuilder.destroy( metaServiceRegistry ); } } } }
new StandardServiceRegistryBuilder().build(); MetadataSources sources = new MetadataSources( standardRegistry ); sources.addAnnotatedClass( MyEntity.class ); sources.addAnnotatedClassName( "org.hibernate.example.Customer" ); sources.addPackage( "hibernate.example" ); sources.addPackage( MyEntity.class.getPackage() ); sources.addResource( "org/hibernate/example/Order.hbm.xml" ); sources.addResource( "org/hibernate/example/Product.orm.xml" ); sources.addDirectory( new File( ".") ); sources.addFile( new File( "./mapping.xml") ); sources.addJar( new File( "./entities.jar") ); sources.addClass( MyEntity.class );
@Test public void test_basic_custom_type_register_BasicType_example() { try { //tag::basic-custom-type-register-BasicType-example[] ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().build(); MetadataSources sources = new MetadataSources( standardRegistry ); MetadataBuilder metadataBuilder = sources.getMetadataBuilder(); metadataBuilder.applyBasicType( BitSetType.INSTANCE ); //end::basic-custom-type-register-BasicType-example[] } catch (Exception ignore) { } }
new StandardServiceRegistryBuilder().build(); MetadataSources sources = new MetadataSources( standardRegistry ) .addAnnotatedClass( MyEntity.class ) .addAnnotatedClassName( "org.hibernate.example.Customer" ) .addResource( "org/hibernate/example/Order.hbm.xml" ) .addResource( "org/hibernate/example/Product.orm.xml" ); new StandardServiceRegistryBuilder().build(); MetadataSources sources = new MetadataSources( standardRegistry ); MetadataBuilder metadataBuilder = sources.getMetadataBuilder();
private static MetadataImplementor buildMetadata( CommandLineArgs parsedArgs, StandardServiceRegistry serviceRegistry) throws Exception { final MetadataSources metadataSources = new MetadataSources(serviceRegistry); for ( String filename : parsedArgs.hbmXmlFiles ) { metadataSources.addFile( filename ); } for ( String filename : parsedArgs.jarFiles ) { metadataSources.addJar( new File( filename ) ); } final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder(); final StrategySelector strategySelector = serviceRegistry.getService( StrategySelector.class ); if ( parsedArgs.implicitNamingStrategy != null ) { metadataBuilder.applyImplicitNamingStrategy( strategySelector.resolveStrategy( ImplicitNamingStrategy.class, parsedArgs.implicitNamingStrategy ) ); } if ( parsedArgs.physicalNamingStrategy != null ) { metadataBuilder.applyPhysicalNamingStrategy( strategySelector.resolveStrategy( PhysicalNamingStrategy.class, parsedArgs.physicalNamingStrategy ) ); } return (MetadataImplementor) metadataBuilder.build(); }
@Test @TestForIssue(jiraKey = "HHH-8462") public void testBasicOrmXmlConverterApplication() { final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build(); try { MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) .addAnnotatedClass( Tester.class ) .addURL( ConfigHelper.findAsResource( "org/hibernate/test/converter/orm.xml" ) ) .getMetadataBuilder() .build(); PersistentClass tester = metadata.getEntityBinding( Tester.class.getName() ); Property nameProp = tester.getProperty( "name" ); SimpleValue nameValue = (SimpleValue) nameProp.getValue(); Type type = nameValue.getType(); assertNotNull( type ); if ( !AttributeConverterTypeAdapter.class.isInstance( type ) ) { fail( "AttributeConverter not applied" ); } AttributeConverterTypeAdapter basicType = assertTyping( AttributeConverterTypeAdapter.class, type ); assertSame( StringTypeDescriptor.INSTANCE, basicType.getJavaTypeDescriptor() ); assertEquals( Types.CLOB, basicType.getSqlTypeDescriptor().getSqlType() ); } finally { StandardServiceRegistryBuilder.destroy( ssr ); } }
@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() ) ); }
@Test public void doTest() { final MetadataSources metadataSources = new MetadataSources(); try { applySources( metadataSources ); final Metadata metadata = metadataSources.getMetadataBuilder() .applyImplicitNamingStrategy( getImplicitNamingStrategyToUse() ) .build(); validateCustomer( metadata ); validateOrder( metadata ); validateZipCode( metadata ); validateCustomerRegisteredTrademarks( metadata ); validateCustomerAddresses( metadata ); validateCustomerOrders( metadata ); validateCustomerIndustries( metadata ); } finally { ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry(); if(metaServiceRegistry instanceof BootstrapServiceRegistry ) { BootstrapServiceRegistryBuilder.destroy( metaServiceRegistry ); } } }
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(); }
/** * 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())); }
@Override protected void applyMetadataSources(MetadataSources sources) { super.applyMetadataSources( sources ); sources.addAnnotatedClass( AddressCapable.class ); sources.addAnnotatedClass( AddressCapable2.class ); sources.addAnnotatedClass( Address.class ); sources.addAnnotatedClass( Address2.class ); }
/** * Short-hand form of calling {@link #getMetadataBuilder()} and using its * {@link org.hibernate.boot.MetadataBuilder#build()} method in cases where the application wants * to accept the defaults. * * @return The built metadata. */ public Metadata buildMetadata() { return getMetadataBuilder().build(); }
public void apply(MetadataSources metadataSources) { switch ( getType() ) { case RESOURCE: { metadataSources.addResource( getReference() ); break; } case CLASS: { metadataSources.addAnnotatedClassName( getReference() ); break; } case FILE: { metadataSources.addFile( getReference() ); break; } case PACKAGE: { metadataSources.addPackage( getReference() ); break; } case JAR: { metadataSources.addJar( new File( getReference() ) ); break; } } } }
/** * Read mappings as a application resourceName (i.e. classpath lookup) * trying different class loaders. * * @param resourceName The resource name * @return this (for method chaining purposes) * @throws MappingException Indicates problems locating the resource or * processing the contained mapping document. */ public Configuration addResource(String resourceName) throws MappingException { metadataSources.addResource( resourceName ); return this; }