Simple Login Application Using Spring MVC and Hibernate – Part 2

 I hope you have visited my part1 of my tutorial. Let’s see the steps for integrating hibernate framework into Spring MVC. Here I have used MySQL database. I have created one database called “springmvc” and created one table called “user” with userid, username, password fields.

 I have inserted some records into table like this.

Step 1: Creating User POJO class.

 We need to create a User POJO class for mapping user table. Ok let’s create it.

Step 2: Creating hibernate mapping xml file for user class.

 In hibernate we need to create hibernate mapping xml file for all domain class for mapping into corresponding database table. Instead of creating xml file you can use annotation for mapping domain class into database table. This is my mapping xml document created for mapping our user domain class into user database table.

Step 3: Creating authenticate service class.

 The method “verifyUserNameAndPassword” present in “AuthenticateService” class is used for  verifying the user name and password details against database table. It simply returns the boolean value.
  Very important one is, I am injecting object for “HibernateTemplate” via setter method. So It’s time for defining data source bean, hibernate properties bean, session factory bean , hibernate template bean and our service beans into applicationContext.xml file.

Step 4: Modifying applicationContext.xml.

 Here I have configured the beans that are related to hibernate as well as for our service class. Now we need to inject our service class into our controller and we need to call the “verifyUserNameAndPassword” method for verifying user credentials. So let’s see the steps for injecting our authenticate service class into login controller. 

Step 5: Modifying Login Controller.

 Here @Autowird annotation is used for injecting our service class into our controller.
 That’s all. We have completed our development. Let’s do testing. I have deployed this application into apache server. The result of the application is,


VPOWER said…

LoginApp-SpringMVC-Part2 on github is not available
Gnanasuriyan said…
Refer the below link,

Anonymous said…
thanks Bro!!!

when i am running u r application , after enter user name and password then hit submit button got this error.

please help me can solve this error.

HTTP Status 404 - /WebApplication2/Login.spring/processCredentials
Anaja Metellus said…
Hello, I'm getting an injection related error when running your code. please help
SaaD said…
Hi dude, I found your tutorial/example very useful and your tutorial made lots of clarification about the concept specially part 1. But I have one complaint that you have explained part 1 very very clearly and never left any doubt to ask, but in tutorial 2 even this tutorial is very us full but it has not explained well specially Template concept. It would be great if you also had few word of explanation for these term.

Any way very very thanks for the tutorial.

I will be very great full if you would refer any good book for me, with little description and more examples.
Amjadali Saiyed said…
Thanks for the brilliant little tutorial however there is a small comment to be made.
Everytime I use my username and password, it shows the invalid credentials. After hours of research I found out that HQL doesn't support the query alias and as soon as I removed the alias, my application started running fine.
Thanks again.
manoj kumar said…
Am New to spring and hibernate. I tried your tutorial but am getting the null pointer exception in hibernate.find(query) !!!!

Wat could b the possible reason???
Pls help me

Thanks in advance
shibu apps said…
Hi bro really thanks for your application.have one doubt,suppose i want to add another user with ID,USERNAME,PASSWORD and after that can i login by this user name and password means whether i can add many users i can and can i login with it..

Gnanasuriyan said…
Hi shibu,

This is simple and the application authenticates user when user name and corresponding password is matching with database records. So you will have to insert to many users in user table. said…
hi suriyan,

thanks for the tutorial. it is simple and easy-to-understand.

Anonymous said…
same as one of the comment
when i submit it will give me erroe message like
HTTP Status 404 - /LoginApp-SpringMVC-Part2/Login.spring/processCredentials
Unknown said…
change the index,html form as below is works well

form action="Login.spring" method="post"

thank u
Md Shahensha said…

at org.hibernate.hql.ast.QueryTranslatorImpl.list(
at org.hibernate.engine.query.HQLQueryPlan.performList(
at org.hibernate.impl.SessionImpl.list(
at org.hibernate.impl.QueryImpl.list(
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(
... 38 more

when ever i login its give me invalid credential
Manoj Kumar said…
HTTP Status 404 - /LoginApp-SpringMVC-Part2/Login.spring/processCredentials
RAMKI said…
Yes. Even am getting the same HTTP Status 404 error

HTTP Status 404 - /LoginApp-SpringMVC-Part2/Login.spring/processCredentials

type Status report

message /LoginApp-SpringMVC-Part2/Login.spring/processCredentials

description The requested resource (/LoginApp-SpringMVC-Part2/Login.spring/processCredentials) is not available.

Please help us in resolving the error.

Thanks in advance.
virat said…
please upload a code to build simple form including signup,signin and sign out in springs using database..
Dhanalakshmi M said…
not working.....http error