-1

Please advise that what is DTO pattern as I came from my analysis is that it is data transfer object pattern , Can you please advise any example of it in Hibernate specially or in any other java example, Thanks

user1538526
  • 95
  • 7
  • 22

1 Answers1

3

A DTO is one of the work-arounds for the terrible persistence found in EJB 2.1 (collectively known as the Sun Blueprint Catalog). It is used for extracting data from a entity bean and passing it to the data layer. Data from that layer is placed in a DTO and put into the entity bean. This is only for bean managed persistence.

This is what was officially the meaning of a DTO. Today you also find this in transferring data from the back-end to the front-end (in client-server architecture).

If you want to use this in Hibernate, create a query/criteria for selecting the columns you need and then use the AliasToBeanResultTransformer for setting these columns in a DTO. An example in the Hibernate documentation:

List resultWithAliasedBean = 
    s.createCriteria(Enrolment.class)
        .createAlias("student", "st")
        .createAlias("course", "co")
        .setProjection(
            Projections.projectionList()
                add( Projections.property("co.description"), "courseDescription" )
        )
        .setResultTransformer( new AliasToBeanResultTransformer(StudentDTO.class) )
    .list();

StudentDTO dto = (StudentDTO)resultWithAliasedBean.get(0);
Jeroen
  • 979
  • 1
  • 7
  • 16
  • 1
    "A DTO is one of the work-arounds for the terrible persistence found in EJB 2.1 (collectively known as the Sun Blueprint Catalog)." I can find no reference for this, both the BluePrints site and P of EAA mention DTOs in relation to working with remoting. Also, even with a sane ORM, there's a case to be made for using DTOs - they specify exactly what data the tier calling the data access code needs, instead of always handing over the entire database. (This also helps prevent the N+1 problem.) Whether you use them depends on where on the scale of DRY versus explicitness your tastes lie. – millimoose Jul 31 '12 at 17:31
  • From [Wikipedia](http://en.wikipedia.org/wiki/Data_Transfer_Object): _"DTOs are often used in conjunction with data access objects to retrieve data from a database."_ is one example. I'm not saying there is absolutely no reason for a DTO, but I do think it is overused. – Jeroen Jul 31 '12 at 18:27
  • I'm not saying that's not a valid use case for a DTO, insofar as its reductionist definition is "an object that transfers data between application tiers". What I'm disputing is that it's intended as a work-around for anything. (Seeing as there are valid reasons to use it consciously.) – millimoose Jul 31 '12 at 19:50
  • Those valid reasons are not the reasons this type of object (I won't call it a pattern, since it isn't one) was created in the first place. If you look at the Blueprint Catalog, you'll find that the only purpose of those "patterns" is to make working with EJB easier. And the introduction of EJB 3 made some of them obsolete, which makes me wonder what kind of design pattern only makes sense in a deprecated technology (not talking about the DTO). You won't hear this from Sun/Oracle of course. I also can't see a reason why Martin Fowler would talk about design decisions in EJB. – Jeroen Aug 01 '12 at 09:59
  • [This StackOverflow answer](http://stackoverflow.com/questions/4558233/what-is-a-good-pattern-for-converting-between-hibernate-entities-and-data-transf#4558255) reflects my sentiment against DTO. I just don't agree with the last three sentences in last paragraph. – Jeroen Aug 01 '12 at 10:10