Spring Hibernate Annotations

In this section, you will learn about the common annotations used for Spring Hibernate Integration.

Spring Hibernate Annotations

In this section, you will learn about the common annotations used for Spring Hibernate Integration.

Hibernate Annotation

Annotation used for Hibernate entity class mapping is given below :

package net.roseindia.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "article")
public class Article {

@Column(name = "article_id")
private Long articleId;

@Column(name = "article_name", nullable = false, length=20)
private String articleName;

@Column(name = "article_desc", nullable = false)
private String articleDesc;

@Column(name = "date_added")
private Date addedDate;

public Article() { 

public Long getArticleId() {
return articleId;

public void setArticleId(Long articleId) {
this.articleId = articleId;

public String getArticleName() {
return articleName;

public void setArticleName(String articleName) {
this.articleName = articleName;

public String getArticleDesc() {
return articleDesc;

public void setArticleDesc(String articleDesc) {
this.articleDesc = articleDesc;

public Date getAddedDate() {
return addedDate;

public void setAddedDate(Date addedDate) {
this.addedDate = addedDate;

Spring Annotation

The annotation used in the controller class is given below :

package net.roseindia.controller;

import java.util.HashMap;
import java.util.Map;

import net.roseindia.model.Article;
import net.roseindia.service.ArticleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

public class ArticleController {

private ArticleService articleService;

@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveArticle(@ModelAttribute(" article") Article article,
BindingResult result) {
articleService.addArticle( article);
return new ModelAndView("redirect:/articles.html");

@RequestMapping(method = RequestMethod.GET)
public ModelAndView listArticles() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("articles", articleService.listArticles());

return new ModelAndView("articlesList", model);

@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addArticle(@ModelAttribute("article") Article article,
BindingResult result) {
return new ModelAndView("addArticle");


In the above example, ArticleDao is the Dao layer Interface and its implementation class is given below:

package net.roseindia.dao;

import java.util.Date;
import java.util.List;

import net.roseindia.model.Article;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

public class ArticleDaoImpl implements ArticleDao {

private SessionFactory sessionFactory;

// To Save the article detail
public void saveArticle(Article article) {
article.setAddedDate(new Date());

// To get list of all articles
public List<Article> listArticles() { 
return (List<Article>) sessionFactory.getCurrentSession().createCriteria(Article.class).list();

In the above example, ArticleService is the service layer interface and its implementation class is given below :

package net.roseindia.service;

import java.util.List;

import net.roseindia.dao.ArticleDao;
import net.roseindia.model.Article;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ArticleServiceImpl implements ArticleService {

private ArticleDao articleDao;

public ArticleServiceImpl() {

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addArticle(Article article) {

public List<Article> listArticles() {
return articleDao.listArticles();


For complete example, click here.


Share on Google+Share on Google+

Spring Hibernate Annotations

Posted on: April 26, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Spring Hibernate Annotations  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Rupali Thorat
March 4, 2013

what is the use of Annotation in program