使用枚举作为鉴别器值的继承策略

使用枚举作为鉴别器值的继承策略

本文介绍了SINGLE_TABLE 使用枚举作为鉴别器值的继承策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 SINGLE_TABLE 继承策略时是否可以使用枚举作为鉴别器值?

Is it possible to use an enum as a discriminator value when using SINGLE_TABLE inheritance strategy?

推荐答案

如果您要实现的是不重复鉴别器值,则有一个简单的解决方法.

If what you are trying to achieve is to not to duplicate the discriminator values, there is a simple workaround.

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="FREQUENCY",
    discriminatorType=DiscriminatorType.STRING
)
public abstract class Event  {
}

@Entity
@DiscriminatorValue(value=Frequency.Values.WEEKLY)
public class WeeklyEvent extends Event {
    …
}

public enum Frequency {
    DAILY(Values.DAILY),
    WEEKLY(Values.WEEKLY),
    MONTHLY(Values.MONTHLY);

    private String value;

    …

    public static class Values {
        public static final String DAILY = "D";
        public static final String WEEKLY = "W";
        public static final String MONTHLY = "M";
    }
}

实际上与 Hibernate/JPA 无关,但总比必须在多个地方维护这些值要好.

Nothing to do with Hibernate/JPA really, but better than having to maintain the values in multiple places.

这篇关于SINGLE_TABLE 使用枚举作为鉴别器值的继承策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 14:23