【问题标题】:org.springframework.orm.hibernate3.HibernateTemplate is pure hibernate or JPA implemenation?org.springframework.orm.hibernate3.HibernateTemplate 是纯休眠还是 JPA 实现?
【发布时间】:2014-04-27 19:28:54
【问题描述】:

如下面的代码 sn-p 所示,我在我的 DAO 中使用org.springframework.orm.hibernate3.HibernateTemplate 进行数据访问。这是否意味着我在这种情况下混合了hibernate和JPA?

到目前为止,我已经阅读到我们不应该在数据访问机制中混合使用 JPA 和 Hibernate。我们应该始终坚持纯休眠(org.hibernate.* API)或 JPA 的休眠实现(java.persistance.* API)。但是通过hibernate集成,我们不是使用纯Hibernate还是JPA?这是正确的还是我误解了这个概念。

基本上我想知道spring提供的hibernate集成风格是数据访问的最佳实践吗?

@Repository
@Transactional
public class DAOImpl implements DAO {

    private Log logger = LogFactory.getLog(this.getClass());

    protected org.springframework.orm.hibernate3.HibernateTemplate
            template = null;

    @Resource(name = "abcSessionFactory")
    protected SessionFactory sessionFactory;

    @Autowired
    public void init(SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        template = new org.springframework.orm.hibernate3.
                HibernateTemplate(sessionFactory);
    }
}

下面是我的 contex.xml 配置

<bean id="abcSessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="abcDataSource" />
        <property name="packagesToScan"
            value="xxxx" />

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.connection.driver_class">${hibernate.connection.driver.class}</prop>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
                <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory
                </prop>
                <prop key="hibernate.validator.apply_to_ddl">false</prop>
                <prop key="hibernate.validator.autoregister_listeners">false</prop>
            </props>
        </property>
    </bean>

【问题讨论】:

    标签: java spring hibernate spring-orm


    【解决方案1】:

    这是否意味着在这种情况下我正在混合使用hibernate和JPA?

    不,org.springframework.orm.hibernate3.HibernateTemplate 是与休眠Session 一起使用的助手。 它使用标准的org.hibernate.Session 方法,例如get()。这里是source of HibernateTemplate.get()

    顺便说一句,在DAOImpl 中,您同时使用了 SessionFactory 和 HibernateTemplate。真的有必要吗?请注意HibernateTemplate 默认会创建新的Session,参见javadoc

    如果您选择仅使用HibernateTemplate,代码可能如下所示:

    @Repository
    @Transactional
    public class DAOImpl implements DAO {
    
       @Autowired
       protected org.springframework.orm.hibernate3.HibernateTemplate template;
    
    }
    

    配置:

    <bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
        <!-- use hibernate SessionFactory.getCurrentSession() for getting session -->
        <property name="allowCreate" value="false"/>
    </bean>
    

    【讨论】:

    • 谢谢,用配置细节更新了我的帖子。他们错了吗?
    • 似乎一切正常。最好用你的模型检查一下。
    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 2011-06-29
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多