<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Five 's Weblog &#187; Data access object</title>
	<atom:link href="http://powerdream5.wordpress.com/tag/data-access-object/feed/" rel="self" type="application/rss+xml" />
	<link>http://powerdream5.wordpress.com</link>
	<description>It is not just another blog! 伍行天下！</description>
	<lastBuildDate>Wed, 28 Nov 2007 21:21:29 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='powerdream5.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/b83f3cb0955eab114902a009525db137?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Five 's Weblog &#187; Data access object</title>
		<link>http://powerdream5.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://powerdream5.wordpress.com/osd.xml" title="Five &#8217;s Weblog" />
		<item>
		<title>DAO pattern for Hibernate!</title>
		<link>http://powerdream5.wordpress.com/2007/10/13/dao-pattern-for-hibernate/</link>
		<comments>http://powerdream5.wordpress.com/2007/10/13/dao-pattern-for-hibernate/#comments</comments>
		<pubDate>Sat, 13 Oct 2007 03:20:25 +0000</pubDate>
		<dc:creator>powerdream5</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[DAO]]></category>
		<category><![CDATA[Data access object]]></category>
		<category><![CDATA[Hibernate]]></category>

		<guid isPermaLink="false">http://powerdream5.wordpress.com/2007/10/13/dao-pattern-for-hibernate/</guid>
		<description><![CDATA[        In J2EE field, open source framework is pretty hot. One of the advantages of them is that they are non-intrusive, because  you don&#8217;t need to make your classes extend any classes or implement any interfaces from the framework. However, when we use the ORM framework, such as Hibernate or JDO, we still need to use [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=powerdream5.wordpress.com&blog=1840509&post=45&subd=powerdream5&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://powerdream5.files.wordpress.com/2007/10/10_11_1_2007.gif" title="10_11_1_2007.gif"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_11_1_3.jpeg" title="10_11_1_3.jpeg"></a>        In J2EE field, open source framework is pretty hot. One of the advantages of them is that they are non-intrusive, because  you don&#8217;t need to make your classes extend any classes or implement any interfaces from the framework. However, when we use the ORM framework, such as Hibernate or JDO, we still need to use some specific classes of them. Therefore, in order to make the application more extensible, we should encapsulate the persistence layer of the system.</p>
<p>        DAO(data access object) pattern has already been proved to be a good way to seprate the business logic layer and the persistent layer. Today, I want to show an example of applying DAO pattern to Hibernate.</p>
<p>       Usually, a typical DAO pattern contains a factory class, some DAO interfaces and some DAO classes which implement those interface. Now, Let&#8217;s take a blog system for an example. Because I just want to show the structure, I will not present the detail of each class.</p>
<p><a href="http://powerdream5.files.wordpress.com/2007/10/10_11_1_2007.gif" title="10_11_1_2007.gif"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_11_1_2007.gif" title="10_11_1_2007.gif"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_11_1_2007.gif" title="10_11_1_2007.gif"></p>
<p style="text-align:center;"><img src="http://powerdream5.files.wordpress.com/2007/10/10_11_1_2007.gif" alt="10_11_1_2007.gif" /></p>
<p>      Firstly, we define the factory class:</p>
<p></a>public class DAOFactory{<br />
        private static DAOFactory daoFactory;<br />
        static{<br />
               daoFactory = new DAOFactory();<br />
        }<br />
       <font color="#ff0000"> //Define the factory method, return the instance of specific classes<br />
</font>        public DAOFactory getInstance(){<br />
               return daoFactory;<br />
        }<br />
        public BlogDAO getBlogDAO(){<br />
               return new BlogDAOImpl();<br />
        }<br />
         public BlogDAO getCommentDAO(){<br />
               return new CommentDAOImpl();<br />
        }<br />
}</p>
<p>       Secondly, we define the DAO interfaces. The purpose of interfaces is to make our applicaiton more extensible. For example, if we want to change the hibernate framework to the JDO framework in the future, we don&#8217;t need to make any change to the business logic, just changing the implementation of the interface works well.</p>
<p><font color="#ff0000">//Data access object interface <br />
</font>public interface BlogDAO{<br />
      <font color="#ff0000">//Blog are an persistence class<br />
      //define the method for inserting a blog to the system<br />
</font>      public boolean insertBlog(Blog blog);<br />
     <font color="#ff0000">//define the method to read a blog from the system according the id</font><br />
          public Blog readBlog(Long blogId);<br />
}</p>
<p>      Then, make an implementation to this interface:</p>
<p><font color="#ff0000">//Data access object <br />
</font>public class BlogDAOImpl implements BlogDAO{<br />
      public boolean insertBlog(Blog blog){<br />
             Session session = HibernateUtil.getSession();<br />
             &#8230;.<br />
      }<br />
     public Blog readBlog(Long blogId){<br />
             Session session = HibernateUtil.getSession();<br />
             &#8230;. <br />
      }<br />
}</p>
<p><font color="#ff0000">//HibernateUtil is a help class</font><br />
public class HibernateUtil{<br />
        private static SessionFactory sessionFactory;<br />
        static{<br />
                sessionFactory = &#8230;.;<br />
         }<br />
        public static Session getSession(){<br />
               return sessionFactory.openSession();<br />
        }<br />
}</p>
<p>      Now, we can create a class for testing:<br />
public class Test{<br />
     public static void main(String[] args){<br />
             <font color="#ff0000">//insert a blog to the database<br />
            //assume that we already get an instance of Blog class named blog</font><br />
            BlogDAO blogDAO = DAOFactory.getInstance().getBlogDAO();<br />
            blogDAO.inserBlog(blog);<br />
     }<br />
}</p>
<p>      However, there are still some drawbacks of DAO pattern. For example, we always have to get an instance of Session at the beginning of each method of the class BlogDAOImpl, which make us write the same code. Fortunately, we can make some advances to this program, for instance, we can adopt spring framework for configuring the relationship between each class.</p>
<p><a href="http://powerdream5.files.wordpress.com/2007/10/10_11_1_3.jpeg" title="10_11_1_3.jpeg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_11_1_3.jpeg" title="10_11_1_3.jpeg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/10_11_1_3.jpeg" title="10_11_1_3.jpeg"></p>
<p style="text-align:center;"><img src="http://powerdream5.files.wordpress.com/2007/10/10_11_1_3.jpeg" alt="10_11_1_3.jpeg" /></p>
<p></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/powerdream5.wordpress.com/45/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/powerdream5.wordpress.com/45/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/powerdream5.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/powerdream5.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/powerdream5.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/powerdream5.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/powerdream5.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/powerdream5.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/powerdream5.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/powerdream5.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/powerdream5.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/powerdream5.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=powerdream5.wordpress.com&blog=1840509&post=45&subd=powerdream5&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://powerdream5.wordpress.com/2007/10/13/dao-pattern-for-hibernate/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a0819a06b6ff4afc4f3e0a1977e6a5a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">powerdream5</media:title>
		</media:content>

		<media:content url="http://powerdream5.files.wordpress.com/2007/10/10_11_1_2007.gif" medium="image">
			<media:title type="html">10_11_1_2007.gif</media:title>
		</media:content>

		<media:content url="http://powerdream5.files.wordpress.com/2007/10/10_11_1_3.jpeg" medium="image">
			<media:title type="html">10_11_1_3.jpeg</media:title>
		</media:content>
	</item>
	</channel>
</rss>