views:

170

answers:

2

I'm still struggling with changing my Spring Application to use Hibernate with JPA to do database activities. Well apparently from a previous post I need an persistence.xml file. However do I need to make changes to my current DAO class?

public class JdbcProductDao extends Dao implements ProductDao {
    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    public List<Product> getProductList() {
        logger.info("Getting products!");
        List<Product> products = getSimpleJdbcTemplate().query(
                "select id, description, price from products", 
                new ProductMapper());
        return products;
    }

    public void saveProduct(Product prod) {
        logger.info("Saving product: " + prod.getDescription());
        int count = getSimpleJdbcTemplate().update(
            "update products set description = :description, price = :price where id = :id",
            new MapSqlParameterSource().addValue("description", prod.getDescription())
                .addValue("price", prod.getPrice())
                .addValue("id", prod.getId()));
        logger.info("Rows affected: " + count);
    }

    private static class ProductMapper implements ParameterizedRowMapper<Product> {

        public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
            Product prod = new Product();
            prod.setId(rs.getInt("id"));
            prod.setDescription(rs.getString("description"));
            prod.setPrice(new Double(rs.getDouble("price")));
            return prod;
        }

    }

}

Also my Product.Java is below

public class Product implements Serializable {
    private int id;
    private String description;
    private Double price;

    public void setId(int i) {
        id = i;
    }

    public int getId() {
        return id;
    }

    public String getDescription() {
        return description;
    }

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

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("Description: " + description + ";");
        buffer.append("Price: " + price);
        return buffer.toString();
    }
}

I guess my question would be, How would my current classes change after using Hibernate + JPA with an Entity Manager

+1  A: 

Did you check the section 12.6. JPA of the Chapter 12. Object Relational Mapping (ORM) data access in the official documentation? Everything you need to know is discussed there.

If this is an option, you should extend the JpaDaoSupport base class, it provides convenient methods like get/setEntityManagerFactory and getJpaTemplate() to be used by sublasses. If not, then get a EntityManagerFactory injected and use it to create a JpaTemplate. See the section 12.6.2. JpaTemplate and JpaDaoSupport for more details on this.

Also have a look at Getting Started With JPA in Spring 2.0 for a more complete sample (the blog post is a bit old but is not really outdated) that will show you how to rewrite the methods of your DAO.

Pascal Thivent
thank you, I'm not sure if it's just me but I have a hard time finding good documentation on Spring/Hibernate/JPA
Albinoswordfish
A: 

Moreover, I suggest you to read this article: Generic DAO with Hibernate and Spring AOP, before deciding your design.

Adeel Ansari
Thank you, that article gave me a little bit more of an idea on this
Albinoswordfish
Glad to know that.
Adeel Ansari