My initial idea was to display Enum using @Enumerated annotion. It would look like this:
@Enumerated(STRING) private DescriptorEnum descriptor;
There will be a column in the database called DESCRIPTOR of type varchar, and Hibernate (in my case) will map the string to an enumeration.
But I have this limit of 65 thousand (see the question), which in my case is small. But I found a solution. Take a look at the following example:
public final class Descriptor { public final String acronym; private static final Hashtable<String, Descriptor> all = new Hashtable<String, Descriptor>(); static { initialize(); } private static void initialize() { new Descriptor("example001"); new Descriptor("example002"); new Descriptor("example003"); } private Descriptor(String acronym) { this.acronym = acronym; if (all.contains(this.acronym)) { throw new RuntimeException("duplicate acronym: " + this.acronym); } all.put(this.acronym, this); } public static Descriptor valueOf(String acronym) { return all.get(acronym); } public String value() { return this.acronym; } }
This descriptor class mimics the use of a typical enumeration. But now I can split the initialize () method into several that work with a 65k limit, which also exists for methods. Enum does not allow to split the initialization into several pieces - my class does.
Now I need to use a slightly different mapping:
@Column(name = "DESCRIPTOR") private String descriptorAcronym = null; private transient Descriptor descriptor = null; public Descriptor getDescriptor() { return descriptor; } public void setDescriptor(Descriptor desc) { this.descriptor = desc; this.descriptorAcronym = desc != null ? desc.acronym : null; } public String getDescriptorAcronym() { return descriptorAcronym; } public void setDescriptorAcronym(String desc) { this.descriptorAcronym = desc; this.descriptor = desc != null ? Descriptor.valueOf(desc) : null; } @PostLoad private void syncDescriptor() { this.descriptor = this.descriptorAcronym != null ? Descriptor.valueOf(this.descriptorAcronym) : null; }
That way, I can use a class like Enum in most cases. It's a little complicated ... but it seems to work. Thanks for all that led me to this decision.
Daniel Bleisteiner Mar 31 '10 at 10:21 2010-03-31 10:21
source share