You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by bo...@apache.org on 2026/10/11 20:10:52 UTC
(grails-core) branch 8.0.x updated: Hibernate 7: unique groups on enum and collection properties (#16533)
This is an automated email from the ASF dual-hosted git repository.
borinquenkid pushed a commit to branch 8.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git
The following commit(s) were added to refs/heads/8.0.x by this push:
new c424313499 Hibernate 7: unique groups on enum and collection properties (#16533)
c424313499 is described below
commit c42431349954ba6d2ed28cea6391f6964c308af6
Author: Walter B Duque de Estrada <wb...@mac.com>
AuthorDate: Sun Oct 11 15:10:47 2026 -0500
Hibernate 7: unique groups on enum and collection properties (#16533)
* fix: unique groups on enum and collection properties
A unique group (unique: 'other' or unique: ['a', 'b']) creates a unique key over
the property's column and the columns of the listed properties. Two shapes were
broken:
- An enum property made no key at all: EnumTypeBinder never ran the unique key
creation that ColumnBinder.bindColumn performs through CreateKeyForProps, so
duplicates of the group were accepted by the database. EnumTypeBinder now calls
CreateKeyForProps for the enum column, exactly as ColumnBinder does.
- A collection property made a key over a column its table lacks: the collection
key is bound through ColumnBinder with the collection property, so the group
became a unique key of the collection table that named the owner's column
(x on ug_collection_group_tags). The collection table then failed to create
and the first insert failed with "Table not found". The same path serves a
basic collection of enums, via EnumTypeBinder. CreateKeyForProps now creates
no key for a to-many property: the group's other columns are not in its table,
so there is nothing meaningful to make. Hibernate 5 reaches the same code
(bindColumn -> handleUniqueConstraint on the collection key) and, for enums,
never creates the key either, so it has both defects; ignoring the group on a
collection is the only consistent reading.
Plain unique: true on an enum already worked and is covered as a regression row.
Co-Authored-By: Claude Sonnet 5.5 <no...@anthropic.com>
* fix: keep the unique group key of a serializable to-many property
A to-many property mapped with type 'serializable' is bound as a plain
column of the owner table (GrailsPropertyBinder routes it through
bindBasicValue), so a unique group on it is valid and the previous code
created its key. The blanket exclusion added for collection tables
dropped that key; restrict it to to-many properties bound as collections.
Co-Authored-By: Claude Sonnet 5.5 <no...@anthropic.com>
* fix: keep the unique group key of a to-many mapped with a user type
GrailsPropertyBinder binds a to-many property mapped with a user type as a
plain column of the owner table, like a serializable one, but the key
creation only exempted serializable, so duplicates the database used to
reject were allowed again. Both now use one shared condition,
HibernateToManyProperty.isBoundAsCollection, so they cannot drift apart.
Co-Authored-By: Claude Sonnet 5.5 <no...@anthropic.com>
---------
Co-authored-by: Claude Sonnet 5.5 <no...@anthropic.com>
---
.../cfg/domainbinding/binder/EnumTypeBinder.java | 20 ++
.../domainbinding/binder/GrailsPropertyBinder.java | 5 +-
.../hibernate/HibernateToManyProperty.java | 15 +
.../cfg/domainbinding/util/CreateKeyForProps.java | 8 +
.../tests/UniqueGroupEnumAndCollectionSpec.groovy | 302 +++++++++++++++++++++
.../cfg/domainbinding/CreateKeyForPropsSpec.groovy | 77 ++++++
.../cfg/domainbinding/EnumTypeBinderSpec.groovy | 20 ++
7 files changed, 444 insertions(+), 3 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java
index c955d3810b..0c14d5c8b2 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java
@@ -34,6 +34,7 @@ import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy;
import org.grails.orm.hibernate.cfg.PropertyConfig;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEnumProperty;
import org.grails.orm.hibernate.cfg.domainbinding.util.ColumnNameForPropertyAndPathFetcher;
+import org.grails.orm.hibernate.cfg.domainbinding.util.CreateKeyForProps;
import org.grails.orm.hibernate.cfg.domainbinding.util.GrailsEnumType;
import static org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBinder.ENUM_CLASS_PROP;
@@ -47,6 +48,7 @@ public class EnumTypeBinder {
private final IndexBinder indexBinder;
private final ColumnConfigToColumnBinder columnConfigToColumnBinder;
private final PersistentEntityNamingStrategy namingStrategy;
+ private final CreateKeyForProps createKeyForProps;
public EnumTypeBinder(
MetadataBuildingContext metadataBuildingContext,
@@ -66,11 +68,28 @@ public class EnumTypeBinder {
IndexBinder indexBinder,
ColumnConfigToColumnBinder columnConfigToColumnBinder,
PersistentEntityNamingStrategy namingStrategy) {
+ this(
+ metadataBuildingContext,
+ columnNameForPropertyAndPathFetcher,
+ indexBinder,
+ columnConfigToColumnBinder,
+ namingStrategy,
+ new CreateKeyForProps(columnNameForPropertyAndPathFetcher));
+ }
+
+ protected EnumTypeBinder(
+ MetadataBuildingContext metadataBuildingContext,
+ ColumnNameForPropertyAndPathFetcher columnNameForPropertyAndPathFetcher,
+ IndexBinder indexBinder,
+ ColumnConfigToColumnBinder columnConfigToColumnBinder,
+ PersistentEntityNamingStrategy namingStrategy,
+ CreateKeyForProps createKeyForProps) {
this.metadataBuildingContext = metadataBuildingContext;
this.columnNameForPropertyAndPathFetcher = columnNameForPropertyAndPathFetcher;
this.indexBinder = indexBinder;
this.columnConfigToColumnBinder = columnConfigToColumnBinder;
this.namingStrategy = namingStrategy;
+ this.createKeyForProps = createKeyForProps;
}
public BasicValue bindEnumType(@Nonnull HibernateEnumProperty property, String path) {
@@ -99,6 +118,7 @@ public class EnumTypeBinder {
Table t = simpleValue.getTable();
t.addColumn(column);
simpleValue.addColumn(column);
+ createKeyForProps.createKeyForProps(property, path, t, columnName);
if (!pc.getColumns().isEmpty()) {
ColumnConfig columnConfig = pc.getColumns().get(0);
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
index f54cd92584..bfca162094 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
@@ -90,9 +90,8 @@ public class GrailsPropertyBinder {
value = foreignKeyOneToOneBinder.bind(oneToOne, path);
} else if (currentGrailsProp instanceof HibernateManyToOneProperty manyToOne) {
value = manyToOneBinder.bindManyToOne(manyToOne, table, path);
- } else if (currentGrailsProp instanceof HibernateToManyProperty toMany &&
- !currentGrailsProp.isSerializableType()) {
- value = collectionBinder.bindCollection(toMany, path);
+ } else if (HibernateToManyProperty.isBoundAsCollection(currentGrailsProp)) {
+ value = collectionBinder.bindCollection((HibernateToManyProperty) currentGrailsProp, path);
} else if (currentGrailsProp instanceof HibernateEmbeddedProperty embedded) {
value = componentBinder.bindComponent(embedded, path);
} else if (currentGrailsProp instanceof HibernateSimpleProperty simple) {
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java
index c836359d60..cbbda617e6 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java
@@ -53,6 +53,21 @@ import static org.grails.orm.hibernate.cfg.domainbinding.util.CascadeBehavior.SA
/** Marker interface for Hibernate to-many associations */
public interface HibernateToManyProperty extends PropertyWithMapping<PropertyConfig>, HibernateAssociation {
+ /**
+ * Decides whether a property is bound as a collection, with a collection table of its own, or as a plain
+ * column of its owner's table. A to-many property mapped with a user type or as {@code serializable} is a
+ * plain column. This is the single condition that {@code GrailsPropertyBinder} routes by and that the
+ * unique group key creation relies on.
+ *
+ * @param property the property to inspect
+ * @return true if the property is a to-many bound as a collection
+ */
+ static boolean isBoundAsCollection(HibernatePersistentProperty property) {
+ return property instanceof HibernateToManyProperty &&
+ !property.isUserButNotCollectionType() &&
+ !property.isSerializableType();
+ }
+
default boolean hasSort() {
return StringUtils.hasText(getHibernateMappedForm().getSort());
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/CreateKeyForProps.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/CreateKeyForProps.java
index feaf52f04c..1c04ba6c1d 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/CreateKeyForProps.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/CreateKeyForProps.java
@@ -28,6 +28,7 @@ import org.hibernate.mapping.Table;
import org.grails.orm.hibernate.cfg.PropertyConfig;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
public class CreateKeyForProps {
@@ -48,6 +49,13 @@ public class CreateKeyForProps {
}
public void createKeyForProps(HibernatePersistentProperty grailsProp, String path, Table table, String columnName) {
+ if (HibernateToManyProperty.isBoundAsCollection(grailsProp)) {
+ // The column of a bound collection lives in the collection table, which does not hold the columns
+ // of the other properties of the group, so a unique group has no key to create there. A to-many
+ // mapped with a user type or as serializable is a plain column of the owner table, so it keeps its
+ // key. The condition is the one GrailsPropertyBinder routes by.
+ return;
+ }
PropertyConfig mappedForm = grailsProp.getMappedForm();
if (mappedForm.isUnique() && mappedForm.isUniqueWithinGroup()) {
diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/UniqueGroupEnumAndCollectionSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/UniqueGroupEnumAndCollectionSpec.groovy
new file mode 100644
index 0000000000..7e53ca8d3e
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/UniqueGroupEnumAndCollectionSpec.groovy
@@ -0,0 +1,302 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package grails.gorm.tests
+
+import java.sql.PreparedStatement
+import java.sql.ResultSet
+import java.sql.SQLException
+import java.sql.Types
+
+import grails.gorm.annotation.Entity
+import grails.gorm.transactions.Rollback
+import org.grails.orm.hibernate.HibernateDatastore
+import org.hibernate.mapping.Collection
+import org.hibernate.mapping.Table
+import org.hibernate.type.descriptor.WrapperOptions
+import org.hibernate.usertype.UserType
+import spock.lang.AutoCleanup
+import spock.lang.Shared
+import spock.lang.Specification
+
+/**
+ * A {@code unique} group makes a unique key over the property's column and the columns of the listed
+ * properties. It has to do so for enum properties as it does for every other column, and it has no meaning on
+ * a collection property, whose table does not hold the listed columns.
+ */
+@Rollback
+class UniqueGroupEnumAndCollectionSpec extends Specification {
+
+ @Shared @AutoCleanup HibernateDatastore datastore = new HibernateDatastore(
+ UgEnumGroup, UgEnumConstraintGroup, UgStringGroup, UgEnumUnique, UgCollectionGroup, UgEnumCollectionGroup, UgSerializableGroup, UgUserTypeGroup)
+
+ void "a unique group on an enum property makes a unique key over the enum and the listed columns"() {
+ expect:
+ uniqueKeyColumns(UgEnumGroup) == [['state', 'other'] as Set]
+ }
+
+ void "a unique group declared in the constraints block on an enum property makes the same key"() {
+ expect:
+ uniqueKeyColumns(UgEnumConstraintGroup) == [['state', 'other'] as Set]
+ }
+
+ void "a unique group on a string property still makes a unique key, as before"() {
+ expect:
+ uniqueKeyColumns(UgStringGroup) == [['name', 'other'] as Set]
+ }
+
+ void "a duplicate of an enum unique group is rejected by the database"() {
+ given:
+ new UgEnumGroup(state: UgState.ON, other: 'a').save(flush: true, failOnError: true)
+ UgEnumGroup.withSession { it.clear() }
+
+ when:
+ new UgEnumGroup(state: UgState.ON, other: 'a').save(flush: true, validate: false)
+
+ then:
+ thrown(Exception)
+ }
+
+ void "the same enum with another value of the grouped column is accepted"() {
+ when:
+ new UgEnumGroup(state: UgState.ON, other: 'b').save(flush: true, failOnError: true)
+ new UgEnumGroup(state: UgState.OFF, other: 'b').save(flush: true, failOnError: true)
+ new UgEnumGroup(state: UgState.ON, other: 'c').save(flush: true, failOnError: true)
+
+ then:
+ UgEnumGroup.countByOther('b') == 2
+ }
+
+ void "a plain unique enum property is unique in the database"() {
+ given:
+ new UgEnumUnique(state: UgState.ON).save(flush: true, failOnError: true)
+ UgEnumUnique.withSession { it.clear() }
+
+ when:
+ new UgEnumUnique(state: UgState.ON).save(flush: true, validate: false)
+
+ then:
+ thrown(Exception)
+ }
+
+ void "the unique keys of a collection table name only columns of that table"() {
+ given:
+ Collection collection = datastore.metadata.getCollectionBinding(UgCollectionGroup.name + '.tags')
+ Table table = collection.collectionTable
+ Set<String> columns = table.columns*.name.toSet()
+
+ expect:
+ table.uniqueKeys.values().every { it.columns*.name.every { String name -> columns.contains(name) } }
+ }
+
+ void "a collection property with a unique group can be saved and reloaded"() {
+ given:
+ UgCollectionGroup owner = new UgCollectionGroup(x: 'x')
+ owner.addToTags('one')
+ owner.addToTags('two')
+ owner.save(flush: true, failOnError: true)
+ UgCollectionGroup.withSession { it.clear() }
+
+ expect:
+ UgCollectionGroup.get(owner.id).tags == ['one', 'two'] as Set
+ }
+
+ void "a collection of enums with a unique group can be saved and reloaded"() {
+ given:
+ UgEnumCollectionGroup owner = new UgEnumCollectionGroup(x: 'x')
+ owner.addToStates(UgState.ON)
+ owner.addToStates(UgState.OFF)
+ owner.save(flush: true, failOnError: true)
+ UgEnumCollectionGroup.withSession { it.clear() }
+
+ expect:
+ UgEnumCollectionGroup.get(owner.id).states == [UgState.ON, UgState.OFF] as Set
+ }
+
+ void "a serializable collection property keeps its unique group key on the owner table"() {
+ expect:
+ uniqueKeyColumns(UgSerializableGroup) == [['tags', 'x'] as Set]
+ }
+
+ void "a duplicate of a serializable collection unique group is rejected by the database"() {
+ given:
+ new UgSerializableGroup(x: 'x', tags: ['one'] as Set).save(flush: true, failOnError: true)
+ UgSerializableGroup.withSession { it.clear() }
+
+ when:
+ new UgSerializableGroup(x: 'x', tags: ['one'] as Set).save(flush: true, validate: false)
+
+ then:
+ thrown(Exception)
+ }
+
+ void "a to-many property mapped with a user type keeps its unique group key on the owner table"() {
+ expect:
+ uniqueKeyColumns(UgUserTypeGroup) == [['tags', 'x'] as Set]
+ }
+
+ void "a duplicate of a user type to-many unique group is rejected by the database"() {
+ given:
+ new UgUserTypeGroup(x: 'x', tags: ['one', 'two'] as Set).save(flush: true, failOnError: true)
+ UgUserTypeGroup.withSession { it.clear() }
+
+ expect: 'the user type round-trips the value'
+ UgUserTypeGroup.list().first().tags == ['one', 'two'] as Set
+
+ when:
+ new UgUserTypeGroup(x: 'x', tags: ['one', 'two'] as Set).save(flush: true, validate: false)
+
+ then:
+ thrown(Exception)
+ }
+
+ private List<Set<String>> uniqueKeyColumns(Class<?> domain) {
+ Table table = datastore.metadata.getEntityBinding(domain.name).table
+ table.uniqueKeys.values().collect { it.columns*.name*.toLowerCase().toSet() }
+ }
+}
+
+enum UgState {
+ ON, OFF
+}
+
+@Entity
+class UgEnumGroup {
+ UgState state
+ String other
+
+ static mapping = {
+ state unique: 'other'
+ }
+}
+
+@Entity
+class UgEnumConstraintGroup {
+ UgState state
+ String other
+
+ static constraints = {
+ state unique: 'other'
+ }
+}
+
+@Entity
+class UgStringGroup {
+ String name
+ String other
+
+ static mapping = {
+ name unique: 'other'
+ }
+}
+
+@Entity
+class UgEnumUnique {
+ UgState state
+
+ static mapping = {
+ state unique: true
+ }
+}
+
+@Entity
+class UgCollectionGroup {
+ String x
+ Set<String> tags
+
+ static hasMany = [tags: String]
+
+ static mapping = {
+ tags unique: 'x'
+ }
+}
+
+@Entity
+class UgEnumCollectionGroup {
+ String x
+ Set<UgState> states
+
+ static hasMany = [states: UgState]
+
+ static mapping = {
+ states unique: 'x'
+ }
+}
+
+@Entity
+class UgSerializableGroup {
+ String x
+ Set<String> tags
+
+ static hasMany = [tags: String]
+
+ static mapping = {
+ tags type: 'serializable', unique: 'x'
+ }
+}
+
+@Entity
+class UgUserTypeGroup {
+ String x
+ Set<String> tags
+
+ static hasMany = [tags: String]
+
+ static mapping = {
+ tags type: UgTagSetType, unique: 'x'
+ }
+}
+
+class UgTagSetType implements UserType<Set> {
+
+ @Override
+ int getSqlType() {
+ Types.VARCHAR
+ }
+
+ @Override
+ Class<Set> returnedClass() {
+ Set
+ }
+
+ @Override
+ Set nullSafeGet(ResultSet rs, int position, WrapperOptions options) throws SQLException {
+ String value = rs.getString(position)
+ rs.wasNull() ? null : (value.split(',') as Set)
+ }
+
+ @Override
+ void nullSafeSet(PreparedStatement st, Set value, int position, WrapperOptions options) throws SQLException {
+ if (value == null) {
+ st.setNull(position, Types.VARCHAR)
+ }
+ else {
+ st.setString(position, value.toList().sort().join(','))
+ }
+ }
+
+ @Override
+ Set deepCopy(Set value) {
+ value == null ? null : new LinkedHashSet(value)
+ }
+
+ @Override
+ boolean isMutable() {
+ true
+ }
+}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CreateKeyForPropsSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CreateKeyForPropsSpec.groovy
index 061225d18e..260ffaadda 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CreateKeyForPropsSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CreateKeyForPropsSpec.groovy
@@ -21,6 +21,7 @@ package org.grails.orm.hibernate.cfg.domainbinding
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty
import org.hibernate.MappingException
import org.hibernate.mapping.Table
import spock.lang.Specification
@@ -138,4 +139,80 @@ class CreateKeyForPropsSpec extends Specification {
0 * uniqueKeyCreator._
0 * columnNameFetcher._
}
+
+ def "creates no key for a collection property, whose table does not hold the other columns of the group"() {
+ given:
+ def columnNameFetcher = Mock(ColumnNameForPropertyAndPathFetcher)
+ def uniqueKeyCreator = Mock(UniqueKeyForColumnsCreator)
+ def subject = new CreateKeyForProps(columnNameFetcher, uniqueKeyCreator)
+ def collectionProp = Mock(HibernateToManyProperty) {
+ isSerializableType() >> false
+ isUserButNotCollectionType() >> false
+ }
+
+ when:
+ subject.createKeyForProps(collectionProp, "", new Table("t_collection"), "owner_id")
+
+ then:
+ 0 * collectionProp.getMappedForm()
+ 0 * uniqueKeyCreator._
+ 0 * columnNameFetcher._
+ }
+
+ def "creates the key for a serializable collection property, whose column is on the owner table"() {
+ given:
+ def columnNameFetcher = Mock(ColumnNameForPropertyAndPathFetcher)
+ def uniqueKeyCreator = Mock(UniqueKeyForColumnsCreator)
+ def subject = new CreateKeyForProps(columnNameFetcher, uniqueKeyCreator)
+ def owner = Mock(GrailsHibernatePersistentEntity)
+ def otherProp = Mock(HibernatePersistentProperty)
+ def mappedForm = Mock(org.grails.orm.hibernate.cfg.PropertyConfig) {
+ isUnique() >> true
+ isUniqueWithinGroup() >> true
+ getUniquenessGroup() >> ["x"]
+ }
+ def serializableProp = Mock(HibernateToManyProperty) {
+ isSerializableType() >> true
+ getMappedForm() >> mappedForm
+ getHibernateOwner() >> owner
+ }
+ owner.getHibernatePropertyByName("x") >> otherProp
+ columnNameFetcher.getColumnNameForPropertyAndPath(otherProp, "", null) >> "x_col"
+ def table = new Table("owner")
+
+ when:
+ subject.createKeyForProps(serializableProp, "", table, "tags")
+
+ then:
+ 1 * uniqueKeyCreator.createUniqueKeyForColumns(table, { List cols -> cols*.name == ["tags", "x_col"] })
+ }
+
+ def "creates the key for a to-many property mapped with a user type, whose column is on the owner table"() {
+ given:
+ def columnNameFetcher = Mock(ColumnNameForPropertyAndPathFetcher)
+ def uniqueKeyCreator = Mock(UniqueKeyForColumnsCreator)
+ def subject = new CreateKeyForProps(columnNameFetcher, uniqueKeyCreator)
+ def owner = Mock(GrailsHibernatePersistentEntity)
+ def otherProp = Mock(HibernatePersistentProperty)
+ def mappedForm = Mock(org.grails.orm.hibernate.cfg.PropertyConfig) {
+ isUnique() >> true
+ isUniqueWithinGroup() >> true
+ getUniquenessGroup() >> ["x"]
+ }
+ def userTypeProp = Mock(HibernateToManyProperty) {
+ isSerializableType() >> false
+ isUserButNotCollectionType() >> true
+ getMappedForm() >> mappedForm
+ getHibernateOwner() >> owner
+ }
+ owner.getHibernatePropertyByName("x") >> otherProp
+ columnNameFetcher.getColumnNameForPropertyAndPath(otherProp, "", null) >> "x_col"
+ def table = new Table("owner")
+
+ when:
+ subject.createKeyForProps(userTypeProp, "", table, "tags")
+
+ then:
+ 1 * uniqueKeyCreator.createUniqueKeyForColumns(table, { List cols -> cols*.name == ["tags", "x_col"] })
+ }
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy
index bb30982a1c..27ba192c41 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy
@@ -44,6 +44,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateBasicProper
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEnumProperty
import org.grails.orm.hibernate.cfg.domainbinding.util.BackticksRemover
import org.grails.orm.hibernate.cfg.domainbinding.util.ColumnNameForPropertyAndPathFetcher
+import org.grails.orm.hibernate.cfg.domainbinding.util.CreateKeyForProps
import org.grails.orm.hibernate.cfg.domainbinding.util.DefaultColumnNameFetcher
class EnumTypeBinderSpec extends HibernateGormDatastoreSpec {
@@ -210,6 +211,25 @@ class EnumTypeBinderSpec extends HibernateGormDatastoreSpec {
metadata[0].toString().contains("AVAILABLE")
metadata[1] == "the status"
}
+
+ def "should create the unique group key for the enum column on the bound table"() {
+ given:
+ def grailsDomainBinder = getGrailsDomainBinder()
+ def namingStrategy = grailsDomainBinder.getNamingStrategy()
+ def defaultColumnNameFetcher = new DefaultColumnNameFetcher(namingStrategy, new BackticksRemover())
+ def columnNameFetcher = new ColumnNameForPropertyAndPathFetcher(namingStrategy, defaultColumnNameFetcher, new BackticksRemover())
+ def createKeyForProps = Mock(CreateKeyForProps)
+ def keyBinder = new EnumTypeBinder(grailsDomainBinder.getMetadataBuildingContext(), columnNameFetcher,
+ indexBinder, columnBinder, namingStrategy, createKeyForProps)
+ def table = new Table("person")
+ def property = setupProperty(Person01, "status", table)
+
+ when:
+ keyBinder.bindEnumType(property as HibernateEnumProperty, "myPath")
+
+ then:
+ 1 * createKeyForProps.createKeyForProps(property, "myPath", table, "my_path_status")
+ }
}
// --- Supporting Classes ---