Grails

No way of specifying hibernate <natural-id> in GORM

Details

  • Type: Sub-task Sub-task
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.0-RC1
  • Fix Version/s: 1.1-RC1
  • Component/s: Persistence
  • Labels:
    None
  • Environment:
    any

Description

Hibernate recommends identification of natural-ids (things that would be primary keys if we were not using auto-ids). From the Hibernate docs:
"even though we recommend the use of surrogate keys as primary keys, you should still try to identify natural keys for all entities. A natural key is a property or combination of properties that is unique and non-null. If it is also immutable, even better. Map the properties of the natural key inside the <natural-id> element. Hibernate will generate the necessary unique key and nullability constraints, and your mapping will be more self-documenting."

And it also says that natural ids will work with query caching (chaper 15).

So we should be able to specify them in GORM for a performance benefit both at the database level (ok, we can specify the indices in GORM) and in the query caching (I've no idea what that entails)

Activity

Hide
dev danke added a comment -

GORMS nice online docs say: "With GORM's DSL you can specify which columns need to live in which indexes:"

But the GORM docs do not show a way to specify that the index should be unique. At least on MySQL, the multicolumn index created when using the index: keyword is set to NON-UNIQUE=true. But I want a unique index.

I think the GORM DSL would benefit from adding a new keyword "uniqueIndex:" for this situation.

Also, where is the reference list of all GORM DSL keywords (or whatever they're called in a DSL)?

Thanks,
Dev

Show
dev danke added a comment - GORMS nice online docs say: "With GORM's DSL you can specify which columns need to live in which indexes:" But the GORM docs do not show a way to specify that the index should be unique. At least on MySQL, the multicolumn index created when using the index: keyword is set to NON-UNIQUE=true. But I want a unique index. I think the GORM DSL would benefit from adding a new keyword "uniqueIndex:" for this situation. Also, where is the reference list of all GORM DSL keywords (or whatever they're called in a DSL)? Thanks, Dev
Hide
Derek Alexander added a comment -

Another benefit of being able to specify natural-id is that if specified it can be used by a
org.hibernate.id.SelectGenerator

http://www.hibernate.org/hib_docs/v3/api/org/hibernate/id/SelectGenerator.html

I had a need to use the select generator strategy where my natural id was a composite of two properties.
Couldn't see a way to do this in GORM at present so had to resort to reloading the object manually
after persisting.

Show
Derek Alexander added a comment - Another benefit of being able to specify natural-id is that if specified it can be used by a org.hibernate.id.SelectGenerator http://www.hibernate.org/hib_docs/v3/api/org/hibernate/id/SelectGenerator.html I had a need to use the select generator strategy where my natural id was a composite of two properties. Couldn't see a way to do this in GORM at present so had to resort to reloading the object manually after persisting.
Hide
Graeme Rocher added a comment -

natural id can now be specified:

static mapping = {
        id natural:['title', 'author']
    }

Note following hibernate's defaults the natural identifier properties are immutable. You can make them mutable with:

static mapping = {
        id natural:[properties:['title', 'author'], mutable:true]
    }
Show
Graeme Rocher added a comment - natural id can now be specified:
static mapping = {
        id natural:['title', 'author']
    }
Note following hibernate's defaults the natural identifier properties are immutable. You can make them mutable with:
static mapping = {
        id natural:[properties:['title', 'author'], mutable:true]
    }

People

Vote (8)
Watch (7)

Dates

  • Created:
    Updated:
    Resolved: