在 Hibernate 中注释枚举列表
我在 hibernat 中有这个注释用于枚举列表:
@CollectionOfElements(targetElement = SomeType.class)
@JoinTable(name = "table_name",
joinColumns = @JoinColumn(name = FK_NAME)
)
@Column(name = "someTypeCd", nullable = false)
@LazyCollection(LazyCollectionOption.FALSE)
@GeneratedValue(strategy=GenerationType.AUTO)
@Enumerated(EnumType.STRING)
private List<SomeType> someType;
问题是当我调用搜索查询执行时它返回这个:
java.lang.ClassCastException - java.lang.String cannot be cast to java.lang.Enum
问题在哪里?
I have this annotation in hibernat for list of Enums :
@CollectionOfElements(targetElement = SomeType.class)
@JoinTable(name = "table_name",
joinColumns = @JoinColumn(name = FK_NAME)
)
@Column(name = "someTypeCd", nullable = false)
@LazyCollection(LazyCollectionOption.FALSE)
@GeneratedValue(strategy=GenerationType.AUTO)
@Enumerated(EnumType.STRING)
private List<SomeType> someType;
And problem is when i call search query execution it return this :
java.lang.ClassCastException - java.lang.String cannot be cast to java.lang.Enum
Where is problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我通过寻找解决方案来到这里时:
为我完成了工作。感谢您对
@ElementCollection
的提示,我不知道这一点。您粘贴的片段可能会带来问题,即您使用
@Enumerated(EnumType.String)
将枚举值描述为 String 类型。我猜这会带来强制转换异常。也许EnumType.Ordinal
会比较合适。我对此没有经验。我不定义任何东西就很好。As I came here through searching for the solution:
did the job for me. Thanks for the hint to
@ElementCollection
I were not aware of it.Your pasted snippped might bring the problem, that you describe your enum value to be of type String with
@Enumerated(EnumType.String)
. I guss this brings in the cast exception. MaybeEnumType.Ordinal
would be suiting. I am not experienced with that. I am fine by not defining anything.更改 @Column 定义以包含定义的枚举值,例如
Change the @Column definition to include the defined enum values, e.g.