Search in sources :

Example 6 with BasicType

use of org.hibernate.type.BasicType 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 7 with BasicType

use of org.hibernate.type.BasicType 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 8 with BasicType

use of org.hibernate.type.BasicType in project hibernate-orm by hibernate.

the class BasicMetadataGenerator method addBasic.

@SuppressWarnings({ "unchecked" })
boolean addBasic(Element parent, PropertyAuditingData propertyAuditingData, Value value, SimpleMapperBuilder mapper, boolean insertable, boolean key) {
    final Type type = value.getType();
    if (type instanceof BasicType || type instanceof SerializableToBlobType || "org.hibernate.type.PrimitiveByteArrayBlobType".equals(type.getClass().getName())) {
        if (parent != null) {
            final boolean addNestedType = (value instanceof SimpleValue) && ((SimpleValue) value).getTypeParameters() != null;
            String typeName = type.getName();
            if (typeName == null) {
                typeName = type.getClass().getName();
            }
            final Element propMapping = MetadataTools.addProperty(parent, propertyAuditingData.getName(), addNestedType ? null : typeName, propertyAuditingData.isForceInsertable() || insertable, key);
            MetadataTools.addColumns(propMapping, value.getColumnIterator());
            if (addNestedType) {
                final Properties typeParameters = ((SimpleValue) value).getTypeParameters();
                final Element typeMapping = propMapping.addElement("type");
                typeMapping.addAttribute("name", typeName);
                if ("org.hibernate.type.EnumType".equals(typeName)) {
                    // Proper handling of enumeration type
                    mapEnumerationType(typeMapping, type, typeParameters);
                } else {
                    // By default copying all Hibernate properties
                    for (Object object : typeParameters.keySet()) {
                        final String keyType = (String) object;
                        final String property = typeParameters.getProperty(keyType);
                        if (property != null) {
                            typeMapping.addElement("param").addAttribute("name", keyType).setText(property);
                        }
                    }
                }
            }
        }
        // A null mapper means that we only want to add xml mappings
        if (mapper != null) {
            mapper.add(propertyAuditingData.getPropertyData());
        }
    } else {
        return false;
    }
    return true;
}
Also used : BasicType(org.hibernate.type.BasicType) CustomType(org.hibernate.type.CustomType) SerializableToBlobType(org.hibernate.type.SerializableToBlobType) EnumType(org.hibernate.type.EnumType) Type(org.hibernate.type.Type) BasicType(org.hibernate.type.BasicType) SerializableToBlobType(org.hibernate.type.SerializableToBlobType) Element(org.dom4j.Element) Properties(java.util.Properties) SimpleValue(org.hibernate.mapping.SimpleValue)

Example 9 with BasicType

use of org.hibernate.type.BasicType in project hibernate-orm by hibernate.

the class AttributeConverterTest method testBasicOrmXmlConverterApplication.

@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);
    }
}
Also used : BasicType(org.hibernate.type.BasicType) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) Type(org.hibernate.type.Type) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) Property(org.hibernate.mapping.Property) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 10 with BasicType

use of org.hibernate.type.BasicType in project hibernate-orm by hibernate.

the class AttributeConverterTest method testBasicConverterDisableApplication.

@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);
    }
}
Also used : BasicType(org.hibernate.type.BasicType) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) Type(org.hibernate.type.Type) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) Property(org.hibernate.mapping.Property) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.Test)

Aggregations

BasicType (org.hibernate.type.BasicType)13 Test (org.junit.Test)10 SimpleValue (org.hibernate.mapping.SimpleValue)7 Type (org.hibernate.type.Type)6 MetadataSources (org.hibernate.boot.MetadataSources)5 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)5 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)5 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)5 PersistentClass (org.hibernate.mapping.PersistentClass)5 Property (org.hibernate.mapping.Property)5 AbstractStandardBasicType (org.hibernate.type.AbstractStandardBasicType)5 BasicTypeRegistry (org.hibernate.type.BasicTypeRegistry)4 AttributeConverterTypeAdapter (org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter)4 TestForIssue (org.hibernate.testing.TestForIssue)3 AbstractSingleColumnStandardBasicType (org.hibernate.type.AbstractSingleColumnStandardBasicType)3 Map (java.util.Map)2 SessionFactory (org.hibernate.SessionFactory)2 TypeContributor (org.hibernate.boot.model.TypeContributor)2 CustomType (org.hibernate.type.CustomType)2 HashMap (java.util.HashMap)1