对使用 @ElementCollection 和 Enums 的类执行合并/更新时出现 Hibernate ClassCastException
我有一个类在保存时给出以下异常。这很令人费解,因为我可以很好地检索数据。即使我对已检索的实体执行合并/更新(并进行任何更改),该错误仍然会发生。
ServerStatusException data:
ASObject(26024375){message=java.lang.String cannot be cast to java.lang.Enum,
rootCause=ASObject(9663101){message=java.lang.String cannot be cast to
java.lang.Enum, localizedMessage=java.lang.String cannot be cast to
java.lang.Enum, cause=null}, details=java.lang.ClassCastException,
code=Server.Processing} HttpResponseInfo: HttpResponseInfo code: 200 message: OK
这是属性定义:
@ElementCollection(targetClass=FilePrivilegeEnum.class, fetch=FetchType.EAGER)
@JoinTable(name="edrfiletyperoleprivilegelink", joinColumns={@JoinColumn(name="file_type_role_id")})
@Column(name="privilege", nullable = false)
@Enumerated(EnumType.STRING)
private Set<FilePrivilegeEnum> privileges = new HashSet<FilePrivilegeEnum>();
public Set<FilePrivilegeEnum> getPrivileges() { return this.privileges; }
public void setPrivileges (Set<FilePrivilegeEnum> privileges) { this.privileges = privileges; }
这是特权等级:
public enum FilePrivilegeEnum {
FILE_VIEW,
FILE_CREATE,
FILE_AMEND,
FILE_DELETE;
}
I have a class that gives the exception below when saving. This is puzzling because I can retrieve the data fine. The error still occurs even if I perform a merge / update on an entity I have retrieved (with making any changes).
ServerStatusException data:
ASObject(26024375){message=java.lang.String cannot be cast to java.lang.Enum,
rootCause=ASObject(9663101){message=java.lang.String cannot be cast to
java.lang.Enum, localizedMessage=java.lang.String cannot be cast to
java.lang.Enum, cause=null}, details=java.lang.ClassCastException,
code=Server.Processing} HttpResponseInfo: HttpResponseInfo code: 200 message: OK
Here is the property definition:
@ElementCollection(targetClass=FilePrivilegeEnum.class, fetch=FetchType.EAGER)
@JoinTable(name="edrfiletyperoleprivilegelink", joinColumns={@JoinColumn(name="file_type_role_id")})
@Column(name="privilege", nullable = false)
@Enumerated(EnumType.STRING)
private Set<FilePrivilegeEnum> privileges = new HashSet<FilePrivilegeEnum>();
public Set<FilePrivilegeEnum> getPrivileges() { return this.privileges; }
public void setPrivileges (Set<FilePrivilegeEnum> privileges) { this.privileges = privileges; }
And here is the privilege class:
public enum FilePrivilegeEnum {
FILE_VIEW,
FILE_CREATE,
FILE_AMEND,
FILE_DELETE;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为用以下内容替换 @ElementCollection 可以解决问题:
I think that replacing your @ElementCollection with the following will solve the problem: