From b8730c6bacb2d7f57ac5a70b003f272c3e373048 Mon Sep 17 00:00:00 2001 From: Manuel Unno Vio Date: Wed, 12 Feb 2014 15:07:48 +0100 Subject: [PATCH 1/4] Fixed Class Casts exceptions --- .../hibernate/usertype/HstoreType.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/java/net/kaleidos/hibernate/usertype/HstoreType.java b/src/java/net/kaleidos/hibernate/usertype/HstoreType.java index 761bfdb..e39c96c 100644 --- a/src/java/net/kaleidos/hibernate/usertype/HstoreType.java +++ b/src/java/net/kaleidos/hibernate/usertype/HstoreType.java @@ -6,6 +6,7 @@ import java.sql.SQLException; import java.sql.Types; import java.util.HashMap; +import java.util.TreeMap; import java.util.Map; import net.kaleidos.hibernate.postgresql.hstore.HstoreDomainType; @@ -39,8 +40,20 @@ public Class returnedClass() { @Override @SuppressWarnings("rawtypes") public boolean equals(Object x, Object y) throws HibernateException { - Map m1 = (Map) x; - Map m2 = ((HstoreDomainType)y).getDataStore(); + Map m1; + Map m2; + if (x instanceof HstoreDomainType){ + m1 = ((HstoreDomainType)x).getDataStore(); + } else { + m1 = (Map) x; + } + + if (y instanceof HstoreDomainType){ + m2 = ((HstoreDomainType)y).getDataStore(); + } else { + m2 = (Map) y; + } + return m1.equals(m2); } @@ -53,7 +66,12 @@ public int hashCode(Object x) throws HibernateException { @SuppressWarnings({ "rawtypes", "unchecked" }) public Object deepCopy(Object value) throws HibernateException { if (value != null) { - Map m = ((HstoreDomainType)value).getDataStore(); + Map m; + if (value instanceof HstoreDomainType) + m = ((HstoreDomainType)value).getDataStore(); + else + m = (Map)value; + if (m == null) { m = new HashMap(); } From 842b2aebc28a0c779df31307e94afb0aecc51a85 Mon Sep 17 00:00:00 2001 From: Manuel Unno Vio Date: Wed, 12 Feb 2014 15:10:02 +0100 Subject: [PATCH 2/4] Added test for instance delete --- ...stgresqlHstoreDomainIntegrationSpec.groovy | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/test/integration/net/kaleidos/hibernate/hstore/PostgresqlHstoreDomainIntegrationSpec.groovy b/test/integration/net/kaleidos/hibernate/hstore/PostgresqlHstoreDomainIntegrationSpec.groovy index 83cfa32..5d63a41 100644 --- a/test/integration/net/kaleidos/hibernate/hstore/PostgresqlHstoreDomainIntegrationSpec.groovy +++ b/test/integration/net/kaleidos/hibernate/hstore/PostgresqlHstoreDomainIntegrationSpec.groovy @@ -68,4 +68,22 @@ class PostgresqlHstoreDomainIntegrationSpec extends IntegrationSpec { ["foo,bar":"baz,qux"] | 'foo,bar' | 0 [foo:"bar"] | 'xxx' | 1 } -} \ No newline at end of file + + @Unroll + void 'save and delete a domain class with a map. key: #data'() { + setup: + def testHstore = new TestHstore(testAttributes: data) + when: 'I save an instance' + testHstore.save(flush: true) + and: 'I try to delete it' + testHstore.delete(flush: true) + + then: 'It shouldn\'t be present in database anymore' + TestHstore.count() == 0 + + where: + data | attribute | value + [foo:"bar"] | "foo" | "bar" + ["foo,bar":"baz,qux"] | "foo,bar" | "baz,qux" + } +} From 5b3409ac2cec60ba2af1d3a63f2f13c614a2c3ea Mon Sep 17 00:00:00 2001 From: Manuel Unno Vio Date: Wed, 12 Feb 2014 15:59:55 +0100 Subject: [PATCH 3/4] Fixed toString() method signature. Related to issue #25 --- src/java/net/kaleidos/hibernate/usertype/HstoreHelper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java/net/kaleidos/hibernate/usertype/HstoreHelper.java b/src/java/net/kaleidos/hibernate/usertype/HstoreHelper.java index 7f4e299..2b95cdc 100644 --- a/src/java/net/kaleidos/hibernate/usertype/HstoreHelper.java +++ b/src/java/net/kaleidos/hibernate/usertype/HstoreHelper.java @@ -19,7 +19,7 @@ private static String escapeQuotes(String text) { return text.replaceAll("\"", "'"); } - public static String toString(Map m) { + public static String toString(Map m) { if (m == null || m.isEmpty()) { return ""; } @@ -77,4 +77,4 @@ public static HstoreDomainType toMap(String s) { return new HstoreDomainType(m); } -} \ No newline at end of file +} From 92adee95a72dca2e22d55751f57e268919d0fd02 Mon Sep 17 00:00:00 2001 From: Manuel Unno Vio Date: Wed, 12 Feb 2014 16:51:03 +0100 Subject: [PATCH 4/4] Fixes Map type in order to reflect HstoreHelper.toString() signature change. --- .../criterion/hstore/PgHstoreOperatorExpression.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java/net/kaleidos/hibernate/criterion/hstore/PgHstoreOperatorExpression.java b/src/java/net/kaleidos/hibernate/criterion/hstore/PgHstoreOperatorExpression.java index 9709631..f22982b 100644 --- a/src/java/net/kaleidos/hibernate/criterion/hstore/PgHstoreOperatorExpression.java +++ b/src/java/net/kaleidos/hibernate/criterion/hstore/PgHstoreOperatorExpression.java @@ -21,12 +21,12 @@ public class PgHstoreOperatorExpression implements Criterion { private static final long serialVersionUID = 2872183637309166619L; private final String propertyName; - private final Map value; + private final Map value; private final String operator; private static final TypedValue[] NO_VALUES = new TypedValue[0]; - protected PgHstoreOperatorExpression(String propertyName, Map value, String operator) { + protected PgHstoreOperatorExpression(String propertyName, Map value, String operator) { this.propertyName = propertyName; this.value = value; this.operator = operator;