<?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-4267630802587505626</id><updated>2011-11-21T13:49:07.823-08:00</updated><category term='local classes'/><category term='final variables'/><title type='text'>Pouring thoughts on Java...</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://sumit2java.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4267630802587505626/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://sumit2java.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Sumit Sengar</name><uri>http://www.blogger.com/profile/12567099389716303111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4267630802587505626.post-5391229919272644014</id><published>2009-09-20T10:35:00.000-07:00</published><updated>2009-09-20T23:57:06.229-07:00</updated><title type='text'>Calling Stored Procedures via Spring API</title><content type='html'>To call database stored procedure from a Java application, the normal way is to make use of JDBC API via CallableStatement. Coming of the age, Spring comes with Object level representation of Stored Procedure. Welcome to StoredProcedure class. This class gives us an object oriented abstraction of Stored procedures. Rather than making use of native JDBC API to call a stored proc, create and instance of StoredProcedure class and pass on the procedure parameters to it. Lets quickly get into an example.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt; Lets say we have a procedure calculateTax, which takes yearly income as the parameter and returns the tax amount. One approach is you may create a custom stored procedure class called TaxStoredProc , which can be used by other parts of your application which are interested in invoking this procedure.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;public class TaxStoredProc extends StoredProcedure{&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;public TaxStoredProc(){&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;setSql("calculateTax");&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;DataSource ds = getDataSource();&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;setDataSource(ds);&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;public Double calculateTax(double income){&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;declareParams();&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;Map params = new HashMap();&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;params.put("income", income);&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;Map output = execute(params);&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;//this map out contains out put result.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;// value of the stored proc is stored in the out put parameter "result"&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;Double result = (Double)output.get("result");&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;return result;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;private void declareParams() {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;declareParameter(new SqlParameter("income",Types.DOUBLE));&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;// out put parameter which will collect the result.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;declareParameter(new SqlOutParameter("result",Types.DOUBLE));&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Code is simple. Extend StoredProcedure class with your custom class. while instantiating pass on the name of the procedure by calling setSql(procName), which is a method of the super class StoredProcedure hierarchy. Get the dataSource object (It depends on how you want to get it...so not specified here.) and set it. Create input parameters and output parameter for result and just call execute method passing the parameter map.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;By this we have created a wrapper over the database stored proc called calculateTax.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Say from some part of your application one is required to call this proc,  then he/she is not supposed to be aware of the proc name and write any jdbc api to call this. One can simply make use of this as :&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;TaxStoredProc proc = new TaxStoredProc ();&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Double tax = proc.calculateTax(5689.45);&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Just a two line of code and done.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Extending this set up would also help you develop Junit test cases for testing stored procedures. The test case writer has nothing to worry about the JDBC api semantics, and can simply instantiate our wrapper StoredProcedure instances and call methods on it.&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4267630802587505626-5391229919272644014?l=sumit2java.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sumit2java.blogspot.com/feeds/5391229919272644014/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sumit2java.blogspot.com/2009/09/calling-stored-procedures.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4267630802587505626/posts/default/5391229919272644014'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4267630802587505626/posts/default/5391229919272644014'/><link rel='alternate' type='text/html' href='http://sumit2java.blogspot.com/2009/09/calling-stored-procedures.html' title='Calling Stored Procedures via Spring API'/><author><name>Sumit Sengar</name><uri>http://www.blogger.com/profile/12567099389716303111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4267630802587505626.post-8736550129146346785</id><published>2009-09-04T02:23:00.000-07:00</published><updated>2009-09-04T05:38:40.605-07:00</updated><title type='text'>Bidirectional Map</title><content type='html'>The most common hits we make to a map is to retrieve the value of a mapping based on a key. Now suppose, we have a collection of values and we need to fetch their corresponding keys from the map. Plain old way iterate over the entry set in the map, get their values and then corresponding keys. Apache commons library has pretty cool utility methods for jdk collections API. One of them is a Bidirectional Map, with the interface as BidiMap. This map allows bidirectional lookup between key and values. Calling bidimap.getKey(Object value) gives you the corresponding key for the passed on value. Other useful interfaces like OrderdMap allows you to maintain ordering in the map based data structures. Other interesting feature comes in the form of Bag implementation. Bag is a data structure that stores an object along with its cardinality(number of occurences)  .&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4267630802587505626-8736550129146346785?l=sumit2java.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sumit2java.blogspot.com/feeds/8736550129146346785/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sumit2java.blogspot.com/2009/09/bidirectional-map.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4267630802587505626/posts/default/8736550129146346785'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4267630802587505626/posts/default/8736550129146346785'/><link rel='alternate' type='text/html' href='http://sumit2java.blogspot.com/2009/09/bidirectional-map.html' title='Bidirectional Map'/><author><name>Sumit Sengar</name><uri>http://www.blogger.com/profile/12567099389716303111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4267630802587505626.post-4680113744241185501</id><published>2009-08-08T07:49:00.000-07:00</published><updated>2009-08-08T08:05:47.257-07:00</updated><title type='text'>Where Oracle IN operator fails...</title><content type='html'>I figured this out while working in one of my recent projects. Implementing the data access layer for search use cases through Spring's Hibernate support framework, I require to create a lot of restrictions (Hibernate Criterion API). This most of the time requires to create a Restrictions.sqlRestriction criterion for certain search criteria.  I had a condition to create a restriction like this &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;DetachedCriteria criteria = DetachedCriteria.forClass(Client.class);&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;criteria.add(Restrictions.in("clientId",getClientIds()) );&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I created a seperate method getClientIds which gives back a list of clientIds. The problem occured when the number of clientIds returned by getClientIds method is more than 1000. IN operator against oracle fails with an exception if the number of items in the IN operator is &gt; 1000. I got over this problem by replacing the clientId IN (id1, id2, id3,...id1000) kind of clause by clientId IN (innerQuery).&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4267630802587505626-4680113744241185501?l=sumit2java.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sumit2java.blogspot.com/feeds/4680113744241185501/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sumit2java.blogspot.com/2009/08/where-oracle-in-operator-fails.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4267630802587505626/posts/default/4680113744241185501'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4267630802587505626/posts/default/4680113744241185501'/><link rel='alternate' type='text/html' href='http://sumit2java.blogspot.com/2009/08/where-oracle-in-operator-fails.html' title='Where Oracle IN operator fails...'/><author><name>Sumit Sengar</name><uri>http://www.blogger.com/profile/12567099389716303111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4267630802587505626.post-7984920252401510222</id><published>2009-08-05T12:01:00.000-07:00</published><updated>2009-08-07T13:45:37.348-07:00</updated><title type='text'>Y Cloneable?</title><content type='html'>We all have heard and quite often implemented and worked with Cloneable interface. Working with it quite recently encountered something that easily gets ignored while reading about Cloneable. Delving a bit deeper into it ,I figured out that if we have a basic requirment of creating a shallow copy of our custom class, why not define our own method with the name clone or can even technically override the one from Object's class, without calling the conventional super.clone() statement?&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For eg: I have a class called User.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;public class User{&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;private String firstName;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;private String lastName;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;private int age;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;// a constructor to initialize an object&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;public User(String fName, String lName, int age){//code goes here}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Now if I need to return a clone of this object, I can always define/override the one from object class as&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;@Override&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;public Object clone(){&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;return new User(this.firstName, this.lastName, this.age);&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;My idea is that making your class implement Cloneable does not trigger anything automatic in the jvm. The semantics of this interface make sense, if you call Object's class clone method via super.clone(). Because Object's clone method, first makes a check if the calling class has implemented Cloneable , otherwise throws a checked exception CloneNotSupported. So implementing Cloneable interface for your class is more like adhearing to some java object oriented guidlines, saying that one can clone any object if its class has implemented this interface, else throw an exception. This kind of enforces a restriction on the callers, to carefully make a wise decision before making a clone of the object. But the other thing which comes to my mind is that, if I do not want others to make a copy of my object why would I need to expose a 'clone' like method in my class? This is because if I do not write any clone method in my User class above, compiler will not let you call user.clone() saying that "method clone from type Object is not visible". Then whole point of writing a clone method in my User class and to implement this restriction by throwing CloneNotSupportedException makes an additional exercise.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So the whole point is that if I really want to expose cloning of my objects, making a call to Object's clone would give me a template shallow copy of my object, which I could further extend as per requirment to create a deep copy. And to do so, I need to make my class implement Cloneable, otherwise a call to super.clone() would fail. So override clone whenever you seriosuly need to generate a copy of it.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4267630802587505626-7984920252401510222?l=sumit2java.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sumit2java.blogspot.com/feeds/7984920252401510222/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sumit2java.blogspot.com/2009/08/y-not-cloneable.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4267630802587505626/posts/default/7984920252401510222'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4267630802587505626/posts/default/7984920252401510222'/><link rel='alternate' type='text/html' href='http://sumit2java.blogspot.com/2009/08/y-not-cloneable.html' title='Y Cloneable?'/><author><name>Sumit Sengar</name><uri>http://www.blogger.com/profile/12567099389716303111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4267630802587505626.post-1698950398535069145</id><published>2009-06-24T03:44:00.000-07:00</published><updated>2009-08-07T13:39:50.149-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='local classes'/><category scheme='http://www.blogger.com/atom/ns#' term='final variables'/><title type='text'></title><content type='html'>&lt;p&gt;&lt;span style="font-family:arial;color:#cc0000;"&gt;&lt;strong&gt;Why local classes access only final variables?&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:#000000;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Local classes in java are of big help and a nice utility to work with making your code more encapsulated and arranged. Anonymous local inner classes is a flavour that most of us use pretty often. Now there is a strong mandate that runs with this concept, they can use only final variables of the method in which they are declared. Now why 'final', and why not non-final variables?&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;The reason gets clear when we delve a bit deeper and find out how local classes works.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;An anonymous local class can use local variables because the compiler automatically gives the class a private instance field to hold a copy of each local variable the class uses. The compiler also adds hidden parameters to each constructor to initialize these automatically created private fields. Thus, a local class does not actually access local variables, but merely its own private copies of them. The only way this can work correctly is if the local variables are declared final, so that they are guaranteed not to change. With this guarantee in place, the local class is assured that its internal copies of the variables accurately reflect the actual local variables.&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4267630802587505626-1698950398535069145?l=sumit2java.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sumit2java.blogspot.com/feeds/1698950398535069145/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sumit2java.blogspot.com/2009/06/why-local-classes-access-only-final.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4267630802587505626/posts/default/1698950398535069145'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4267630802587505626/posts/default/1698950398535069145'/><link rel='alternate' type='text/html' href='http://sumit2java.blogspot.com/2009/06/why-local-classes-access-only-final.html' title=''/><author><name>Sumit Sengar</name><uri>http://www.blogger.com/profile/12567099389716303111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
