如下面的代码片段所示,我org.springframework.orm.hibernate3.HibernateTemplate
在我的 DAO 中使用数据访问。这是否意味着我在这种情况下混合了休眠和 JPA?
到目前为止,我已经读到我们不应该在数据访问机制中同时使用 JPA 和 Hibernate。我们应该始终坚持纯休眠( API)或JPA( * API)org.hibernate.*
的休眠实现。java.persistance.
但是通过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>