开发者

How do I control column size for enums?

This is with EclipseLink via JPA, where I let it create the tables for me. The back end data base is Derby for development mode, and I expect to use MySQL or something else for deployment.

In my entity I have an enum:

@Entity
public class Thing implements Serializable {

    public enum Choice { Able, Baker, Charlie, Delta }

    @Column(precision = 1)
    private Choice  choiceValue;

    // etc
}

The enum ordinal only needs one digit, 开发者_JAVA技巧but when the table gets created it allocates 10 digits for it. So a million records will waste 9 Megabytes. The "precision = 1" term is ignored.

Is there a way to cause it to use just one byte to encode the enum?


Setting scale or precision for Integer column where enum is mapped in Derby & EclipseLink does not help. It does nothing, you will end up with INTEGER anyway. You can have more control with following.

@Column(columnDefinition = "SMALLINT") //smallint= 2 bytes or
//@Column(columnDefinition = "NUMERIC(1)") //
private Choice  choiceValue;

It had worked for me, but basically Java type for NUMERIC is BigDecimal and for SMALLINT short, maybe that can produce problems in some cases. For example native queries will return BigDecimal for choiceValue-column.

How much storage is needed depends bit about database provider, some older MySQL stored character per digit, now it seems to be quite fine tuned: http://dev.mysql.com/doc/refman/5.0/en/precision-math-decimal-changes.html I don't know how it is exactly in Derby, but I would assume it not taking more than byte.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜