【发布时间】: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