【问题标题】:Another Spring + Hibernate + JPA question另一个 Spring + Hibernate + JPA 问题
【发布时间】:2010-01-14 05:41:51
【问题描述】:

我仍在努力更改我的 Spring 应用程序以使用 Hibernate 和 JPA 来执行数据库活动。显然,从以前的帖子中我需要一个 persistence.xml 文件。但是,我需要对我当前的 DAO 类进行更改吗?

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;
        }

    }

}

下面还有我的 Product.Java

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();
    }
}

我想我的问题是, 将 Hibernate + JPA 与实体管理器一起使用后,我当前的课程将如何变化

【问题讨论】:

    标签: java hibernate spring orm jpa


    【解决方案1】:

    你查看官方文档中Chapter 12. Object Relational Mapping (ORM) data access12.6. JPA 部分了吗?您需要知道的一切都在此处讨论。

    如果这是一个选项,您应该扩展JpaDaoSupport 基类,它提供了方便的方法,如get/setEntityManagerFactorygetJpaTemplate() 供子类使用。如果没有,则注入一个EntityManagerFactory 并使用它来创建一个JpaTemplate。有关这方面的更多详细信息,请参阅12.6.2. JpaTemplate and JpaDaoSupport 部分。

    还可以查看Getting Started With JPA in Spring 2.0 以获得更完整的示例(博客文章有点旧,但并没有真正过时),它将向您展示如何重写 DAO 的方法。

    【讨论】:

    • 谢谢,我不确定是不是只有我一个人,但我很难找到关于 Spring/Hibernate/JPA 的好的文档
    • JpaTemplate 和 JpaDaoSupport 都已被弃用;他们也很长时间没有被春季团队推荐过。直接针对 Java Persistence API 进行编码应该是可行的方法。
    【解决方案2】:

    此外,我建议您在决定您的设计之前阅读this article: Generic DAO with Hibernate and Spring AOP

    【讨论】:

    • 谢谢,那篇文章让我对此有了更多的了解
    猜你喜欢
    • 2015-11-24
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    相关资源
    最近更新 更多