在Hibernate中映射EnumSet [英] Mapping EnumSet in Hibernate

查看:121
本文介绍了在Hibernate中映射EnumSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在数据库中存储EnumSet(使用Hibernate)?

  @Entity 
public class A
{
public static enum SOME_ENUM {A,B,C};

私人EnumSet< SOME_ENUM> myEnumSet = EnumSet.of(SOME_ENUM.A,SOME_ENUM.B);

...
...
}



<如果我试图坚持以上,我当然会得到例外。
我想使用@CollectionOfElements,但不推荐使用。是否有任何其他的@CollectionOfElements?



有没有一种方法可以在不写UserType的情况下将EnumSet存储在单个列中?



谢谢!

解决方案

hibernate没有内置的对这种东西的支持。请注意,在处理hibernate和集合时,你应该只是指定一个接口;在这种情况下设置。 Hibernate代理所有的集合,因此它可以有效地处理延迟加载。

这并不是说这很难。请参阅此文档



https:/ /forum.hibernate.org/viewtopic.php?p=2300843


How to store EnumSet in the DB (using Hibernate)?

@Entity
public class A
{
 public static enum SOME_ENUM { A, B, C };

 private EnumSet<SOME_ENUM> myEnumSet = EnumSet.of(SOME_ENUM.A, SOME_ENUM.B);

 ...
 ...
}

If I try to persist the above, I get exception of course. I wanted to use @CollectionOfElements, but it is deprecated. Is there any alternative of @CollectionOfElements?

Is there a way to store EnumSet in a single column without writing UserType?

Thanks!

解决方案

hibernate does not have built in support for such things. Note that when dealing with hibernate and collections you should really only be specifying an interface; in this case Set. Hibernate proxies all collections so it can efficiently deal with lazy loading.

This is not to say this is difficult. See this documentation

https://forum.hibernate.org/viewtopic.php?p=2300843

这篇关于在Hibernate中映射EnumSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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