Saturday, October 6, 2007

Active Record v/s Hibernate - Round I

You will have heard of shiny new web application framework called Ruby-on-Rails also known as RoR or simply Rails. It provides complete stack from ORM framework to Web Service. From this post onwards for next couple of posts I will compare and contrast between different components of JEE and Rails. I will begin with Hibernate from JEE and Active Record from Rails. The Round I of the Active Record v/s Hibernate will be on the basis of Mapping

The very first aspect is mapping. Hibernate requires the POJO (Java objects that represents the tables) be mapped with the tables using XML. That means if there is even a slight change in the Database schema such as the length of a particular field is changed, then you will have to make changes not only to the corresponding POJO but also to the mapping file. If you fail to do so, then run-time exception is the most common and the least problem that you will face.

Now lets look at Active Record. The driving principle of Rails is "Convention-over-Configuration" (but version 2.0 is deviating from it, more on that later). So , to map a class to a database table you just have to follow conventions. The conventions are

  1. The class derives from ActiveRecord::Base i.e. Base class of ActiveRecord module
  2. The name of the class must be same as that of the table
  3. The name of the class should start with a capital letter (though this applies to all Ruby classes)
Thats it. No XML, no configuration. Even if there is a change in the table, the corresponding class will adapt to it automatically at runtime through reflection and meta-programming. So that is the difference between Active Records and Hibernate on the basis of Mapping. In the next post I will continue comparing the two on how ORM classes are defined.

Authors Note: I am really sorry for the gap between the posts. It wont happen again (this time for sure). For now on you can expect atleast one post in a week.

Thursday, October 4, 2007

Accessing Managed Beans from backing bean


Sun created JSF as a competition for ASP.net. Though the idea is good, yet, the Sun stuck with the aspect of configuration. So if I need the framework to manage any of my beans automatically, then I need to specify it in faces-config.xml. Lets say my bean is named ResultBean, then I will have to put it as follows

<managed-bean>


<managed-bean-name>Result</managed-bean-name>

<managed-bean-class>org.me.ResultBean</managed-bean-class<

<managed-bean-scope>session</managed-bean-scope>

<managed-bean>

Now let us say there is another bean named ValueSetter, which is also a managed bean, wants to access the ResultBean, then the technique is classic Java technique. It goes something like as under

  • Get a reference to FacesContext which essentially is ServletContex when compared to Servlet
  • From FacesContext reference get current Application object
  • From Application object get a reference to VariableResolver object
  • Call resolveVariable method on it passing reference of the FacesContext and the name of the bean as string (not the class name)
In code it will be as follows

FacesContext context = FacesContext.getCurrentInstance();

ResultBean bean = (ResultBean) context.getApplication().getVariableResolver().resolveVariable(context, "Result");

As you have seen how many objects need to be used. Cant it be made simpler?

Tuesday, October 2, 2007

JSP and Servlet - Implicit objects



JSP provides following implicit objects
  1. request
  2. response
  3. session
  4. page
  5. application
  6. exception
  7. pageContext
  8. out
The first question any interviewer or external asks is to name all the implicit object. The next question is which Servlet object corresponds to which JSP implicit object. So from this post onwards I will discuss this aspect. This post looks at request object.

You will be knowing that when JSP is compiled, it becomes a Servlet. HttpServletRequest is one of the two objects passes as arguments to the service method of the Servlet class. In case of compiled JSP, the name of the service method is _jspService, the only method that a developer cannot override. When the compilation takes place the request object is accessed as an object of HttpServletRequest. To cut a long story short request object corresponds to HttpServletRequest. In the next post I will discuss about response and out objects. Till then keep visiting...