Ur doin it wrong. The transforming field marshaller needs to operate on F (the
non-persistent type), and transform in getFromSet/writeToStatement, rather than operate on T (the persistent type) and transform in getFromObject/writeToObject. This makes the newly added test in TransformTest work, rather than fail, as it did previously.
This commit is contained in:
@@ -112,8 +112,8 @@ public abstract class FieldMarshaller<T>
|
||||
}
|
||||
}
|
||||
|
||||
protected static <F,T> FieldMarshaller<T> createTransformingMarshaller (
|
||||
final Transformer<F,T> xformer, Field field, Transform annotation)
|
||||
protected static <F,T> FieldMarshaller<F> createTransformingMarshaller (
|
||||
final Transformer<F,T> xformer, final Field field, Transform annotation)
|
||||
{
|
||||
Class<?> pojoType = getTransformerType(xformer, "from");
|
||||
checkArgument(pojoType.isAssignableFrom(field.getType()),
|
||||
@@ -125,28 +125,28 @@ public abstract class FieldMarshaller<T>
|
||||
(FieldMarshaller<T>)createMarshaller(getTransformerType(xformer, "to"));
|
||||
delegate.create(field);
|
||||
|
||||
FieldMarshaller<T> xmarsh = new FieldMarshaller<T>() {
|
||||
FieldMarshaller<F> xmarsh = new FieldMarshaller<F>() {
|
||||
@Override public String getColumnType (ColumnTyper typer, int length) {
|
||||
return delegate.getColumnType(typer, length);
|
||||
}
|
||||
@Override public T getFromObject (Object po)
|
||||
@Override public F getFromObject (Object po)
|
||||
throws IllegalArgumentException, IllegalAccessException {
|
||||
@SuppressWarnings("unchecked") F value = (F)_field.get(po);
|
||||
return xformer.toPersistent(value);
|
||||
return value;
|
||||
}
|
||||
@Override public T getFromSet (ResultSet rs) throws SQLException {
|
||||
return delegate.getFromSet(rs);
|
||||
@Override public F getFromSet (ResultSet rs) throws SQLException {
|
||||
return xformer.fromPersistent(delegate.getFromSet(rs));
|
||||
}
|
||||
@Override public T getFromSet (ResultSet rs, int index) throws SQLException {
|
||||
return delegate.getFromSet(rs, index);
|
||||
@Override public F getFromSet (ResultSet rs, int index) throws SQLException {
|
||||
return xformer.fromPersistent(delegate.getFromSet(rs, index));
|
||||
}
|
||||
@Override public void writeToObject (Object po, T value)
|
||||
@Override public void writeToObject (Object po, F value)
|
||||
throws IllegalArgumentException, IllegalAccessException {
|
||||
_field.set(po, xformer.fromPersistent(value));
|
||||
_field.set(po, value);
|
||||
}
|
||||
@Override public void writeToStatement (PreparedStatement ps, int column, T value)
|
||||
@Override public void writeToStatement (PreparedStatement ps, int column, F value)
|
||||
throws SQLException {
|
||||
delegate.writeToStatement(ps, column, value);
|
||||
delegate.writeToStatement(ps, column, xformer.toPersistent(value));
|
||||
}
|
||||
};
|
||||
xmarsh.create(field);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id$
|
||||
// $Id: TransformTest.java 760 2010-12-09 00:08:58Z samskivert $
|
||||
//
|
||||
// Depot library - a Java relational persistence library
|
||||
// Copyright (C) 2006-2010 Michael Bayne and Pär Winzell
|
||||
@@ -230,7 +230,16 @@ public class TransformTest extends TestBase
|
||||
testCreateReadDelete(new String[] { "", "\n", "", "\n\n" });
|
||||
}
|
||||
|
||||
protected void testCreateReadDelete (String[] strings)
|
||||
@Test public void testProject ()
|
||||
{
|
||||
TransformRecord in = createAndInsert(new String[] { "one", "two", "three" });
|
||||
Set<ExtraOrdinal> bobs = _repo.from(TransformRecord.class)
|
||||
.where(TransformRecord.RECORD_ID.eq(in.recordId))
|
||||
.load(TransformRecord.BOBS);
|
||||
assertEquals(in.bobs, bobs);
|
||||
}
|
||||
|
||||
protected TransformRecord createAndInsert (String[] strings)
|
||||
{
|
||||
TransformRecord in = new TransformRecord();
|
||||
in.recordId = 1;
|
||||
@@ -241,6 +250,17 @@ public class TransformTest extends TestBase
|
||||
in.ordinal = Ordinal.THREE;
|
||||
in.bobs = EnumSet.of(ExtraOrdinal.TWO);
|
||||
_repo.insert(in);
|
||||
return in;
|
||||
}
|
||||
|
||||
protected void delete (TransformRecord in)
|
||||
{
|
||||
_repo.delete(TransformRecord.getKey(in.recordId));
|
||||
}
|
||||
|
||||
protected void testCreateReadDelete (String[] strings)
|
||||
{
|
||||
TransformRecord in = createAndInsert(strings);
|
||||
|
||||
TransformRecord out = _repo.loadNoCache(in.recordId);
|
||||
assertNotNull(out != null); // we got a result
|
||||
@@ -256,7 +276,7 @@ public class TransformTest extends TestBase
|
||||
assertEquals(in.bobs, out.bobs);
|
||||
|
||||
// finally clean up after ourselves
|
||||
_repo.delete(TransformRecord.getKey(in.recordId));
|
||||
delete(in);
|
||||
assertNull(_repo.loadNoCache(in.recordId));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user