PropertyUtils.copyProperties复制对象失败
在跟着网上资源写个小项目时,运行测试类报这个错误,
org.springframework.beans.FatalBeanException: Could not copy property 'buyerAddress' from source to target; nested exception is java.lang.reflect.InvocationTargetException
找了很多的回答,第一种就是说在使用BeanUtils.copyProperties(model,target)对属性进行拷贝时,如果javaBean中的属性有基本类型,而model模型中对应的属性值为 null 的话,就会出现这个异常。
参考于:https://blog.youkuaiyun.com/rchm8519/article/details/7101911
第二种是BeanUtils.copyProperties深浅拷贝问题,我这里对bean属性进行复制,不是复制数组和集合。
参考于:https://www.cnblogs.com/JoeyWong/p/11011360.html
然后再看了一遍报错日志发现了另一个问题
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy [com.izgk.sell.dataobject.OrderMaster#1586938219267140779] - no Session
这是因为 getOne() 是 lazy load 的,而教程写的是findOne()。
所以我在配置文件application.yml中加上 spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true 就解决了。但是这个方法不是很推荐。
或者直接在对应的service方法加一个事务的注解@Transactional也可以
参考于: https://www.cnblogs.com/EasonJim/p/9773668.html
最后附上结果: