枚举超出静态初始化的65535字节限制...最好做什么? [英] Enum exeeding the 65535 bytes limit of static initializer... what's best to do?

查看:3374
本文介绍了枚举超出静态初始化的65535字节限制...最好做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始了一个很大的枚举所谓的描述符,我想在我的模型中作为参考列表。但现在我第一次遇到编译器/ VM限制,所以我正在寻找最好的解决方案来处理这个。



这里是我的错误: em>静态初始化程序的代码超过了65535个字节的限制



很清楚这是从哪里来的 - my Enum只有很多元素。但我需要这些元素 - 没有办法减少这个集合。



初始化我计划使用一个单一的枚举,因为我想确保所有的元素Enum是唯一的。它用于 Hibernate 持久性上下文中,其中对枚举的引用作为字符串值存储在数据库中。所以这必须是独一无二的!



我的Enum的内容可以分为几组属于同一组的元素。但是拆分枚举将会消除我在编译期间获得的独特安全性。

我现在的想法是定义一些名为 Descriptor 的接口,然后编写几个Enums实现它。这样我希望能够使用Hibernate枚举映射,就像它是一个单一的枚举。但我不知道如果这将工作。



任何想法如何处理这种情况? b

解决方案

我原来的想法是使用@Enumerated动画映射枚举。这看起来像下面的例子:

  @Enumerated(STRING)
private DescriptorEnum descriptor;

数据库将有一个名为DESCRIPTOR的类型varchar的列,Hibernate(在我的case)字符串到枚举。



但我有65k的限制(见问题),这在我的情况下是小。但我找到了一个解决方案。请看下面的例子:

  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描述符valueOf(String acronym){
return all.get(acronym);
}
public String value(){
return this.acronym;
}
}

此Descriptor类模拟典型枚举的用法。但现在我可以把initialize()方法分成几个工作围绕65k限制,也存在的方法。枚举不允许将初始化分成几个块 - 我的类。



现在我必须使用一个稍微不同的映射:

  @Column(name =DESCRIPTOR)
private String descriptorAcronym = null;
private transient Descriptor descriptor = null;
public Descriptor getDescriptor(){
return descriptor;
}
public void setDescriptor(描述符desc){
this.descriptor = desc;
this.descriptorAcronym = desc!= 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;
}

在大多数情况下,我可以像Enum一样使用类。这有点棘手...但它似乎工作。感谢所有的输入,最终导致我的解决方案。


I've started a rather large Enum of so called Descriptors that I've wanted to use as a reference list in my model. But now I've come across a compiler/VM limit the first time and so I'm looking for the best solution to handle this.

Here is my error : The code for the static initializer is exceeding the 65535 bytes limit

It is clear where this comes from - my Enum simply has far to much elements. But I need those elements - there is no way to reduce that set.

Initialy I've planed to use a single Enum because I want to make sure that all elements within the Enum are unique. It is used in a Hibernate persistence context where the reference to the Enum is stored as String value in the database. So this must be unique!

The content of my Enum can be devided into several groups of elements belonging together. But splitting the Enum would remove the unique safety I get during compile time. Or can this be achieved with multiple Enums in some way?

My only current idea is to define some Interface called Descriptor and code several Enums implementing it. This way I hope to be able to use the Hibernate Enum mapping as if it were a single Enum. But I'm not even sure if this will work. And I loose unique safety.

Any ideas how to handle that case?

解决方案

My original idea was to map the Enum using the @Enumerated annotion. This would have looked like the following example:

@Enumerated(STRING)
private DescriptorEnum descriptor;

The database would have a column called DESCRIPTOR of type varchar and Hibernate (in my case) would map the string to the enumeration.

But I have that limit of 65k (see question) which is to small in my case. But I've found a solution. Have 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 simulates the usage of a typical enum. But now I'm able to split the initialize() method into several ones that work around the 65k limit that also exists for methods. The Enum doesn't allow to split the initialization into several chunks - my class does.

Now I have to use a slightly different mapping though:

@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;
}

This way I can use the class like an Enum in most cases. It's a bit tricky... but it seems to work. Thanks for all the input that finally led me to that solution.

这篇关于枚举超出静态初始化的65535字节限制...最好做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆