From 1dfe7302550cbbbaeee821a83397cb0896f59beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20L=C3=B3pez?= Date: Fri, 6 Sep 2013 20:07:55 +0200 Subject: [PATCH] GRAILS-10335 - Override Grails mappings for collections when property is mapped with a custom Hibernate UserType --- ...rTypeOverridesGrailsMapMappingTests.groovy | 38 ++++++++++++ .../orm/hibernate/cfg/MapFakeUserType.groovy | 60 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 test/unit/org/codehaus/groovy/grails/orm/hibernate/CustomUserTypeOverridesGrailsMapMappingTests.groovy create mode 100644 test/unit/org/codehaus/groovy/grails/orm/hibernate/cfg/MapFakeUserType.groovy diff --git a/test/unit/org/codehaus/groovy/grails/orm/hibernate/CustomUserTypeOverridesGrailsMapMappingTests.groovy b/test/unit/org/codehaus/groovy/grails/orm/hibernate/CustomUserTypeOverridesGrailsMapMappingTests.groovy new file mode 100644 index 0000000..60cb510 --- /dev/null +++ b/test/unit/org/codehaus/groovy/grails/orm/hibernate/CustomUserTypeOverridesGrailsMapMappingTests.groovy @@ -0,0 +1,38 @@ +package org.codehaus.groovy.grails.orm.hibernate + +import grails.persistence.Entity + +import org.codehaus.groovy.grails.orm.hibernate.cfg.MapFakeUserType + +class CustomUserTypeOverridesGrailsMapMappingTests extends AbstractGrailsHibernateTests { + + void testUserTypeOverridesGrailsMapMappingTests() { + def d = new DomainUserTypeMappings() + + d.myMap = [foo:"bar"] + + assert d.save(flush:true) != null + + // the map should not be mapped onto a join table but instead a single column + session.connection().prepareStatement("select my_map from domain_user_type_mappings").execute() + + session.clear() + + d = DomainUserTypeMappings.get(d.id) + + assert d != null + } + + @Override protected getDomainClasses() { + [DomainUserTypeMappings] + } +} + +@Entity +class DomainUserTypeMappings { + Map myMap + + static mapping = { + myMap type:MapFakeUserType + } +} \ No newline at end of file diff --git a/test/unit/org/codehaus/groovy/grails/orm/hibernate/cfg/MapFakeUserType.groovy b/test/unit/org/codehaus/groovy/grails/orm/hibernate/cfg/MapFakeUserType.groovy new file mode 100644 index 0000000..5475cf9 --- /dev/null +++ b/test/unit/org/codehaus/groovy/grails/orm/hibernate/cfg/MapFakeUserType.groovy @@ -0,0 +1,60 @@ +package org.codehaus.groovy.grails.orm.hibernate.cfg + +import java.io.Serializable +import java.sql.PreparedStatement +import java.sql.ResultSet +import java.sql.SQLException +import java.sql.Types +import java.util.HashMap +import java.util.Map + +import org.hibernate.HibernateException +import org.hibernate.usertype.UserType + +public class MapFakeUserType implements UserType { + + int[] sqlTypes() { Types.VARCHAR } + Class returnedClass() { MapFakeUserType } + + public Object nullSafeGet(ResultSet rs, String[] names, owner) throws SQLException { + String name = rs.getString(names[0]) + rs.wasNull() ? null : new MapFakeUserType(name: name) + } + + public void nullSafeSet(PreparedStatement ps, value, int index) throws SQLException { + if (value == null) { + ps.setNull(index, Types.VARCHAR) + } + else { + ps.setString(index, value.name) + } + } + + public boolean equals(Object x, Object y) throws HibernateException { + return x == y; + } + + public int hashCode(Object x) throws HibernateException { + return x.hashCode(); + } + + public Object deepCopy(Object value) throws HibernateException { + return value; + } + + public boolean isMutable() { + return false; + } + + public Serializable disassemble(Object value) throws HibernateException { + return (Serializable)value; + } + + public Object assemble(Serializable cached, Object owner) throws HibernateException { + return cached; + } + + public Object replace(Object original, Object target, Object owner) throws HibernateException { + return original; + } +} \ No newline at end of file