本文介绍了什么是hibernate实体和数据传输对象之间转换的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的问题和关注,如何在Hibernate实体和数据传输对象之间转换由Web服务返回,如这个问题所讨论的:





这里提到的一个因素是,如果域模型发生变化,一组DTO将在Web服务的情况下保护消费者。 >

即使它似乎会在我的项目中添加大量的代码,这个推理看起来似乎是正确的。



是有一个很好的设计模式,我可以用来将一个Hibernate实体(实现一个接口)转换为实现相同接口的DTO?



所以假设以下两个实现书,我需要将BookEntity.class转换为BookDTO.class,以便我可以让JA XB序列化和返回。



再次,这整个前景对我来说似乎是可疑的,但是如果有好的模式帮助处理这个转换,我会喜欢得到一些洞察力。



有没有可能有一些有趣的方式通过反射转换?还是一个建设者模式,我不在想?



我应该忽略DTO模式并传递实体吗?

解决方案

My preference is usually "yes". I don't like the idea of parallel hierarchies created just for the sake of architectural or layer purity.

The original reason for the DTO pattern was excessive chattiness in EJB 1.0 and 2.0 apps when passing entity EJBs to the view tier. The solution was to put the entity bean state into a DTO.

Another reason that's usually given for creating DTOs is to prohibit modification by the view layer. DTOs are immutable objects in that case, with no behavior. They do nothing but ferry data to the view layer.

I would argue that DTO is a Core J2EE pattern that's become an anti-pattern.

I realize that some people would disagree. I'm simply offering my opinion. It's not the only way to do it, nor necessarily the "right" way. It's my preference.

这篇关于什么是hibernate实体和数据传输对象之间转换的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 14:41