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.

Tuesday, March 18, 2014

Now I understand...Death, Life, Celebration...

I never really understood that part of my culture where we throw a party at the demise of an aged person. I always thought that no matter how old someone was, death is death and should be something of sobriety not something that deserves fanfare.

I never got it. Until now.

My grandpa just passed away. And for the past couple of days, I have being trying to grapple with the reality of it all; he is really gone? It feels surreal…

Then I understood why celebration after death makes sense.

All of a sudden I feel this strong need to throw a party and proclaim to the whole world, what great man my grand father was. A man of character. A man of valour. A family man. A principled man who has guided not just me but a lot of people that were blessed to be under his tutelage. I have this strong desire to have a musician…not just any but a distinguished one come rain praises to his name. While everybody celebrates and acknowledge truly the greatness of his life.

The second reason why the tradition of throwing a funeral party now makes sense is how I see it bringing closure. I have been in a reflective mood ever since and deep down inside I feel a need for some sense of closure. A sense of having said the final farewell. And now I can just appreciate how the fanfare and merriment of a party can bring such a closure. The final farewell done with the utmost gusty celebration that we can muster.

I can appreciate how it all makes sense now and how therapeutic it can be. Some listen to music, some write, some cry, some spend time with cherished memorabilia...all we all yearn for is a way to deal with loss.

sigh...

Death is part of life I guess. And it adds yet another twist to this unexplainable miracle that our reality is.

Sunday, March 16, 2014

To Birth or not to Birth

From fairest creatures we desire increase,
That thereby beauty's rose might never die,
...and there goes the opening of Shakespeare's first sonnet. Where he waxed lyrical about the importance of procreation and its role in preserving and perpetuating beauty in nature. In this sonnet and the couple of sonnets that followed, Shakespeare was addressing a dear friend who was beautiful to the eyes, but whom to reasons best known to him, was unenthusiastic about finding a woman, settling down and start having children.

In the same sonnet, Shakespeare went further to chide him on this unexplainable reluctance to settle down, marry and procreate:
But thou, contracted to thine own bright eyes,
Feed'st thy light's flame with self-substantial fuel,
Making a famine where abundance lies,
Thyself thy foe, to thy sweet self too cruel...
You should read the complete sonnet here.

This poem is one of my favourite and one of the first few ones I got committed to memory; maybe because I sort of empathise with this unknown friend of shakespeare regarding his reluctance to procreate.

I got an email yesterday from one of my closest pal back in high school. Actually literally, he was the closest as we shared the same desk. And he told me that his wife just gave birth...

See, I have gotten to that age where questions from my parent and siblings about when I would marry and start having kids have stopped being subtle and are now outrightly blatant. I have also gotten to that age where not just my female friends from high school are getting married and having kids but also my male friends are birthing their first children...

So when I got this message you would assume that I would take some moment to reflect on this news and ask myself what's up with me? I did take a moment to have this reflection and the interesting thing is that I could not be bothered to feel perturbed. Neither did I have a sense of needing to have a child...or start a family...

...Because I don't know what this all means! Yes, this life! I don't know what it all means and why! I am yet to come to terms of it all, something I think I need to do before I can feel comfortable bequeathing life onto another...

Life feels more like a big mystery...a very haunting one and I am not sure of the advantage of bringing yet another life to come live through it.

It is a big conundrum we are all forced to live in (and through) which we do not really understand. Where do we all came from, why are we here? to what purpose? and where do we go after here? And the uncertainty of what lays after here, if anything at all, after death is decapitating.

Why do we have to live to die?

This is all really scary thought. A painful state of being. if feels existence is one big scary captivity. One we don't understand the reason behind, one in which no matter how we scream and yell, we can't get out of. We can't get answers, we can only hang on faith and believe or not and come to terms to the uncertainty and maybe hopelessness of it all.

And yes we lose ourselves to the hustle bustle of life and most of us try to push this gloom to the back our minds, but unfortunately I cant. I really cant help slip into this state of utter uncertainty now and then, and feel helpless over what this all means. I really find it incredulous that we all get so caught up in living this life and yet no one knows...

Is this what I would bring another life to come and experience? this unexplainable abject sense of hopelessness? This huge conundrum?

Life is a bitch and then you die?

Some hold the philosophy that if life is a bitch, then bang the hell out of her before you die. All good and dandy but I just cant get it out of my head that you still die? what happens next? Really?

Which is why I am in no way excited at the prospect of bringing another life in here yet. (at least not until I come to reconcile our fate) right now I have not. Which pushes me to contemplate the conclusion that maybe the best gift I can ever give my children is not to give birth to them?

Spare them all these haunting.

Between the time I started writing this post and the time I got it finished my Grand Daddy passed on to glory. A man who has had tremendous positive influence on my life and the life of people around him. A man of conviction, of strength of character, a family man. A source of confidence and stability. His passing somewhat changed the emotion I started off this post with...Maybe just for the family, maybe just for the sense of security it gives, the companionship as we go through this life...maybe just for that we have no choice but to keep bringing forth life as it makes passing through it a lot more bearable?...