开发者

How to handle choice field with JPA 2, Hibernate 3.5

I have an entity with Integer attributes that looks like this in proto code:

class MyEntity:
    String name

    @Choices({1, "BSD", 2, "Apache", 3, "GPL"}
    Integer frequency

    @ChoicesSegment({1000, 2000, 3000}, {"BSD", "Apache", "GPL"})
    Integer type

    St开发者_开发技巧ring getFrequency()
           return getChoice("frequency", frequency)
    String getType()
           return getChoice("type", type)

maybe this solution is more feasible:

class MyEntity:
    String name

    final static private Something? frequencyChoices = {1000, 2000, 3000}, {"BSD", "Apache", "GPL"}
    Integer frequency

    final static private String[] typeChoices = new String[] {"BSD", "Apache", "GPL"}
    Integer type

    @Choices(MyEntity.frequencyChoices)
    String getFrequency()
           return frequency)

    @IntervalChoices(MyEntity.typeChoices)
    String getType()
           return type

*get** accessors return strings according to this table.

value(type) HumanReadableString(type)
  1             BSD
  2             Apache
  3             GPL

min frequency         max frequency    HumanReadableString(frequency)
    0                     1000                rare
    1000                  2000              frequent
    2001                  3000                sexy

It should be possible to get all possible values that an attribute can take, example:

getChoices(MyEntity, "type") returns ("rare", "frequent", "sexy")

It should be possible to get the bound value from the string:

getValue(MyEntity, "frequency", "sexy") returns (2000,3000)

edit: purpose of all of this This methods should simplify the generation of forms and requests (of course this should not be view implementation bound)

edit: added how I would like to tell Java that some attributes are spécial so that it can generate get* accessors acordingly.

edit: added how to submit in the code the choices

edit: the only thing that I store in the db is integers, when I want to print them they should be converted somehow to their human readable string.


You can have additional info in enums:

public enum License { 

    GPL("GPL"),

    APACHE("Apache License");

    public License(String displayName) {
        this.displayName=displayName;
    }

    String displayName;

 } 

Additional functions as required, but have a close look which functions are already provided by the Enum classes.


You can do it without any hassle (but note, that the value in the DB will be the ordinal() value of the enums. So:

public enum License { GPL, APACHE, BSD }

FrequencyChoices could go into an @ElementCollection.

If you need human readable values, you may want to convert your Enum to an ordinary class, and persist it as a separate table, so you can add new licenses more easily to the list...

@Entity
public class License {
    @Id long id;
    String name;
}


I have not tried to persist this but you can try following http://marekhalmo.blogspot.sk/2012/09/cool-java-enums.html

I would stick to enums.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜