Search in sources :

Example 11 with AttributeConverterTypeAdapter

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

the class ExplicitDateConvertersTest 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 12 with AttributeConverterTypeAdapter

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

the class SimpleConvertsAnnotationTest method testSimpleConvertsUsage.

@Test
public void testSimpleConvertsUsage() 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 13 with AttributeConverterTypeAdapter

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

the class JavaConstantNode method getRenderText.

@Override
@SuppressWarnings("unchecked")
public String getRenderText(SessionFactoryImplementor sessionFactory) {
    final Type type = expectedType == null ? heuristicType : Number.class.isAssignableFrom(heuristicType.getReturnedClass()) ? heuristicType : expectedType;
    try {
        if (LiteralType.class.isInstance(type)) {
            final LiteralType literalType = (LiteralType) type;
            final Dialect dialect = factory.getDialect();
            return literalType.objectToSQLString(constantValue, dialect);
        } else if (AttributeConverterTypeAdapter.class.isInstance(type)) {
            final AttributeConverterTypeAdapter converterType = (AttributeConverterTypeAdapter) type;
            if (!converterType.getModelType().isInstance(constantValue)) {
                throw new QueryException(String.format(Locale.ENGLISH, "Recognized query constant expression [%s] was not resolved to type [%s] expected by defined AttributeConverter [%s]", constantExpression, constantValue.getClass().getName(), converterType.getModelType().getName()));
            }
            final Object value = converterType.getAttributeConverter().convertToDatabaseColumn(constantValue);
            if (String.class.equals(converterType.getJdbcType())) {
                return "'" + value + "'";
            } else {
                return value.toString();
            }
        } else {
            throw new QueryException(String.format(Locale.ENGLISH, "Unrecognized Hibernate Type for handling query constant (%s); expecting LiteralType implementation or AttributeConverter", constantExpression));
        }
    } catch (QueryException e) {
        throw e;
    } catch (Exception t) {
        throw new QueryException(QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + constantExpression, t);
    }
}
Also used : LiteralType(org.hibernate.type.LiteralType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) Dialect(org.hibernate.dialect.Dialect) LiteralType(org.hibernate.type.LiteralType) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) QueryException(org.hibernate.QueryException)

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