[英]Hibernate SessionFactory
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
什么是SessionFactory類? 我們為什么用它? 什么是hibernateTemplate類用於?
<bean id="myUserDAO" class="com.mysticcoders.mysticpaste.services.ContactSerImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>com.mysticcoders.mysticpaste.model.Contact</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
這在bean中做了什么
應用程序從Session Factory獲取會話實例。 SessionFactory
主要在應用程序中配置為Singleton
,如果您使用的是Spring,它將在應用程序上下文中配置為singleton。
SessionFactory
緩存生成Hibernate在運行時使用的SQL語句和其他映射元數據。
已在一個工作單元中讀取的緩存數據,可在未來的工作單元中重復使用。
您可以從Configuration類獲取會話工廠的對象
SessionFactory sessionFactory =
Configuration.buildSessionFactory();
在你的conf。 您已使用AnnotationSessionFactoryBean類配置了sessionFactory
bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
並且您已經設置了所需的會話工廠的一些屬性。
HibernateTemplate
是Spring提供的一個類:
Helper類簡化了Hibernate數據訪問代碼。 在org.springframework.dao異常層次結構之后自動將HibernateExceptions轉換為DataAccessExceptions。
SessionFactory作為Interface為整個應用程序或整個hibernate應用程序提供會話對象。
通常會有一個SessionFactory ,可以由所有應用程序線程共享。 SessionFactory是線程安全的。
SessionFactory是數據的二級緩存,可在進程或集群級別的事務之間重用。
Continue.......
SessionFactory包含所有hibernate映射信息,它負責在事務中創建和維護hibernate會話。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.