[hibernate-issues] [Hibernate-JIRA] Commented: (ANN-161) Add support for custom code-based enums in EnumType - such as mapping ISO codes to gender Enums

Frederic Leitenberger (JIRA) noreply at atlassian.com
Wed May 21 14:33:33 EDT 2008


    [ http://opensource.atlassian.com/projects/hibernate/browse/ANN-161?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_30236 ] 

Frederic Leitenberger commented on ANN-161:
-------------------------------------------

And here my TestEntity. Never mind the BaseEntity, it contains id, version and some additional fields.
You may need to modify the code a little bit to get it working. Or try modifying your own enums to use the MappedEnum-interface.

import javax.persistence.*;

@Entity
public class TestEntity extends BaseEntity {

    ExampleEnum color;

    @Enumerated
    public ExampleEnum getColor() {
        return color;
    }

    public void setColor(ExampleEnum color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return super.toString() + ", color: " + color;
    }

}

> Add support for custom code-based enums in EnumType - such as mapping ISO codes to gender Enums
> -----------------------------------------------------------------------------------------------
>
>                 Key: ANN-161
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/ANN-161
>             Project: Hibernate Annotations
>          Issue Type: Improvement
>          Components: binder
>    Affects Versions: 3.1beta6
>            Reporter: Leonardo Quijano
>            Priority: Minor
>         Attachments: MappedEnum.java.patch
>
>
> Now, this could be seen as an improvement to EnumType that's specific to a business domain, but I'm thinking it could help in the general case. Right now we can map JDK enums to several database types, such as INT, VARCHAR, etc. Each of these approaches have some drawbacks, depending on the domain:
> 1) The int mapping is not very reliable through refactorings. An enum that's mapped to a "0" or "2" integer value in the database could end up mapped as a different value if a future refactoring adds an additional enum with an ordinal of 0.
> 2) The varchar mapping could use the enum name instead of its ordinal. But again, if the enum changes, the mapping gets lost and it needs manual changes. It's also heavier on the performance side.
> A useful approach for enum mappings is to use common codes to represent them in the database. That depends of course of the domain logic that's being used. A useful example is the Human sex mapping, defined in ISO code 5218:
> http://en.wikipedia.org/wiki/ISO_5218
>     * 0 = not known,
>     * 1 = male,
>     * 2 = female,
>     * 9 = not specified.
> For this to work, the Sex enum could have a property "isoCode" that specifies the mapping between the database integer code and the Java enum. 
> For other domain examples additional examples could be specified.
> The EnumType could include a parameter that indicates the property to be evaluated for code-mapping, performing the translation in this way. Of course, reflection cache / byte-code generation / whatever could be specified to speed up the process.
> As always, patches are offered if you think this improvement is viable.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list