Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Beans VS Data Transfer Objects

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
Whats the difference between Java Beans VS Data Transfer Objects.
Are both the same or is one a superset of another.
Please note that im referring to Java Beans and not EJBs.
Thanks,
Saj
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Data Transfer object is a java bean which is transported across tier i.e. from presentation tier and EJB tier and vice versa.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sajee Joseph:
Whats the difference between Java Beans VS Data Transfer Objects.

An entity EJB is a Java class that maps to a persistence framework, typically some form of object-relational mapping tool or directly through your container. Being an EJB, it uses the container to access services like persistence, transactions, security, and the like. You should not normally access entity beans directly from your client application but rather through a session beans.
Typically in the presentation layer you need a different set of data about an entity bean -- either a subset or maybe a grouping of several beans together. The Data Transfer Object pattern is used here as a light-weight method of transfering the data you need between the two layers. Another example is value objects generated by XDoclet. These objects don't have any clue as to persistence, security, or transactions. They are simple collections of data -- you could use a Map or JavaBean or your own framework.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sajee Joseph:
Please note that im referring to Java Beans and not EJBs.

Originally posted by me:
An entity EJB ...

Yeah, space cadet, sorry.
To actually answer your question, a DTO is a pattern while a JavaBean is a specification. To be a JavaBean, your class must conform to a contract: a no-argument constructor and getters/setters for the attributes. A DTO is a pattern for shuttling data from various sources between your application layers (or across a network or whatever). You could use a JavaBean to implement the DTO pattern, or you could use something else (my DTO is based on a simple Map of fields without named accessors).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic