Search in sources :

Example 6 with AttributeConverterTypeAdapter

use of org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter in project hibernate-orm by hibernate.

the class ParameterizedAttributeConverterParameterTypeTest method testNestedTypeParameterAutoApplication.

@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());
        assertTyping(StringListConverter.class, type.getAttributeConverter());
    }
    {
        Property prop = pc.getProperty("someIntegers");
        AttributeConverterTypeAdapter type = assertTyping(AttributeConverterTypeAdapter.class, prop.getType());
        assertTyping(IntegerListConverter.class, type.getAttributeConverter());
    }
}
Also used : Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 7 with AttributeConverterTypeAdapter

use of org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter in project hibernate-orm by hibernate.

the class SimpleConvertAnnotationTest method testSimpleConvertUsage.

@Test
public void testSimpleConvertUsage() throws MalformedURLException {
    final EntityPersister ep = sessionFactory().getEntityPersister(Entity1.class.getName());
    final Type websitePropertyType = ep.getPropertyType("website");
    final AttributeConverterTypeAdapter type = assertTyping(AttributeConverterTypeAdapter.class, websitePropertyType);
    assertTyping(UrlConverter.class, type.getAttributeConverter());
    resetFlags();
    Session session = openSession();
    session.getTransaction().begin();
    session.persist(new Entity1(1, "1", new URL("http://hibernate.org")));
    session.getTransaction().commit();
    session.close();
    assertTrue(convertToDatabaseColumnCalled);
    session = openSession();
    session.getTransaction().begin();
    session.createQuery("delete Entity1").executeUpdate();
    session.getTransaction().commit();
    session.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Type(org.hibernate.type.Type) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) URL(java.net.URL) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with AttributeConverterTypeAdapter

use of org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter in project hibernate-orm by hibernate.

the class SimpleXmlOverriddenTest method baseline.

/**
	 * A baseline test, with an explicit @Convert annotation that should be in effect
	 */
@Test
public void baseline() {
    Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata();
    PersistentClass pc = metadata.getEntityBinding(TheEntity.class.getName());
    Type type = pc.getProperty("it").getType();
    AttributeConverterTypeAdapter adapter = assertTyping(AttributeConverterTypeAdapter.class, type);
    assertTyping(SillyStringConverter.class, adapter.getAttributeConverter());
}
Also used : StringType(org.hibernate.type.StringType) Type(org.hibernate.type.Type) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Example 9 with AttributeConverterTypeAdapter

use of org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter in project hibernate-orm by hibernate.

the class XmlWithExplicitConvertAnnotationsTest method testSimpleConvertUsage.

@Test
public void testSimpleConvertUsage() throws MalformedURLException {
    final EntityPersister ep = sessionFactory().getEntityPersister(Entity1.class.getName());
    final Type theDatePropertyType = ep.getPropertyType("theDate");
    final AttributeConverterTypeAdapter type = assertTyping(AttributeConverterTypeAdapter.class, theDatePropertyType);
    assertTyping(LongToDateConverter.class, type.getAttributeConverter());
    resetFlags();
    Session session = openSession();
    session.getTransaction().begin();
    session.persist(new Entity1(1, "1", new Date()));
    session.getTransaction().commit();
    session.close();
    assertTrue(convertToDatabaseColumnCalled);
    resetFlags();
    session = openSession();
    session.getTransaction().begin();
    session.get(Entity1.class, 1);
    session.getTransaction().commit();
    session.close();
    assertTrue(convertToEntityAttributeCalled);
    session = openSession();
    session.getTransaction().begin();
    session.createQuery("delete Entity1").executeUpdate();
    session.getTransaction().commit();
    session.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Type(org.hibernate.type.Type) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Example 10 with AttributeConverterTypeAdapter

use of org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter 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)

Aggregations

AttributeConverterTypeAdapter (org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter)13 Type (org.hibernate.type.Type)10 Test (org.junit.Test)10 Session (org.hibernate.Session)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)6 MetadataSources (org.hibernate.boot.MetadataSources)4 Metadata (org.hibernate.boot.Metadata)3 PersistentClass (org.hibernate.mapping.PersistentClass)3 URL (java.net.URL)2 Date (java.util.Date)2 Property (org.hibernate.mapping.Property)2 TestForIssue (org.hibernate.testing.TestForIssue)2 QueryException (org.hibernate.QueryException)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)1 Dialect (org.hibernate.dialect.Dialect)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 SimpleValue (org.hibernate.mapping.SimpleValue)1 AbstractStandardBasicType (org.hibernate.type.AbstractStandardBasicType)1