Dozer:Hibernate PersistentMap 未映射到 java.util.HashMap
我有一个带有字段的 Hibernate 带注释的实体:
@OneToMany(mappedBy="templateInstance", fetch = FetchType.EAGER)
@MapKey(name = "attributeName")
private Map<String, Component> components;
Hibernate 从中创建了一个 PersistentMap。 然后我希望 Dozer 将其映射到具有这样一个字段的对象:
private Map<String, ComponentDto> components;
在进行 LazyInitializationExceptions 和一些调试之后,我发现这不是关闭的 Hibernate 会话的问题,而是 Dozer 尝试不映射到 HashMap 但到持久映射!因此,当 Dozer 访问目标映射时,PersistentMap 会抛出异常,因为它当然没有会话。
所以,我认为 Dozer 的意图行为是映射到 HashMap。现在的问题是:我做错了什么还是 Dozer 以及如何让 Dozer 将 PersistentMap 映射到普通的 Java 标准映射?
我之前使用 List 时没有任何问题。还有其他人使用 Dozer 来映射 Hibernate PersistentMap 吗?
问候, 孔苏米耶尔
I have a Hibernate annotated entity with a field:
@OneToMany(mappedBy="templateInstance", fetch = FetchType.EAGER)
@MapKey(name = "attributeName")
private Map<String, Component> components;
Hibernate makes a PersistentMap out of this.
Then I want Dozer to map this to an object with such a field:
private Map<String, ComponentDto> components;
After having LazyInitializationExceptions and some debugging, I found out, that it´s not a problem of a closed Hibernate session, but that Dozer tries not to map to a HashMap but to a PersistentMap! And therefore when Dozer accesses the target map the PersistentMap throws the Exception, because it has no session of course.
So, I thought that the intented behaviour of Dozer is to map to a HashMap. Now the question: am I doing something wrong or Dozer and how can I get Dozer to map the PersistentMap to normal Java standard map?
I had no problems when using a List before. Has anybody else used Dozer for mapping Hibernate PersistentMap?
Regards,
Konsumierer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己找到了解决方案。 DTO 中的 Components 字段必须如下所示:
只有这样才能防止 Dozer 使用 PersistentMap 作为目标对象。
另一件重要的事情是为 DO 使用自定义 BeanMappingBuilder 来保存地图,如下所示:
有关该问题的更多信息,请参阅 Dozer 论坛 https://sourceforge.net/projects/dozer/forums/forum/452530/topic/4020856/index/page /1
I found the solution by myself. The components field in the DTO has to look like this:
Only this way you can prevent Dozer from using the PersistentMap as the target object.
And the other important thing is to use a custom BeanMappingBuilder for the DO that holds the map and that looks like that:
For further information about that problem, see the Dozer forums at https://sourceforge.net/projects/dozer/forums/forum/452530/topic/4020856/index/page/1