Permalink
Browse files

Correction for GitHub #5

Allow to fetch @Text field on H2 Database : JdbcClob object are now read instead of set directly into a String.
  • Loading branch information...
dwursteisen committed Dec 10, 2011
1 parent 7558c47 commit 46b2fc4d21ee13575d9652844d69cd74675b798b
@@ -216,7 +216,7 @@ public static Object fromObject(Field field, Object value) {
throw new SienaException(e);
}
}
-
+
if(field.getAnnotation(Embedded.class) != null && value != null && java.sql.Clob.class.isAssignableFrom(value.getClass())) {
java.sql.Clob clob = (java.sql.Clob)value;
try {
@@ -225,7 +225,19 @@ public static Object fromObject(Field field, Object value) {
} catch (SQLException e) {
throw new SienaException(e);
}
- }
+ }
+
+ // issue https://github.com/mandubian/siena/issues/5
+ if (value != null && java.sql.Clob.class.isAssignableFrom(value.getClass())) {
+ java.sql.Clob clob = (java.sql.Clob) value;
+ try {
+ // @see http://osdir.com/ml/h2-database/2011-06/msg00170.html
+ return clob.getSubString(1, (int) clob.length());
+ } catch (SQLException e) {
+ throw new SienaException(e);
+ }
+ }
+
if(field.isAnnotationPresent(Polymorphic.class)){
try {
@@ -54,6 +54,7 @@
import siena.base.test.model.PersonStringID;
import siena.base.test.model.PersonUUID;
import siena.base.test.model.PolymorphicModel;
+import siena.base.test.model.TextModel;
import siena.base.test.model.TransactionAccountFrom;
import siena.base.test.model.TransactionAccountTo;
import siena.core.PersistenceManagerLifeCycleWrapper;
@@ -126,6 +127,7 @@ protected void setUp() throws Exception {
classes.add(BigDecimalDoubleModel.class);
classes.add(TransactionAccountFrom.class);
classes.add(TransactionAccountTo.class);
+ classes.add(TextModel.class);
pm = createPersistenceManager(classes);
@@ -15,6 +15,7 @@
import siena.Query;
import siena.base.test.model.Discovery4Search;
import siena.base.test.model.Discovery4Search2;
+import siena.base.test.model.TextModel;
import siena.jdbc.H2PersistenceManager;
import siena.jdbc.PostgresqlPersistenceManager;
import siena.jdbc.ddl.DdlGenerator;
@@ -1728,4 +1729,16 @@ public void testTransactionSaveBatchFailure() {
super.testTransactionSaveBatchFailure();
}
+ public void testTextModel() {
+ TextModel model = new TextModel();
+ model.text = "Lorum Ipsum";
+
+ pm.insert(model);
+
+ Query<TextModel> query = pm.createQuery(TextModel.class);
+
+ List<TextModel> models = pm.fetch(query);
+ assertEquals(1, models.size());
+ assertEquals("Lorum Ipsum", models.get(0).text);
+ }
}
@@ -0,0 +1,18 @@
+package siena.base.test.model;
+
+import siena.*;
+
+/**
+ * User: Wursteisen David
+ * Date: 10/12/11
+ * Time: 13:15
+ */
+@Table("text_model")
+public class TextModel extends Model {
+
+ @Id(Generator.AUTO_INCREMENT)
+ public Long id;
+
+ @Text
+ public String text;
+}

0 comments on commit 46b2fc4

Please sign in to comment.