大佬教程收集整理的这篇文章主要介绍了Prevent Dozer from triggering Hibernate lazy loading,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
public class MyCustomFIEldMapper implements CustomFIEldMapper
{
public Boolean mapFIEld(Object source, Object desTination, Object sourceFIEldValue, Classmap classmap, FIEldMap fIEldMapPing)
{
// check if fIEld is a Hibernate collection proxy
if (!(sourceFIEldValue instanceof AbstractPersistentCollection)) {
// Allow dozer to map as normal
return false;
}
// check if fIEld is already initialized
if (((AbstractPersistentCollection) sourceFIEldvalue).wasInitialized()) {
// Allow dozer to map as normal
return false;
}
// Set desTination to null, and tell dozer that the fIEld is mapped
desTination = null;
return true;
}
}
@H_262_2@这将返回任何未初始化的PersistentSet对象为null。我这样做是为了将它们传递给客户端时,可以区分NulL(未加载)集合和空集合。这使我可以在客户端中定义通用行为,以使用预加载的集合,或进行另一个服务调用以检索集合(如果需要)。此外,如果您决定在服务层中急于加载任何集合,那么它们将照常进行映射。
@H_262_2@我使用spring注入自定义字段映射器:
<bean ID="dozerMapper" class="org.dozer.DozerBeanMapper" lazy-init="false">
<property name="mapPingfiles">
...
</property>
<property name="customFIEldMapper" ref="dozerCustomFIEldMapper" />
</bean>
<bean ID="dozerCustomFIEldMapper" class="my.project.MyCustomFIEldMapper" />
@H_262_2@我希望这对任何寻求解决方案的人有所帮助,因为我在搜索Internet时@R_673_9112@示例。
PlatformtransactionManager tm = (PlatformtransactionManager) SingletonFactoryProvider.getSingletonFactory().getSingleton("transactionManager");
tm.commit(tm.gettransaction(new DefaulttransactionDefinition()));
@H_262_2@我不知道事务会发生什么,但是Hibernate会话不会关闭,并且仍然会发生延迟加载。
@H_262_2@我尝试了这个:
SessionFactory sf = (SessionFactory) SingletonFactoryProvider.getSingletonFactory().getSingleton("sessionFactory");
sf.getCurrentSession().clear();
sf.getCurrentSession().close();
@H_262_2@而且它可以防止延迟加载,但是在直接在应用程序层(在我的项目中称为“ facade”)中操纵会话是否是一种好习惯?我应该担心哪些副作用?(我已经看到涉及POJO-> DTO转换的测试无法再通过AbstracttransactionnalDatasource Spring测试类启动,因为此类试图触发不再与活动会话链接的事务的回滚)。
@H_262_2@我还尝试将传播设置为NOT_SUPPORTED或requIRES_NEW,但它会重用当前的Hibernate会话,并且不会阻止延迟加载。
以上是大佬教程为你收集整理的Prevent Dozer from triggering Hibernate lazy loading全部内容,希望文章能够帮你解决Prevent Dozer from triggering Hibernate lazy loading所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。