<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3921307291004200144</id><updated>2011-05-24T07:02:19.971-07:00</updated><title type='text'>java developer notes</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://ivanobulo.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3921307291004200144/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://ivanobulo.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ivan Luzyanin</name><uri>http://www.blogger.com/profile/09379224911633923823</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='http://2.bp.blogspot.com/-ZBXPR3AY_-s/Tdu6Kqr6e8I/AAAAAAAABnQ/zXHUhlFklZY/s220/me_180120111786.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3921307291004200144.post-7648173205496141438</id><published>2008-10-07T00:10:00.000-07:00</published><updated>2008-10-07T01:20:41.904-07:00</updated><title type='text'>Use criterias to enhance the search of your DAOs</title><content type='html'>I was reviewing the DZone today and found the blog entry &lt;a onclick="track(this, 'who_needs_implementations', ''); " href="http://www.altuure.com/2008/10/02/who-needs-implementations/"&gt;who needs implementations?&lt;/a&gt; It is interesting approach but in my opinion it is nothing then just "unusual". I think that it will take same amount of time writing method and parameter annotation as it would take to write an actual implementation.&lt;br /&gt;&lt;br /&gt;But let's postpone the discussion about it. I'd like to share my approach I used on several projects.&lt;br /&gt;&lt;br /&gt;Here the common DAO interface:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;public interface UserDAO extends GenericDAO&lt;user,&gt; {&lt;/user,&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;GenericDAO has common methods like save, delete, update, etc.&lt;br /&gt;&lt;br /&gt;In most cases you need some search methods and most developers are usually creating numerous of methods to perform all types of different search, like findByName , findByNameAndEmail, findByUsername etc. Also these methods are usually have "twins" methods for counting results.&lt;br /&gt;&lt;br /&gt;Some smarter developers using "example" beans for searching. But you should be very careful because of the default values assigned to the bean properties.&lt;br /&gt;&lt;br /&gt;I don't like both approaches, I like "Hibernate Criteria API" approach. But because it is very low level and because I do not wish to expose third-party API in my DAOs I need additional level of abstraction over it.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public interface SearchCriteria {&lt;br /&gt;  int count();&lt;br /&gt;  List find();&lt;br /&gt;  List find(int fromIdx, int maxCount);&lt;br /&gt;  T findUnique();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public interface UserSearchCriteria extends SearchCriteria{&lt;br /&gt;  UserSearchCriteria addUsernameRestriction(String username);&lt;br /&gt;  UserSearchCriteria addEMailRestriction(String email);&lt;br /&gt;  ...&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So your final code might look like this:&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;pre&gt;&lt;br /&gt;UserDAO userDAO = ...;&lt;br /&gt;UserSearchCriteria criteria = userDAO.createCriteria();&lt;br /&gt;criteria.addUsernameRestriction("vasya").addEMailRestriction("pupkin@gdeto.com");&lt;br /&gt;&lt;br /&gt;//To get amount of results that criteria is able to find&lt;br /&gt;int amount = criteria.count();&lt;br /&gt;&lt;br /&gt;//To display results for page number "pageNum"&lt;br /&gt;List&lt;user&gt; users = criteria.find((pageNum - 1) * perPageCount, perPageCount);&lt;br /&gt;&lt;/user&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;I'm not showing the actual implementation since it very straightforward if you are using hibernate criteria API. You can actually use SQL/HQL if you like but it will be more complicated to join strings into final query.&lt;br /&gt;&lt;br /&gt;You may also be able to "detach" your criterias so they can be stored in HTTP session rather then storing values and re-create your search over again on the next request. you are also free to add ordering and whatever you need to fine your search.&lt;br /&gt;&lt;br /&gt;So who needs implementations?&lt;br /&gt;&lt;br /&gt;I need!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3921307291004200144-7648173205496141438?l=ivanobulo.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ivanobulo.blogspot.com/feeds/7648173205496141438/comments/default' title='Дописати коментарі'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3921307291004200144&amp;postID=7648173205496141438' title='0 коментарі(в)'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3921307291004200144/posts/default/7648173205496141438'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3921307291004200144/posts/default/7648173205496141438'/><link rel='alternate' type='text/html' href='http://ivanobulo.blogspot.com/2008/10/use-criterias-to-enhance-search-of-your.html' title='Use criterias to enhance the search of your DAOs'/><author><name>Ivan Luzyanin</name><uri>http://www.blogger.com/profile/09379224911633923823</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='http://2.bp.blogspot.com/-ZBXPR3AY_-s/Tdu6Kqr6e8I/AAAAAAAABnQ/zXHUhlFklZY/s220/me_180120111786.jpg'/></author><thr:total>0</thr:total></entry></feed>
