Skip to content

fix(hibernate7): apply maxSize and type text to Map, List and Set element columns - #16922

Open
borinquenkid wants to merge 5 commits into
8.0.xfrom
fix/h7-map-element-column-8.0.x
Open

borinquenkid wants to merge 5 commits into
8.0.xfrom
fix/h7-map-element-column-8.0.x

Conversation

@borinquenkid

@borinquenkid borinquenkid commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Fixes #16897

Problem

  • Mapping type: 'text' on a basic collection (Map, List or Set of simple values) failed metadata build with Unable to load class [text], because the type name was passed to Hibernate as a user collection type class.
  • The element column of such a collection stayed at the default length, with no way to make it unbounded.

Change

  • CollectionType.getTypeName returns null for basic collection properties, so the mapped type is no longer treated as a collection type class.
  • MapSecondPassBinder and BasicCollectionElementBinder leave the element column unbounded for type: 'text' (StringColumnConstraintsBinder.bindTextType).
  • maxSize and inList do not size the element column. For a collection, maxSize is the number of its elements and inList lists whole collections, so a List of String with maxSize: 2 is valid with one element of any length.
  • type.adoc documents the behavior with a CmsPage example and says to use type: 'text' for long values.

Tests

  • MapElementColumnSpec covers default, text and maxSize on Map and List, an inList of lists, a list within maxSize that is saved, the join column name, and the unchanged key column.
  • Extended MapSecondPassBinderSpec, BasicCollectionElementBinderSpec, StringColumnConstraintsBinderSpec and MapCollectionTypeSpec.
  • H7 core suite: 4,631 tests, 0 failures.

Not covered

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Branch policy: the schema binders are touched by this pull request's own change (the element column of a basic collection, with its upgrade note); the latest commit only keeps a registered user collection type, as 8.0.x does

…ment columns

A mapped type: 'text' on a basic collection failed with "Unable to load
class [text]" because the type name was handed to Hibernate as a user
collection type. Basic collections no longer pass it as the collection
type, and the element column now honors maxSize, inList and type text.

Fixes #16897

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approving. A collection inList whose allowed values are collections prevents startup. The new tests are inside an enum, so they never run.

if (elementConfig != null) {
Column elementColumn = simpleValueColumnFetcher.getColumnForSimpleValue(element);
if (elementColumn != null) {
stringColumnConstraintsBinder.bindStringColumnConstraints(elementColumn, elementConfig, typeName);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tags inList: [['a'], ['b']] on a List of String throws while the mapping is built. The allowed value is the whole list, not a string. Do not read those allowed values as element strings.

}

enum BCEBStatus { ACTIVE, INACTIVE }
enum BCEBStatus { ACTIVE, INACTIVE

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new maxSize, text, and default-length cases sit inside BCEBStatus, after the specification has closed. Running the specification never runs them. Move them into the specification.

@borinquenkid
borinquenkid requested a review from matrei November 12, 2026 14:12
…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 with maxSize 2 was valid with one element
of six characters, and saving it failed on a varchar(2); an inList of lists, such as [['a'], ['b']],
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 unbounded for type text and keeps its length otherwise; the
documentation says to use type text for long values.

The specs that had been written inside the BCEBStatus enum, where they never ran, are in the
specification.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley Both are addressed in the new commit, and it changes more than your two points, for the reason in your review of the Hibernate 5 counterpart (#16923).

  • tags inList: [['a'], ['b']] on a collection prevented startup. The element column took its length from the inList as it does for a string property, and the allowed values of a collection are whole collections, so reading them as strings failed while the mapping was built. ListEltInList is in MapElementColumnSpec now, and BasicCollectionElementBinderSpec has a row for it; they fail without the change.
  • The new rows sat inside an enum and never ran. BCEBStatus was declared as enum BCEBStatus { ACTIVE, INACTIVE with the rows after it, so they were methods of the enum. The enum is closed and the rows are in the specification. They ran, and one of them showed that the length was still taken from maxSize for a Map too, by MapSecondPassBinder.
  • maxSize of a collection is its size, not the length of an element. A List of String with maxSize: 2 and the value ['abcdef'] is valid, and its column would have been varchar(2). Neither maxSize nor inList sizes the element column any more, for a Map, a List or a Set. What stays is type: 'text', which makes the element column unbounded; it is the fix for type: 'text' failing on a map that [Hibernate 7] Cannot use 'text' type or size in mapping for Map (the value part) #16897 asked for, and the documentation says to use it for long values. The spec has a list within maxSize that is saved, and maxSize leaving the element length alone for a map and for a list.

A length for the element column is not something maxSize can give, and joinTable: [column: ...] takes only a name today: a map there is turned into the column name (I checked [name: 'body_value', length: 500], which names the column name:body_value, length:500) and its length is ignored. I filed #17025 for both, with options.

The Hibernate 7 module (4,632 tests) and codeStyle pass.

Branch policy: this commit adds no public type and no throw. Where the PR belongs is still open; I have not moved it.

If you find another shape that does not reproduce first time, a failing spec on a throwaway branch, or the few lines you ran, would help me a lot.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A registered collection type is ignored for a basic collection.

* values or components the mapped {@code type} names the type of the elements, so it is not a collection type.
*/
public String getTypeName(HibernateToManyProperty property) {
if (property instanceof HibernateToManyCollectionProperty) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A map, set, or list registers its own collection type. The mapping uses the default collection instead, because this returns before that registration is read. Use the registered type.

@borinquenkid

Copy link
Copy Markdown
Member Author

Pushed 48e084a. You are right: my change returned no type name for every collection of basic values, which also dropped a collection type the mapping registers. CollectionType.getTypeName, CollectionType.java line 73 now returns the mapped type when it names a Hibernate UserCollectionType (namesUserCollectionType, line 80): that one is the collection type of a map, set or list, and is used as it was before this pull request. Any other type, such as text or a user type that is not a collection, still names the type of the values and is not a collection type.

The test: MapCollectionTypeSpec line 75, seven rows for a map, a set and a list, each with text (the values), a registered collection type (the collection type), and for a map a user type that is not a collection (the values). The three rows with a registered type fail without the change. grails-data-hibernate7-core runs 4,638 tests green and codeStyle is clean. The reference page says it in type.adoc line 82. What counts as a user type that is not a collection is the existing rule, isUserButNotCollectionType.

I did not add a row that runs a real UserCollectionType implementation through a session factory; the rows check the type name the binder takes, with a registered type that is only a marker. If you want an end-to-end row, say which collection type to use.

@jdaugherty jdaugherty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The collection maxSize/inList handling and the tests previously placed inside the enum are corrected. The latest guard preserves a collection type supplied directly on a property, but the registered-type concern remains for an actual user-type registration.

P2 — Inspect the effective registered collection type before returning null. CollectionType.java:73–82 asks getUserType(), which reads only the property's own mapped type. A basic map can instead register a concrete Hibernate UserCollectionType this way:

Map<String, String> attributes
static mapping = {
    'user-type'(class: Map, type: CustomMapType)
}

Here getUserType() returns null, although the normal type-name resolution consults the owning mapping and obtains CustomMapType. The new guard returns before that resolution and substitutes the default collection implementation. Registrations through grails.gorm.default.mapping have the same problem. The two resolution paths are visible in HibernatePersistentProperty.java:116–133.

Please preserve the effective registration, including its owner/default-mapping fallback. The new registered-type rows mock getUserType() to return a class, bypassing the failing lookup. Add a datastore-level test with a real collection implementation registered through the DSL, and verify that it remains the collection type while text still applies only to elements. This review is source-based; I have not rerun tests.

… basic collection stays its collection type

The guard asked the property for the type of its own mapping only, so a user collection type registered with a user-type
mapping of the owner, or of grails.gorm.default.mapping, for the class of the property was dropped for the default
collection implementation. The guard now looks at the type name that the property resolves, own mapping first and then the
registration, and a name that is not a user collection type, such as text, is still a type of the elements.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

Fixed in e961db9: the guard looks at the type name that the property resolves, its own mapping first and then the registration of its owner, or of grails.gorm.default.mapping, for its class, which is the name HibernateToManyProperty.getTypeName() returns. A name that is a user collection type stays the collection type, and a name that is not one, such as text, is still a type of the elements.

MapElementColumnSpec has the datastore-level case you asked for, with real Hibernate 7 collection types registered through the DSL: 'user-type'(type: MapEltUserType, class: Map) on a map and 'user-type'(type: SetEltUserType, class: Set) on a set. The binding of each has that type as its collection type, and the map of the entity with type: 'text' has no collection type. The fixtures implement UserCollectionType with PersistentMap and PersistentSet. With the earlier guard put back, that feature is the only one that fails. The full Hibernate 7 module passes and codeStyle passes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[Hibernate 7] Cannot use 'text' type or size in mapping for Map (the value part)

4 participants