use of org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder in project tapestry-5 by apache.
the class HibernateModule method contributeValueEncoderSource.
/**
* Contributes {@link ValueEncoderFactory}s for all registered Hibernate entity classes. Encoding and decoding are
* based on the id property value of the entity using type coercion. Hence, if the id can be coerced to a String and
* back then the entity can be coerced.
*/
@SuppressWarnings("unchecked")
public static void contributeValueEncoderSource(MappedConfiguration<Class, ValueEncoderFactory> configuration, @Symbol(HibernateSymbols.PROVIDE_ENTITY_VALUE_ENCODERS) boolean provideEncoders, final HibernateSessionSource sessionSource, final Session session, final TypeCoercer typeCoercer, final PropertyAccess propertyAccess, final LoggerSource loggerSource) {
if (!provideEncoders)
return;
Set<EntityType<?>> entities = sessionSource.getSessionFactory().getMetamodel().getEntities();
for (EntityType<?> entityType : entities) {
Class<?> entityClass = entityType.getJavaType();
if (entityClass != null) {
SingularAttribute<?, ?> id = entityType.getId(entityType.getIdType().getJavaType());
final String idenfierPropertyName = id.getName();
ValueEncoderFactory factory = new ValueEncoderFactory() {
@Override
public ValueEncoder create(Class type) {
return new HibernateEntityValueEncoder(entityClass, idenfierPropertyName, session, propertyAccess, typeCoercer, loggerSource.getLogger(entityClass));
}
};
configuration.add(entityClass, factory);
}
}
}
use of org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder in project tapestry-5 by apache.
the class HibernateEntityValueEncoderTest method to_client_id_null.
@Test
public void to_client_id_null() {
Session session = mockSession();
Logger logger = mockLogger();
replay();
SampleEntity entity = new SampleEntity();
HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(SampleEntity.class, "id", session, access, typeCoercer, logger);
assertNull(encoder.toClient(entity));
verify();
}
use of org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder in project tapestry-5 by apache.
the class HibernateEntityValueEncoderTest method to_value_not_found.
@Test
public void to_value_not_found() {
Session session = mockSession();
Logger logger = mockLogger();
expect(session.get(SampleEntity.class, new Long(12345))).andReturn(null);
logger.error("Unable to convert client value '12345' into an entity instance.");
replay();
SampleEntity entity = new SampleEntity();
HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(SampleEntity.class, "id", session, access, typeCoercer, logger);
assertNull(encoder.toValue("12345"));
verify();
}
use of org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder in project tapestry-5 by apache.
the class HibernateEntityValueEncoderTest method to_value_bad_type_coercion.
@Test
public void to_value_bad_type_coercion() {
Session session = mockSession();
Logger logger = mockLogger();
replay();
HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(SampleEntity.class, "id", session, access, typeCoercer, logger);
try {
encoder.toValue("xyz");
unreachable();
} catch (RuntimeException ex) {
assertMessageContains(ex, "Exception converting 'xyz' to instance of java.lang.Long (id type for entity org.apache.tapestry5.internal.hibernate.SampleEntity)");
}
assertNull(encoder.toValue(""));
verify();
}
Aggregations