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; 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 +} 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(); } 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" + } +}