@Override protected void addConfigOptions(Map options) { options.put( EntityManagerFactoryBuilderImpl.METADATA_BUILDER_CONTRIBUTOR, (MetadataBuilderContributor) metadataBuilder -> metadataBuilder.applyAttributeConverter( YearMonthAttributeConverter.class ) ); }
metadataBuilder.applyAttributeConverter( attributeConverterDefinition );
@Test @TestForIssue( jiraKey = "HHH-10050" ) public void testNestedTypeParameterAutoApplication() { final Metadata metadata = new MetadataSources( ssr ) .addAnnotatedClass( SampleEntity.class ) .getMetadataBuilder() .applyAttributeConverter( IntegerListConverter.class ) .applyAttributeConverter( StringListConverter.class ) .build(); // lets make sure the auto-apply converters were applied properly... PersistentClass pc = metadata.getEntityBinding( SampleEntity.class.getName() ); { Property prop = pc.getProperty( "someStrings" ); AttributeConverterTypeAdapter type = assertTyping( AttributeConverterTypeAdapter.class, prop.getType() ); assertTrue( StringListConverter.class.isAssignableFrom( type.getAttributeConverter().getConverterJavaTypeDescriptor().getJavaType() ) ); } { Property prop = pc.getProperty( "someIntegers" ); AttributeConverterTypeAdapter type = assertTyping( AttributeConverterTypeAdapter.class, prop.getType() ); assertTrue( IntegerListConverter.class.isAssignableFrom( type.getAttributeConverter().getConverterJavaTypeDescriptor().getJavaType() ) ); } }
@Test public void testNonAutoApplyHandling() { final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build(); try { MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) .addAnnotatedClass( Tester.class ) .getMetadataBuilder() .applyAttributeConverter( NotAutoAppliedConverter.class, false ) .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 with autoApply=false was auto applied" ); } } finally { StandardServiceRegistryBuilder.destroy( ssr ); } }
@Test @TestForIssue( jiraKey = "HHH-9042" ) public void testMetamodelBuilding() { StandardServiceRegistry ssr = new StandardServiceRegistryBuilder() .applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" ) .build(); try { Metadata metadata = new MetadataSources( ssr ) .addAnnotatedClass( Person.class ) .getMetadataBuilder() .applyAttributeConverter( SexConverter.class ) .build(); ( (MetadataImpl) metadata ).validate(); PersistentClass personBinding = metadata.getEntityBinding( Person.class.getName() ); assertNotNull( personBinding ); PersistentClass personAuditBinding = metadata.getEntityBinding( Person.class.getName() + "_AUD" ); assertNotNull( personAuditBinding ); } finally { StandardServiceRegistryBuilder.destroy( ssr ); } } }
.addAnnotatedClass( EntityWithConvertibleField.class ) .getMetadataBuilder() .applyAttributeConverter( ConvertibleEnumConverter.class, true ) .build();
@Test public void testBasicConverterDisableApplication() { final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build(); try { MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) .addAnnotatedClass( Tester2.class ) .getMetadataBuilder() .applyAttributeConverter( StringClobConverter.class, true ) .build(); PersistentClass tester = metadata.getEntityBinding( Tester2.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 applied (should not have been)" ); } AbstractStandardBasicType basicType = assertTyping( AbstractStandardBasicType.class, type ); assertSame( StringTypeDescriptor.INSTANCE, basicType.getJavaTypeDescriptor() ); assertEquals( Types.VARCHAR, basicType.getSqlTypeDescriptor().getSqlType() ); } finally { StandardServiceRegistryBuilder.destroy( ssr ); } }
@Test public void testBasicConverterApplication() { final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build(); try { MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) .addAnnotatedClass( Tester.class ) .getMetadataBuilder() .applyAttributeConverter( StringClobConverter.class, true ) .build(); PersistentClass tester = metadata.getEntityBinding( Tester.class.getName() ); Property nameProp = tester.getProperty( "name" ); SimpleValue nameValue = (SimpleValue) nameProp.getValue(); Type type = nameValue.getType(); assertNotNull( type ); assertTyping( BasicType.class, type ); if ( !AttributeConverterTypeAdapter.class.isInstance( type ) ) { fail( "AttributeConverter not applied" ); } AbstractStandardBasicType basicType = assertTyping( AbstractStandardBasicType.class, type ); assertSame( StringTypeDescriptor.INSTANCE, basicType.getJavaTypeDescriptor() ); assertEquals( Types.CLOB, basicType.getSqlTypeDescriptor().getSqlType() ); } finally { StandardServiceRegistryBuilder.destroy( ssr ); } }
metadataBuilder.applyAttributeConverter( myAttributeConverter );
@Override protected void additionalProperties(Properties properties) { properties.put("hibernate.metadata_builder_contributor", (MetadataBuilderContributor) metadataBuilder -> metadataBuilder.applyAttributeConverter(YearMonthIntegerAttributeConverter.class) ); }
metadataBuilder.applyAttributeConverter( attributeConverterDefinition );