Repository navigation
fix(hibernate5): honour type: 'text' on the elements of a Map, List or Set - #17270
borinquenkid wants to merge 9 commits into
Conversation
…ment columns A mapped type: 'text' on a basic collection failed with "user collection type class not found: text" because the type name was handed to Hibernate as a user collection type. Basic collections no longer name a collection type, and the element column now honors maxSize and inList. Relates to #16897 Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…pe, not the length from maxSize or inList The maxSize of a collection bounds the number of its elements, and its inList lists whole collections, so neither is the length of an element. A List of String named body with maxSize 2 is valid with the value ['abcdef'], because it has one element, and saving it failed on a varchar(2); an inList of lists, such as [['abcdef']], was read as element strings and stopped the mapping from being built. The column of the elements of a list, a set or a map is left to the type the mapping gives it: type text is mapped as text, and the length stays what it was otherwise. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
There was a problem hiding this comment.
🟡 Changes recommended
The list-labelled fixtures default to sets, leaving Hibernate’s distinct list/index binding path untested in the save and inList cases.
1 open issue
What changed in this PR
Fixes Hibernate 5 basic-collection mappings on 7.0.x so type: 'text' applies to collection elements without causing metadata initialization to fail. This complements the Hibernate 7 fix for #16897.
Changes:
- Prevent basic collections from treating the mapped element type as a user collection type.
- Add regression checks for element types, column metadata, map keys, and collection constraints.
| File | Description |
|---|---|
grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/MapElementColumnSpec.groovy |
Adds collection mapping and constraint regression tests. |
grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java |
Returns no collection-level type name for basic collections. |
Review effort: Balanced
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A collection type that was applied before is dropped.
| */ | ||
| public String getTypeName(ToMany property) { | ||
| if (property instanceof Basic) { | ||
| return null; |
There was a problem hiding this comment.
A map is given a collection type. That type should be used. The default map type is used instead, because this returns nothing for a basic collection. Keep the given type.
There was a problem hiding this comment.
@jamesfredley You are right. On 7.0.x a Map that names a user collection type starts with that type as its collection type, and #17270 dropped it for every collection of simple values. Only a name that is not a user collection type, such as text, names no collection type now. MapElementColumnSpec checks that a Map with a user collection type keeps it and that text names none; the first fails without the change. Pushed in 4853e24.
…names A map or list of simple values that names a user collection type used it as its collection type, and the elements as their type. The change that lets type 'text' reach the elements dropped the collection type for every basic collection. Only a name that is not a user collection type names no collection type now. The list fixtures of the spec declare a List, since hasMany alone declares a Set. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A collection type that was applied before is dropped when this loader cannot see it.
| } | ||
| try { | ||
| ClassLoader loader = Thread.currentThread().getContextClassLoader(); | ||
| return UserCollectionType.class.isAssignableFrom(Class.forName(typeName, false, loader != null ? loader : CollectionType.class.getClassLoader())); |
There was a problem hiding this comment.
A map is given a collection type. That type should be used. The default map type is used, because this loader cannot see the type and the given class is dropped. Keep the given type.
There was a problem hiding this comment.
@jamesfredley You are right, and the lookup by name is gone: the mapping already holds the class, so it is checked with isAssignableFrom and no class is loaded, whichever loader is current. A type given as a name, such as 'text', names no collection type. MapElementColumnSpec runs a Map with a user collection type under a context class loader that sees no class of the application and checks the type is kept; it fails without the change. Pushed in 0413208.
…from the mapping, not from a loader A basic collection kept its mapped type as the collection type only when the name could be loaded through the context class loader. A thread whose loader could not see the application dropped a user collection type that the mapping names as a class. The mapping already holds the class, so it is checked with isAssignableFrom and no class is loaded by name. A type given as a name, such as 'text', names no collection type. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A collection type given as a class name is dropped.
| } | ||
|
|
||
| private static boolean namesUserCollectionType(PropertyConfig config) { | ||
| return config != null && config.getType() instanceof Class<?> && UserCollectionType.class.isAssignableFrom((Class<?>) config.getType()); |
There was a problem hiding this comment.
A basic collection is given a collection type as a class name. That type should be used. The default collection is used, because this accepts only a class and not a name. Read the name too.
There was a problem hiding this comment.
@jamesfredley @sanjana2505006 Confirmed and fixed. I ran a map with type: 'com.acme.MyType' as a name on 7.0.x and on the previous head: 7.0.x applies it as the collection type, the previous head dropped it, and the class form works on both. A name now names no collection type only when Hibernate's registry knows it as a basic type, as text is; any other name is a class name and is kept, as on 7.0.x. MapElementColumnSpec has a row for a name (it fails without the change), a Set row for type: 'text', and a save of a value of 300 characters into a map of text elements. Pushed in 4d13815.
|
+1 to James's last point. Could you also add a test with the user collection type given as a string, and a |
…names as a class name The previous commit kept a user collection type only when the mapping holds the class, so a type given as the name of its class was dropped, where 7.0.x applies it. A name now names no collection type only when Hibernate's registry knows it as a basic type, as 'text' is; any other name is a class name and is kept, as on 7.0.x. Adds a row for a name, for a Set with type: 'text', and a save of a value longer than 255 characters into a map of text elements. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A collection type is dropped when the same name is also a type of an element.
| return UserCollectionType.class.isAssignableFrom(typeClass); | ||
| } | ||
| // a name: Hibernate's registry says whether it is a basic type, as 'text' is; any other name is a class | ||
| return mappings.getTypeConfiguration().getBasicTypeRegistry().getRegisteredType(type.toString()) == null; |
There was a problem hiding this comment.
A basic map is given a collection type by name. That name is also registered as the type of an element. The collection type should be kept. It is dropped, because this treats the element name as proof the collection type does not exist. Keep the collection type.
There was a problem hiding this comment.
@jamesfredley This is the rule that @sanjana2505006 proposed on 12-02, keep the name unless it is a registered basic type like text, and 4d13815 implements it. No change is pushed.
A name that Hibernate's registry knows as a basic type is the type of the element. type: 'text' is that case, and applying it as the collection type is what broke the metadata build in this pull request's issue. Keeping the name as a collection type as well would bring that failure back.
A name that is both a registered element type and the class of a user collection type cannot be told from the first case without loading the class by name. That lookup is gone since 0413208, because the loader that is current at that point may not see the class.
If you have such a name, please post it with its element type and say whether its class implements UserCollectionType, and I will add the row.
There was a problem hiding this comment.
A name that Hibernate registers as a basic type is read as the type of the elements, so a String name that is both a registered basic type and the class name of a user collection type is not kept as the collection type. I did not make that case an exception: telling the two apart needs the class loaded by name, and an earlier round on this PR removed that on purpose (a thread of another module does not see the application's classes).
A user collection type is kept when it is named as a class (type: MyMapType), as the class name of a class that is not a registered basic type, or with a 'user-type' mapping, and the spec has a row for each. If you have a mapping that registers a basic type under the same name as a collection type class, tell me which and I will add the row. I added a sentence to the description.
jdaugherty
left a comment
There was a problem hiding this comment.
The fix does what it claims: type: 'text' on a Map, List or Set of simple values fails the metadata build on 7.0.x and maps the elements as text here, and a user collection type given as a class or as a class name is kept. One regression against 7.0.x remains, inline: a user collection type registered with a 'user-type' mapping is dropped.
| */ | ||
| public String getTypeName(ToMany property, InFlightMetadataCollector mappings) { | ||
| String typeName = binder.getTypeName(property, binder.getPropertyConfig(property), getMapping(property.getOwner())); | ||
| if (property instanceof Basic && !namesUserCollectionType(binder.getPropertyConfig(property), mappings)) { |
There was a problem hiding this comment.
Regression against 7.0.x: a user collection type registered with a 'user-type' mapping is dropped for a Map, List or Set of simple values.
class Book {
Map<String, String> body
static mapping = { 'user-type'(type: MyMapType, class: Map) }
}On 7.0.x the collection type of body is MyMapType (a CustomCollectionType); here it is the default MapType. The same happens for a Set with 'user-type'(type: MySetType, class: Set).
namesUserCollectionType reads only the type of the property, while binder.getTypeName on the line above has already resolved the name from the property or from the user-type mapping. Deciding from that resolved name covers both, and removes the helper:
String typeName = binder.getTypeName(property, binder.getPropertyConfig(property), getMapping(property.getOwner()));
if (property instanceof Basic && typeName != null
&& mappings.getTypeConfiguration().getBasicTypeRegistry().getRegisteredType(typeName) != null) {
return null;
}
return typeName;With this, MapElementColumnSpec still passes, type: 'text' still applies to the elements only, and the user-type case gives the 7.0.x result. Please add a row to the spec for the user-type form.
There was a problem hiding this comment.
Fixed in b14df94, as you proposed: the decision now reads the name binder.getTypeName already resolved from the property or from the 'user-type' mapping, and namesUserCollectionType is gone.
MapElementColumnSpec has the row you asked for: a Map with 'user-type'(type: MapEltUserType, class: Map) and a Set with 'user-type'(type: SetEltUserType, class: Set) both keep their collection type, and type: 'text' still applies to the elements only. With the old logic put back, that row fails and nothing else does. grails-data-hibernate5-core passes in full on the new commit, and codeStyle passes.
…registers with a user-type mapping A Map, List or Set of simple values that names its collection type with a 'user-type' mapping lost it: the type was read only from the type of the property. The collection type is now decided from the type name that the binder resolves from the property or from the user-type mapping, and it is dropped only when that name is a basic type, such as 'text', which then maps the elements. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A collection type is still dropped when the same name is also registered as an element type.
| public String getTypeName(ToMany property, InFlightMetadataCollector mappings) { | ||
| String typeName = binder.getTypeName(property, binder.getPropertyConfig(property), getMapping(property.getOwner())); | ||
| if (property instanceof Basic && typeName != null && | ||
| mappings.getTypeConfiguration().getBasicTypeRegistry().getRegisteredType(typeName) != null) { |
There was a problem hiding this comment.
A basic map is given a collection type by name. That name is also registered as the type of an element. The collection type should be kept. It is dropped, because this treats the element name as proof the collection type does not exist. Keep the collection type.
…ose name is registered as an element type A basic collection that names a user collection type, as a class, as the name of a class or with a user-type mapping, keeps it as its collection type also when the same name is registered as the type of an element. The type is taken for the type of the elements only when it is not a user collection type, by the class the mapping holds or else by the class the name resolves to. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
A basic map, set or list that names a user collection type keeps it as its collection type when the same name is registered as the type of an element. The type is now taken for the type of the elements only when it is not a user collection type. That is decided by the class the mapping holds when it names a class, so the class loader that is current does not matter, and else by the class that the name resolves to. I generated the class: three kinds of collection (map, set, list), three ways to name the collection type (a class, the name of a class, a The Hibernate 5 core module passes (1406 tests on this branch) and |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A custom collection type the context class loader cannot see is replaced with the default.
| Class<?> userType = mapped instanceof Class<?> ? (Class<?>) mapped : null; | ||
| if (userType == null) { | ||
| try { | ||
| userType = Class.forName(typeName, true, Thread.currentThread().getContextClassLoader()); |
There was a problem hiding this comment.
A Map mapped with type: 'com.example.CustomMapType', whose name is also a registered basic type, used to be resolved by Hibernate's fallback loader. This lookup uses only the context class loader. When that loader cannot see the class, the custom collection is dropped and the default is used. Resolve the class the way Hibernate does.
…ibernate's class loader service A basic collection whose mapped type names a class that is also registered as the type of an element is kept as a user collection type when the name resolves to a UserCollectionType. The name was resolved with Class.forName and the context class loader of the thread, so on a thread whose loader cannot see the class, as in a pool of another module, the custom collection type was dropped and the default used, where Hibernate resolves the same name through its class loader service. It is now resolved through the class loader access of the bootstrap context, as Hibernate resolves it. MapElementColumnSpec has the 36 rows of the three kinds of collection, the three ways to name the type and its registration, each with the context class loader that sees the application and one that sees nothing; 3 of them fail with the previous binder.
|
Pushed the next commit, which answers the review of 12-05 at 543a11a. You were right that this lookup read the name of a type differently from Hibernate. For a basic collection whose mapped It is now resolved through This is the same finding as the one of 12-02 ("dropped when this loader cannot see it") in the form that remained: that round covered the class that the mapping holds, this one covers the name of a class, so I made the table over the whole class this time.
|
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A context class loader that cannot see the type no longer drops it. The lookup still does not follow Hibernate's own order, so a type Hibernate would load can still be replaced with the default.
| if (userType == null) { | ||
| try { | ||
| // resolved as Hibernate resolves the name of a type, through its class loader service, not through the loader of the thread | ||
| userType = mappings.getBootstrapContext().getClassLoaderAccess().classForName(typeName); |
There was a problem hiding this comment.
Hibernate's TypeFactory.customCollection looks up the class with ReflectHelper.classForName, which tries the context class loader first. This lookup uses the bootstrap loader first. When the same registered name is a UserCollectionType for Hibernate and a different class for the bootstrap loader, the binder returns null and the default collection is used. Use the same lookup Hibernate uses.
…Hibernate makes, not with another loader A basic collection whose type name is also registered as the type of an element keeps that name as its collection type when the name is a user collection type. The class was found through the class loader service of the build, which is built from the loader that hibernate.classLoaders gives, and Hibernate builds the collection type later with ReflectHelper.classForName, the loader of the thread and then its own. A class that only the loader of the setting can see was kept as the collection type, and Hibernate then failed with "user collection type class not found" for it, and a class that Hibernate loads could be replaced with the default in the opposite layout. The class is now found with ReflectHelper.classForName, the very call Hibernate makes, so a name is the collection type exactly when Hibernate will load its class. MapElementColumnSpec has three layouts of the loaders, with the lookup of Hibernate as the oracle (a class no loader sees, one that the loader of the thread sees, one that only the loader of the setting sees); the last fails with the previous commit with the MappingException of the report.
|
Fixed in db54895. The lookup of the class of a collection type did not follow the order Hibernate uses, so a class that Hibernate loads could be treated differently from how Hibernate treats it. Cause: to tell a user collection type from the type of an element when both have the same name, I found the class through the class loader service of the build. That service is built from the loader that Rule now: there is no lookup of ours. The class is found with Tests: |
🚨 All tests passed but jobs failed 🚨Failed Jobs without Test Failures❌ CI - Groovy Joint Validation Build / build_grails 🏷️ Commit: db54895 Rerun ControlsClick the checkbox to trigger a rerun:
Learn more about TestLens at testlens.app/docs. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A string name now uses Hibernate's lookup. A class value still checks the mapped class instead of the class Hibernate resolves, so a custom collection can still be replaced with the default.
| */ | ||
| private static boolean namesUserCollectionType(ToMany property, PropertyConfig config, Mapping mapping, String typeName) { | ||
| Object mapped = config != null && config.getType() != null ? config.getType() : (mapping != null ? mapping.getUserTypes().get(property.getType()) : null); | ||
| Class<?> userType = mapped instanceof Class<?> ? (Class<?>) mapped : null; |
There was a problem hiding this comment.
When user-type holds a Class from one loader, and Hibernate resolves that binary name to a UserCollectionType from another loader, this check uses the mapped class. If that name is also a basic type, the custom collection is dropped and the default is used. Check the class Hibernate resolves.

Replaces #16923, which was based on 7.2.x. The bug is also on 7.0.x, the oldest branch with it, so the change is rebuilt there. The review history is on #16923.
With Hibernate 5, mapping
type: 'text'on a basic collection (a Map, List or Set of simple values) failed the metadata build withuser collection type class not found: text, because the type name was also passed to Hibernate as the class of a user collection type.Change:
CollectionType.getTypeNamereturnsnullfor a basic collection unless the mapped type is a user collection type, so a type such astextapplies to the elements only, and a user collection type that a Map, List or Set of simple values names stays its collection type, as on 7.0.x.maxSizeandinListstill do not size the element column: for a collection,maxSizeis the number of its elements andinListlists whole collections, so aListofStringwithmaxSize: 2is valid with one element of any length.Branch policy: this touches the schema binder. I ran a
Serializableuser collection type on a Map and on a Set of strings: on 7.0.x it is the collection type and also the type of the elements, which Hibernate stores as serialized objects. That mapping builds the same way here. A user collection type is kept when it is named as a class, as the class name of a class that is not a registered basic type, or with a'user-type'mapping; a name that Hibernate also registers as a basic type is read as the type of the elements. A class name is resolved through the class loader service of Hibernate's bootstrap context, not the thread's context class loader, so it is found the same way whichever loader the caller runs under; a name that cannot be loaded is not a user collection type. Only a type that is not a user collection type, such astext, which failed the metadata build, is sent to the elements alone.Tests
MapElementColumnSpec: the default length,maxSizeandinListleaving the element column alone,type: 'text'on a Map and a List, a user collection type that a Map names staying its collection type, the join column name and the unchanged key column. The list fixtures declare aList, sincehasManyalone declares aSet. Without the change its datastore does not start, with the error above.grails-data-hibernate5-core: 1388 tests, 59 skipped, none failing.codeStylepasses.Not covered
sqlTypefor a join table column cannot be expressed in the mapping DSL.Part of #16897, which was reported for Hibernate 7; #16922 is the Hibernate 7 counterpart.
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL