推土机:Hibernate PersistentMap没有映射到java.util.HashMap [英] Dozer: Hibernate PersistentMap is not mapped to java.util.HashMap

查看:370
本文介绍了推土机:Hibernate PersistentMap没有映射到java.util.HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有字段的Hibernate注释实体:

$ $ p $ @OneToMany(mappedBy =templateInstance,fetch = FetchType。 EAGER)
@MapKey(name =attributeName)
私人地图< String,Component>组件;

Hibernate使PersistentMap不在此列。
然后我想让Dozer把它映射到一个有这样一个字段的对象:

  private Map< String,ComponentDto>组件; 

在LazyInitializationExceptions和一些调试之后,我发现它不是关闭的问题Hibernate会话,但Dozer尝试不映射到HashMap而是映射到PersistentMap!因此,当Dozer访问目标映射时,PersistentMap会抛出Exception,因为它当然没有会话。

所以,我认为Dozer的内部行为是映射到一个HashMap。现在的问题是:我做了什么错误或Dozer,我如何让Dozer将PersistentMap映射到普通Java标准映射?

使用List时没有问题之前。有没有其他人使用Dozer来映射Hibernate PersistentMap?



问候,
Konsumierer

解决方案

我自己找到了解决方案。 DTO中的组件字段必须如下所示:

  HashMap< String,Component> components = new HashMap< String,Component> ;; 

只有这样才能防止Dozer将PersistentMap作为目标对象。



另一个重要的事情是使用一个自定义的BeanMappingBuilder来存放映射,看起来像这样:

  public class TemplateInstanceMappingBuilder extends BeanMappingBuilder {
$ b @Override
protected void configure(){

mapping(TemplateInstance.class,TemplateInstanceDto。 class)
.fields(components,components,FieldsMappingOptions.hintB(ComponentDto.class));





有关该问题的更多信息,请参阅 https:// sourceforge中的Dozer论坛。 net / projects / dozer / forums / forum / 452530 / topic / 4020856 / index / page / 1

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

解决方案

I found the solution by myself. The components field in the DTO has to look like this:

HashMap<String, Component> components = new HashMap<String, Component>;

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:

public class TemplateInstanceMappingBuilder extends BeanMappingBuilder {

    @Override
    protected void configure() {

        mapping(TemplateInstance.class, TemplateInstanceDto.class)
            .fields("components", "components", FieldsMappingOptions.hintB(ComponentDto.class));

    }
}

For further information about that problem, see the Dozer forums at https://sourceforge.net/projects/dozer/forums/forum/452530/topic/4020856/index/page/1

这篇关于推土机:Hibernate PersistentMap没有映射到java.util.HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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