Adding my Spring MVC hibernate app to OpenShift

After developing my local app using Spring MVC and hibernate I decided to put it on openshift.
The technology stack of my app is:

1. Spring MVC v3.2.2.RELEASE
2. Hibernate v3.5.4-Final
3. MySQL
4. Tomcat
5. Maven
6. JQuery
7. Twitter Bootstrap for Responsive Web UI.
OS :Ubuntu 12.04

The source code can be checkout from here :
Read More »


Hibernate learnings

Few of the important key points to remember in Hibernate:

1.) Use Transactional boundaries in your service layer.

2.) Outside a transaction boundary if session gets closed, the proxy object gets detached and hence if there is a collection which is lazily loaded, it can not be fetched. Either make separate call for the collection or use OpenSessionViewFilter.
Read More »

Hibernate basics

NOTE : This is beginner level blog.

Sometimes We just forget the basic things if we get out of touch for some time. Hence I am writing down the basics to remind myself.

1.) One to Many mapping:
example : One User has many tasks
Screen Shot 2014-01-29 at 10.39.19 AM

@Table(name = "user")
public class User {

        private Long id;
        private List<Task> tasks

  @OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL)
        public List<Task> getTasks() {
                return tasks;

        public void setTasks(List<Task> tasks) {
                this.tasks = tasks;

Here, We need to tell hibernate which is the attribute in the referencing Java Class ( In this case, Task)
which can be used to map this class with referencing class. So, in class Task, “user” is the name of attribute which is representing relationship with User Table.
We do not have any field in User table, this mapping is for hibernate, so we do not have to write a query and apply join to find all the tasks of a user.
We can access all the tasks of a user by:


2.) Many to One Mappings:
Similarly, for Task ( Multiple tasks for one user)

@Table(name = "task")
public class Task {
        private Long id;
        private String name;
        private User createdBy;

  @ManyToOne(fetch = FetchType.LAZY)
        public User getCreatedBy() {
                return createdBy;

Here, We need to join the column “createdBy” in table Task with primary key (id) of User table.
By default, it picks up the primary key of the referenced table. Also, note that I have put fetch type as lazy.

3.) One to One Mappings:
These mappings are generally useful when using composition, for example, a Task can have a Type, called as TaskType. Every task has one type, so in order to enable hibernate to fetch task type details from task without writing any query, we need to define mapping.
So, for Task Table :

@Table(name = "task")
public class Task {

    private Long id;
    private String name;
    private TaskType taskType;
    public TaskType getTaskType() {
        return taskType;

There is no need to do define any mapping in TaskType class.

These are very basic, but helpful for a quick reference.