实现接口的枚举类型的映射集合

发布于 2024-11-04 03:59:02 字数 1236 浏览 5 评论 0原文

我有一件棘手的事情,我正在尝试使用枚举来映射以存储静态数据。

它从这样声明的枚举类开始:

interface MyInterface {
    String getName();
}

public enum A implements MyInterface {
FIRST_A("First A");
private final String name;
private A(String name) {
this.name = name;
}
public String getName() {
return name;
}

public String toString() {
return name;
}
}

public enum B implements MyInterface {
FIRST_B("First B");
private final String name;
private B(String name) {
this.name = name;
}
public String getName() {
return name;
}

public String toString() {
return name;
}
}

现在我想将它们用作:

List<MyInterface> elements

问题在于映射集合。如果我使用我编写的 CompositeUserType 来保存要保存的枚举类以及通过使用 EnumType.STRING 保存 @Enumerated 所产生的 String 值,我通常可以很好地保存这些内容。在这些情况下,我可以声明:


@Type(type="MyCustomEnumType.class")
@Columns(columns = {
            @Column(name = "enumValue"), @Column(name = "enumClass")
})
private Enum enumValue;

在这种情况下,我可以很好地保留它们,但我不确定在集合上使用 @Type 是否会使该集合内的所有项目都使用该自定义类型保留。我应该在它上面使用 @ElementCollection 因为它是可嵌入的吗?或者我是否使用我编写的用户类型类作为集合的 targetClass?我对它如何用于类似的事情有点困惑。我想让它保持足够的通用性,以将 A 或 B 保留在集合中(尽管最终每个实体仅保留一种类型,但也可以是其中之一)。

我不想将其设置为实体类,因为它是不会更改的静态数据,但将来可能会有一个新版本,最终会成为另一个枚举。

I've got a tricky thing I'm trying to map with Enums for storing static data.

It starts off with enum classes declared as such:

interface MyInterface {
    String getName();
}

public enum A implements MyInterface {
FIRST_A("First A");
private final String name;
private A(String name) {
this.name = name;
}
public String getName() {
return name;
}

public String toString() {
return name;
}
}

public enum B implements MyInterface {
FIRST_B("First B");
private final String name;
private B(String name) {
this.name = name;
}
public String getName() {
return name;
}

public String toString() {
return name;
}
}

Now I want to use these as:

List<MyInterface> elements

The problem comes in mapping the collection. I can persist these just fine normally if I use a CompositeUserType I've written for writing the class of the enum being persisted as well as the String value that would come from persisting an @Enumerated with the EnumType.STRING. In those cases I can declare:


@Type(type="MyCustomEnumType.class")
@Columns(columns = {
            @Column(name = "enumValue"), @Column(name = "enumClass")
})
private Enum enumValue;

In that case I can persist them just fine, but I'm uncertain if using @Type on the collection will make all items inside that collection persist using that custom type. Should I use @ElementCollection as well on it since it's such that it's embeddable? Or do I use the user type class I've written as the targetClass of the collection? I'm a bit confused on how it works for something like that. I want to keep it generic enough to persist A or B possibly in the collection (although it will end up being only one type per entity persisted, but could be either).

I'd prefer not to have to set this up as an Entity class due to it being static data that won't change, but may have a new version in the future which would end up being another enum.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2024-11-11 03:59:02

Hibernate 不支持开箱即用,但您可以编写自己的 usertype 用于存储完全限定的枚举名称,例如 com.package.Type.ENUM_VALUE。我不知道用户类型是否可用于将一个值/对象映射到多个数据库列。

此处有一个用于存储枚举的用户类型示例。该示例仅存储一个枚举,但您也许可以将其用作模板。

Hibernate doesn't support that out of the box but you can write your own usertype to store the fully qualified Enum name, for example com.package.Type.ENUM_VALUE. I don't know if user types can be used to map one value/object to more than one database column.

There's an example here of a usertype to store an enum. The example just stores an enum, but you might be able to use it as a template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文