Grails

Enums should be mapped using EnumType.STRING by GORM

Details

  • Type: Sub-task Sub-task
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.0.3
  • Fix Version/s: 1.1-beta1
  • Component/s: Persistence
  • Labels:
    None

Description

Currently GORM maps enums to the ordinal value by default. It would be more save to use the string value instead. A least it should be possible to change GRAILS default behavior.

Please check the discussion on the mailing list:
http://www.nabble.com/are-new-Enums-in-gorm-safe--td17699283.html

Activity

Hide
Peter Ledbrook added a comment -

For the record, I saw on the Hibernate forums that the JPA spec. requires that the ordinal is used by default, so that's why Hibernate uses it as a default.

Show
Peter Ledbrook added a comment - For the record, I saw on the Hibernate forums that the JPA spec. requires that the ordinal is used by default, so that's why Hibernate uses it as a default.
Hide
Graeme Rocher added a comment -

Done in 1.1 branch. The old behaviour can be obtained with

static mapping = {
      someEnum enumType:"ordinal"
}
Show
Graeme Rocher added a comment - Done in 1.1 branch. The old behaviour can be obtained with
static mapping = {
      someEnum enumType:"ordinal"
}
Hide
Daniel Monti added a comment -

String is bad for database search and indexing, why should be default?

When some class extends other with enum field the mapping does't work correctly.

Parent.groovy
public abstract class Parent
{
    Status status;

    static constraints =
    {
	type(enumType:"ordinal");
    }

    public enum Status
    {
	ok, denied, cancel, test;
    }
}
Child.groovy
public class Child extends Parent
{
    String name;

    int value;

    static constraints =
    {
        // Test to convert enumType to ordinal *does't work either*
	type(enumType:"ordinal");
    }
}
Show
Daniel Monti added a comment - String is bad for database search and indexing, why should be default? When some class extends other with enum field the mapping does't work correctly.
Parent.groovy
public abstract class Parent
{
    Status status;

    static constraints =
    {
	type(enumType:"ordinal");
    }

    public enum Status
    {
	ok, denied, cancel, test;
    }
}
Child.groovy
public class Child extends Parent
{
    String name;

    int value;

    static constraints =
    {
        // Test to convert enumType to ordinal *does't work either*
	type(enumType:"ordinal");
    }
}
Hide
Daniel Monti added a comment -

My bad, on "type(enumType:"ordinal");" should be "staus(enumType:"ordinal");" but does't work any way...

Show
Daniel Monti added a comment - My bad, on "type(enumType:"ordinal");" should be "staus(enumType:"ordinal");" but does't work any way...

People

Vote (10)
Watch (7)

Dates

  • Created:
    Updated:
    Resolved: