开发者

Another Spring + Hibernate + JPA question

开发者 https://www.devze.com 2022-12-16 03:33 出处:网络
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

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 开发者_如何学Goimplements 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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消