Monday, March 31, 2014

Configuring Hibernate in Spring Framework.

tl;dr to configure Hibernate in Spring, you need to configure a bean that implements org.hibernate.SessionFactory. Configuring this bean involves specifying the data source, the Object-to-Database mapping (if you using xml for the mapping) and the hibernate properties.

Last post in the Configuring Spring series looked into how to configure data sources, a basic requirement, regardless of the actual method in which your application would communicate with the database server: either via plain JDBC or via an ORM tool.

This post would look into how to configure Spring to use Hibernate. Hibernate is an object-relational mapping library for the Java language, that provides a framework for mapping an object-oriented domain model to a traditional relational database.

In how to configure data sources in Spring, we saw how the data source is the entry point to having your application communicate with a database server; how it is configured and retrieved for use in your application by classes that are specifically created to handle interaction with the database (DAO classes)

In configuring hibernate to work with Spring Framework, the data source still remains the primary entry point, but it would not be what your application code (or DAO class if you go with this pattern) would have to use directly in other to communicate with the database. With Hibernate, your application code requires something called the SessionFactory

So the process of configuring Spring to work with Hibernate is basically then all about configuration the bean that would make the SessionFactory available for you.


Sunday, March 30, 2014

Configuring Data Sources in Spring Framework

This is the second post in the configuring Spring series. It kickstarts the database centric configurations required when building an application that persist data with Spring framework. This post talks about configuring data sources.

There are various ways to enable the ability to persist data in your application. From using any of the available Implementation of Java Database Connectivity (commonly referred to as JDBC) to going the ORM route via any of the popular ORM frameworks (Hibernate, MyBatis, Eclipse Link etc )

But with any of the above mentioned method, a data source is always a requirement; because...well you need to have a source to the data if you want to persist and/or retrieve data. And in Spring the data source ends up as a Bean that you would configure.

Spring provides several options of configuring the data source. You can have:
  1. Data sources that are provided via JNDI.
  2. Data sources that work with JDBC drive.
  3. Data sources that are from a connection pool.
But before we go ahead to look at configuration proper, it would be useful to quickly define some basic concepts you would encounter when dealing with persistency:



Sunday, March 23, 2014

The Difference between ServletContext and ServletConfig

The servletContext is used to provide configuration that would be available to all servlets in a web application, while servletConfig is used to provide configuration per servlet level.


Specifying and retrieving servletContext configurations.

To specify servletContext configuration, you do so in your web.xml via the <context-param/> tag. For example:

<context-param>
<param-name>timeout</param-name>
<param-value>60</param-value>
</context-param>

and to retrieve these parameters in any of the defined servlets you have:

String value = getServletContext().getInitParameter("timeout");

Specifying and retrieving servletConfig configurations.

Since servletConfig are available per servlet, the definition is within the servlet definition in the web.xml. This is done using the <init-param/> tag.

<servlet>
    <servlet-name>Polling Servlet</servlet-name>
    <servlet-class>com.foo.pollServlet</servlet-class>
    <init-param>  
        <param-name>timeout</param-name> 
        <param-value>60</param-value> 
    </init-param> 
</servlet>

With this, the specified parameters are only available in the com.foo.pollServlet servlet and can be retrieved by:

String value = getServletConfig().getInitParameter("timeout");

Left to me, I would say the not so intuitive choice of words, used to describe these two configuration type, is the cause of confusion most people usually have. I would rather have had the configurations per servlet level called servletConext while configurations that are on the web app level (that are available to all servlet) called appContext or webappContext.

Configuring Beans in Spring Framework.

This is the first post in the Configuring Spring Series.

Beans in Spring framework are proxies implementation of your classes. And following the Inversion of Control way of doing things, they are managed by the Spring framework container and made available for you to use.

Meaning you basically write your classes like you would normally do when programming in Java. After which, you then need to go through a process of configuration that would guide Spring regarding how your classes would be transformed into managed beans which you can then use to form your business logic.

There are different ways to have this configuration set up. And this post, first in the series of many, that would cover some of the configuration tasks in Spring, covers how to configure Spring to take your classes and have it turned into beans. This process is normally referred to as Bean wiring.

We would look at
  1. Configuration via XML
  2. Configuration via Java Classes: JavaConfig
  3. Configuration via @Component and @Autowired Annotation


Configuring Spring Series

Configuring stuff can be overwhelming when you first start off with Spring framework. You spend an awful amount of time wiring things up and configuring before you actually get to start writing business code to solve the problem you are faced with...

Want to start using SpringMVC? there is a configuration for setting this up. Spring Security? There is a configuration for that. Want to use AOP, then you need to know how to have this configured. Want to access a database? You have to first wire up the datasource, the adapter etc etc

This can feel just too much.

It would fast became obvious to anyone using Spring that in other to get productive, configuration is something that is needed to be sorted out and have nicely filed away in a easily retrievable mental model so as to avoid losing precious time fighting the configurations or falling into configuration mishaps that could be a drag on productivity.

The posts that would follow under a series I called configuring spring is my attempt to do just that. It would be series of posts that covers some of the basic configuration hoop I have needed to jump over before I could get to actual development. And since I have a terrible, horrible memory, these posts would be a place I can also easily run to, to look up how a particular configuration needs to be wired.

This post would serve as an index and would be updated as I add to the series.

Posts so far:

1. Configuring Beans in Spring Framework
2. Configuring Data Sources in Spring for Database Access
3. Configuring Hibernate in Spring
4. Configuring Hibernate JPA in Spring Framework
5. Configuring Spring MVC
6. Configuring Spring Data JPA
7. Configuring Aspects with Spring AOP

Friday, March 21, 2014

Getting Personal

In the beginning, there was this blog. And it was largely a place where I ranted. Not just about technology but other stuff that life brings my way, which not surprisingly, was mostly about technology…it also doubled up as some sort of journal.

But somehow somewhere down the line, it stopped being an avenue for expression: a place to capture my thoughts. Somehow somewhere, my writings morphed into mostly technical, tutorial like posts.

For instance, looking at all the post in 2013, they were all technical related, except the April 30, 2013 post which was supposed to be an attempt to add some personal touch by doing monthly recaps. That did not go very far.

Why was this? Pondering on it for a while, it hit me. It was because of the inroad of social networking sites into my everyday life: because I was spreading myself too thin all over Facebook and Twitter, sharing everything that was on my mind as status updates, sharing everything on my mind as comments to other peoples posts. Liking pictures, re-sharing links etc...

Twitter also, was the perfect place to let out burst of thoughts and emotions that should or could have coagulated into well formed posts...

But I am fed up. Fed up with all these social networking sites. Especially Facebook. I have long realised that it was having detrimental effects on my productivity and emotional well being…I am also fed up with the forced terseness of 140 characters that Twitter imposes. If I want to express myself, I want the liberty of being able to do this with as many words I deem fit.

And as such…I am going, going, going...back...back, to blogging blogging (in Biggie smalls voice)…not that I actually stopped blogging, but I am going back to having my blog as my primary avenue for capturing my thoughts.

I am avoiding Facebook by all means now. I still keep using twitter though, but majorly as a source of discovery for things around my area of professional interests, and once in a while, a place to engage in ephemerial and disjointed rants. If I have something on my mind I really feel the need to share or capture, I write a coherent post instead. No, sorry: fusilade of tweets is no longer for me.

It is time to get back to blogging and get personal here again.

Thursday, March 20, 2014

When CSS position: sticky doesn’t work.

I spent a huge chunk of time at work yesterday trying to figure out why the hell webkits implementation of position: sticky, position: -webkit-sticky wasn’t working.

Applying it on the tablet site I was working on had no effect but when I viewed a demo site: http://html5-demos.appspot.com/static/css/sticky.html it works perfectly; so it was not a case of my browser not supporting the feature, It was definitely something in the markup or css that was screwing things up.

After some couple of hours hunting about, I found the culprit:

#bodyconstraint {
overflow: hidden !important;
}

A parent element has got overflow:hidden, and this, apparently throws position: -webkit-sticky off.

So in case you run into a situation where position: -webkit-sticky isn’t working, an heads-up: check to see if the element you want to position as sticky is not a child of a parent element that has had its overflow set to hidden.

Also, you need to set the top property of the element you want to position sticky. if not, it doesn't work too.