Announcement Announcement Module
No announcement yet.
Problem with Spring and Hibernate Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with Spring and Hibernate

    Hi all I am newbie to spring and hibernate.

    I am using hibernate to get the data from Mysql database and use spring mvc to get the data and JSP to populate the data.

    my hibernate mapping definition is:

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    <class name="com.phonecard.domain.Product" table="product">
    <id name="productId" column="product_id">
    <generator class="assigned"/>

    <property name="categoryId">
    <column name="category_id"/>
    <property name="description">
    <column name="description"/>
    <property name="name">
    <column name="name"/>

  • #2
    Problem with Spring and Hibernate

    Dao implementation:

    package com.phonecard.dao;

    import java.sql.SQLException;
    import java.util.List;
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.springframework.dao.DataAccessException;
    import org.springframework.orm.hibernate3.HibernateCallba ck;
    import org.springframework.orm.hibernate3.HibernateTempla te;
    import org.springframework.transaction.annotation.Transac tional;

    import com.phonecard.domain.Product;

    public class ProductDaoImpl implements ProductDao {

    private HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate){
    this.hibernateTemplate = hibernateTemplate;

    public HibernateTemplate getHibernateTemplate(){
    return hibernateTemplate;

    public Product getProduct(final String productId) throws DataAccessException {
    HibernateCallback callback = new HibernateCallback() {
    public Object doInHibernate(Session session)
    throws HibernateException,SQLException {
    return session.load(Product.class, productId);
    return (Product)hibernateTemplate.execute(callback);

    @Transactional(readOnly = true)
    public List<Product> getProductListByCategory( final int categoryId)
    throws DataAccessException {
    // HibernateCallback callback = new HibernateCallback() {
    // public Object doInHibernate(Session session)
    // throws HibernateException,SQLException {
    // return session.createQuery("from Product product where product.categoryId = :categoryId").setInteger("categoryId", categoryId).list();
    // }
    // };
    // return (List<Product>)hibernateTemplate.execute(callback) ;

    return hibernateTemplate.getSessionFactory().getCurrentSe ssion().
    createQuery("from Product product where product.categoryId = :categoryId").setInteger("categoryId", categoryId).list();

    public List searchProductList(String keywords) throws DataAccessException {
    // TODO Auto-generated method stub
    return null;



    • #3
      Problem with Spring and Hibernate

      Spring view controller:

      package com.phonecard.web;

      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;

      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;

      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      import org.springframework.web.servlet.ModelAndView;
      import org.springframework.web.servlet.mvc.Controller;

      import com.phonecard.dao.ProductDao;
      import com.phonecard.domain.Product;

      public class ViewProductController implements Controller{

      protected final Log logger = LogFactory.getLog(getClass());
      private ProductDao productDao;

      public void setProductDao(ProductDao productDao) {
      this.productDao = productDao;

      public ModelAndView handleRequest(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      Map model = new HashMap();

      String categoryId = request.getParameter("categoryId");
      if(categoryId!=null){"Getting all product for category "+categoryId);
      List<Product> productList=this.productDao.getProductListByCatego ry(Integer.parseInt(categoryId));
      model.put("itemList", productList);


      }"Returning product view");
      return new ModelAndView("Product",model);



      • #4
        Problem with Spring and Hibernate and Product.jsp:

        package com.phonecard.domain;


        public class Product implements Serializable {

        /* Private Fields */

        private String productId;
        private int categoryId;
        private String name;
        private String description;

        /* JavaBeans Properties */

        public String getProductId() { return productId; }
        public void setProductId(String productId) { this.productId = productId.trim(); }

        public int getCategoryId() { return categoryId; }
        public void setCategoryId(int categoryId) { this.categoryId = categoryId; }

        public String getName() { return name; }
        public void setName(String name) { = name; }

        public String getDescription() { return description; }
        public void setDescription(String description) { this.description = description; }

        /* Public Methods*/

        public String toString() {
        return getName();



        <table align="center" bgcolor="#008800" border="0" cellspacing="2" cellpadding="3">
        <tr bgcolor="#CCCCCC"> <td><b>Item ID</b></td> <td><b>Product ID</b></td> <td><b>Description</b></td> <td><b>List Price</b></td> <td>&nbsp;</td> </tr>
        <c:forEach var="item" items="${itemList}">
        <tr bgcolor="#FFFF88">
        <a href="/shop/viewItem.pcrd"><c:out value="${item.productId}"/></a></b></td>
        <td><c:out value="${item.description}"/></td>
        <c:out value="${}"/>


        • #5
          Problem with Spring and Hibernate

          I am not sure where is the problem. The jsp doesnt dispally the data. It's all blank. But when I debug it to see the variables, I see it getting the data from the but in a different format. I am really confused whether I need to reformat the data structure or what. or Is the way of getting data is incorrect??

          Any help will really be appriciated.