DozerMapperUtil.map 引发的问题

废话不多说直接上代码

/**
	 * 将source的所有属性拷贝至target,source里没有的字段,target里不覆盖
	 * @param source
	 * @param target
	 * @return
	 */
	public static <U> void map(final Object source, final U target) {
		DozerBeanMapper mapper = new DozerBeanMapper();
		mapper.addMapping(new BeanMappingBuilder() {
			@Override
			protected void configure() {
				mapping(source.getClass(), target.getClass(),
						new TypeMappingOption[] {TypeMappingOptions.mapNull(false) });
			}

		});
		mapper.map(source, target);
	}

使用:DozerMapperUtil.map(entity, entityOld);
entity对象中有子对象集合
BoJob entityOld = this.findOne(entity.getJobUUID());
entityOld :使用jpa查询到的对象

原始实体类,子字段

  @OneToMany(fetch=FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    @Where(clause="isValid=1")
    @JoinColumn(name = "jobUUID", referencedColumnName = "jobUUID",insertable = false, updatable = false)
    public List<BoJobServer> getBoJobServerList() {        return boJobServerList;    }

    public void setBoJobServerList(List<BoJobServer> boJobServerList) {       this.boJobServerList = boJobServerList;    }

测试代码

 @Test
    public void saveAllJpa(){

        //模拟登陆用户
        SysUser sysUser = new SysUser();

        SysFramework sysFramework = new SysFramework();
        sysFramework.setJobNoCode("SH");

        sysUser.setBranchFramework(sysFramework);
        sysUser.setUserId(60);
        sysUser.setUserNameEn("cxy");
        sysUser.setCreationTime("2012-12-12");


        BoJob boJob = new BoJob();
        boJob.setJobUUID("40288b8172275b3f0172275b66b50000");
        boJob.setJobNoHead("66");
        boJob.setJobType(2);


        BoJobServer boJobServer = new BoJobServer();
//        boJobServer.setJobServerUUID("4028728171634772017163a9cd140005");

        ArrayList<BoJobServer> boJobServers = new ArrayList<>();
        boJobServers.add(boJobServer);

        boJob.setBoJobServerList(boJobServers);

        BoJob boJob1 = boJobJpaService.saveAll(boJob, sysUser);
    }

错误:

org.dozer.MappingException: java.lang.reflect.InvocationTargetException

debug模式下,子集合显示错误

Unable to evaluate the expression Method threw ‘org.hibernate.LazyInitializationException’ exception

修改方式为:把懒加载修改为及时加载
@OneToMany(fetch=FetchType.EAGER)

之后又报错:

cannot simultaneously fetch multiple bags

修改:添加 @Fetch(FetchMode.SUBSELECT)

大功告成:觉得对你有帮助,帮忙点个赞吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值