Search in sources :

Example 1 with AbstractStandardBasicType

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

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

the class AttributeConverterTest method testBasicOperation.

@Test
public void testBasicOperation() {
    SimpleValue simpleValue = new SimpleValue(new MetadataBuildingContextTestingImpl());
    simpleValue.setJpaAttributeConverterDescriptor(new InstanceBasedConverterDescriptor(new StringClobConverter(), new ClassmateContext()));
    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());
}
Also used : MetadataBuildingContextTestingImpl(org.hibernate.testing.boot.MetadataBuildingContextTestingImpl) BasicType(org.hibernate.type.BasicType) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) Type(org.hibernate.type.Type) IrrelevantEntity(org.hibernate.IrrelevantEntity) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) InstanceBasedConverterDescriptor(org.hibernate.boot.model.convert.internal.InstanceBasedConverterDescriptor) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) SimpleValue(org.hibernate.mapping.SimpleValue) ClassmateContext(org.hibernate.boot.internal.ClassmateContext) Test(org.junit.Test)

Example 3 with AbstractStandardBasicType

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

the class Java8DateTimeTests method dump.

private void dump(PersistentClass entityBinding, TheEntity theEntity) {
    final Iterator propertyBindingIterator = entityBinding.getPropertyClosureIterator();
    while (propertyBindingIterator.hasNext()) {
        final Property propertyBinding = (Property) propertyBindingIterator.next();
        final JavaTypeDescriptor javaTypeDescriptor = ((AbstractStandardBasicType) propertyBinding.getType()).getJavaTypeDescriptor();
        System.out.println(String.format("%s (%s) -> %s", propertyBinding.getName(), javaTypeDescriptor.getJavaType().getSimpleName(), javaTypeDescriptor.toString(propertyBinding.getGetter(TheEntity.class).get(theEntity))));
    }
}
Also used : Iterator(java.util.Iterator) Property(org.hibernate.mapping.Property) JavaTypeDescriptor(org.hibernate.type.descriptor.java.JavaTypeDescriptor) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType)

Example 4 with AbstractStandardBasicType

use of org.hibernate.type.AbstractStandardBasicType 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)

Example 5 with AbstractStandardBasicType

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

the class AttributeConverterTest method testBasicConverterApplication.

@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);
    }
}
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) 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

AbstractStandardBasicType (org.hibernate.type.AbstractStandardBasicType)5 Property (org.hibernate.mapping.Property)4 SimpleValue (org.hibernate.mapping.SimpleValue)4 BasicType (org.hibernate.type.BasicType)4 Type (org.hibernate.type.Type)4 Test (org.junit.Test)4 MetadataSources (org.hibernate.boot.MetadataSources)3 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)3 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)3 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)3 PersistentClass (org.hibernate.mapping.PersistentClass)3 AttributeConverterTypeAdapter (org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter)3 Iterator (java.util.Iterator)1 AnnotationException (org.hibernate.AnnotationException)1 IrrelevantEntity (org.hibernate.IrrelevantEntity)1 Session (org.hibernate.Session)1 SessionFactory (org.hibernate.SessionFactory)1 ClassmateContext (org.hibernate.boot.internal.ClassmateContext)1 InstanceBasedConverterDescriptor (org.hibernate.boot.model.convert.internal.InstanceBasedConverterDescriptor)1 JavaConstantNode (org.hibernate.hql.internal.ast.tree.JavaConstantNode)1