Search in sources :

Example 31 with MetadataSources

use of org.hibernate.boot.MetadataSources in project hibernate-orm by hibernate.

the class WrongCircularityDetectionTest method testNoCircularityDetection.

@Test
public void testNoCircularityDetection() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        final Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(Entity1.class).addAnnotatedClass(Entity2.class).buildMetadata();
        org.hibernate.mapping.Table entity1Table = metadata.getEntityBinding(Entity1.class.getName()).getTable();
        org.hibernate.mapping.Table entity2Table = metadata.getEntityBinding(Entity2.class.getName()).getTable();
        assertTrue(entity1Table.getName().equals(entity2Table.getName()));
        assertFalse(entity1Table.getSchema().equals(entity2Table.getSchema()));
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 32 with MetadataSources

use of org.hibernate.boot.MetadataSources in project hibernate-orm by hibernate.

the class AndLobTest method testMappingAttributeWithLobAndAttributeConverter.

@Test
public void testMappingAttributeWithLobAndAttributeConverter() {
    final Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(EntityImpl.class).buildMetadata();
    final Type type = metadata.getEntityBinding(EntityImpl.class.getName()).getProperty("status").getType();
    final AttributeConverterTypeAdapter concreteType = assertTyping(AttributeConverterTypeAdapter.class, type);
    assertEquals(Types.BLOB, concreteType.getSqlTypeDescriptor().getSqlType());
}
Also used : Type(org.hibernate.type.Type) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) Test(org.junit.Test)

Example 33 with MetadataSources

use of org.hibernate.boot.MetadataSources in project hibernate-orm by hibernate.

the class AttributeConverterTest method testBasicOperation.

@Test
public void testBasicOperation() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).buildMetadata();
        SimpleValue simpleValue = new SimpleValue(metadata);
        simpleValue.setJpaAttributeConverterDescriptor(new AttributeConverterDescriptorNonAutoApplicableImpl(new StringClobConverter()));
        simpleValue.setTypeUsingReflection(IrrelevantEntity.class.getName(), "name");
        Type type = simpleValue.getType();
        assertNotNull(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);
    }
}
Also used : BasicType(org.hibernate.type.BasicType) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) Type(org.hibernate.type.Type) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) IrrelevantEntity(org.hibernate.IrrelevantEntity) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) AttributeConverterDescriptorNonAutoApplicableImpl(org.hibernate.boot.internal.AttributeConverterDescriptorNonAutoApplicableImpl) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.Test)

Example 34 with MetadataSources

use of org.hibernate.boot.MetadataSources in project hibernate-orm by hibernate.

the class AttributeConverterTest method testEnumConverter.

@Test
@TestForIssue(jiraKey = "HHH-8866")
public void testEnumConverter() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
    try {
        MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(EntityWithConvertibleField.class).getMetadataBuilder().applyAttributeConverter(ConvertibleEnumConverter.class, true).build();
        // first lets validate that the converter was applied...
        PersistentClass tester = metadata.getEntityBinding(EntityWithConvertibleField.class.getName());
        Property nameProp = tester.getProperty("convertibleEnum");
        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);
        assertTyping(EnumJavaTypeDescriptor.class, basicType.getJavaTypeDescriptor());
        assertEquals(Types.VARCHAR, basicType.getSqlTypeDescriptor().getSqlType());
        // then lets build the SF and verify its use...
        final SessionFactory sf = metadata.buildSessionFactory();
        try {
            Session s = sf.openSession();
            s.getTransaction().begin();
            EntityWithConvertibleField entity = new EntityWithConvertibleField();
            entity.setId("ID");
            entity.setConvertibleEnum(ConvertibleEnum.VALUE);
            String entityID = entity.getId();
            s.persist(entity);
            s.getTransaction().commit();
            s.close();
            s = sf.openSession();
            s.beginTransaction();
            entity = (EntityWithConvertibleField) s.load(EntityWithConvertibleField.class, entityID);
            assertEquals(ConvertibleEnum.VALUE, entity.getConvertibleEnum());
            s.getTransaction().commit();
            s.close();
            JavaConstantNode javaConstantNode = new JavaConstantNode();
            javaConstantNode.setExpectedType(type);
            javaConstantNode.setSessionFactory((SessionFactoryImplementor) sf);
            javaConstantNode.setText("org.hibernate.test.converter.AttributeConverterTest$ConvertibleEnum.VALUE");
            final String outcome = javaConstantNode.getRenderText((SessionFactoryImplementor) sf);
            assertEquals("'VALUE'", outcome);
            s = sf.openSession();
            s.beginTransaction();
            s.createQuery("FROM EntityWithConvertibleField e where e.convertibleEnum = org.hibernate.test.converter.AttributeConverterTest$ConvertibleEnum.VALUE").list();
            s.getTransaction().commit();
            s.close();
            s = sf.openSession();
            s.beginTransaction();
            s.delete(entity);
            s.getTransaction().commit();
            s.close();
        } finally {
            try {
                sf.close();
            } catch (Exception ignore) {
            }
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) JavaConstantNode(org.hibernate.hql.internal.ast.tree.JavaConstantNode) AnnotationException(org.hibernate.AnnotationException) SimpleValue(org.hibernate.mapping.SimpleValue) BasicType(org.hibernate.type.BasicType) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) Type(org.hibernate.type.Type) Property(org.hibernate.mapping.Property) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 35 with MetadataSources

use of org.hibernate.boot.MetadataSources in project hibernate-orm by hibernate.

the class CollectionCompositeElementExplicitConversionTest method testCollectionOfEmbeddablesWithConvertedAttributes.

@Test
public void testCollectionOfEmbeddablesWithConvertedAttributes() throws Exception {
    final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Disguise.class).addAnnotatedClass(Traits.class).buildMetadata();
    metadata.validate();
    final PersistentClass entityBinding = metadata.getEntityBinding(Disguise.class.getName());
    // first check the singular composite...
    final Property singularTraitsProperty = entityBinding.getProperty("singularTraits");
    checkComposite((Component) singularTraitsProperty.getValue());
    // then check the plural composite...
    final Property pluralTraitsProperty = entityBinding.getProperty("pluralTraits");
    checkComposite((Component) ((org.hibernate.mapping.Set) pluralTraitsProperty.getValue()).getElement());
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Aggregations

MetadataSources (org.hibernate.boot.MetadataSources)189 Test (org.junit.Test)133 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)105 Metadata (org.hibernate.boot.Metadata)87 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)73 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)58 TestForIssue (org.hibernate.testing.TestForIssue)47 PersistentClass (org.hibernate.mapping.PersistentClass)36 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)31 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)19 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)18 ServiceRegistry (org.hibernate.service.ServiceRegistry)18 Before (org.junit.Before)18 Property (org.hibernate.mapping.Property)17 File (java.io.File)14 SchemaCreatorImpl (org.hibernate.tool.schema.internal.SchemaCreatorImpl)14 Map (java.util.Map)13 MetadataBuilder (org.hibernate.boot.MetadataBuilder)12 Type (org.hibernate.type.Type)12 SimpleValue (org.hibernate.mapping.SimpleValue)11