java 休眠会话工厂
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4561450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate SessionFactory
提问by theJava
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
What is SessionFactory class? Why do we use it? What is hibernateTemplate Class used for?
什么是 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>
What does this do in bean
这在 bean 中有什么作用
回答by Jigar Joshi
Application obtains session instances from Session Factory.
SessionFactory
is mostly configured as Singleton
in application ,
If you are using Spring it will be configured in application context to be made singleton.
应用程序从会话工厂获取会话实例。
SessionFactory
主要Singleton
在 application 中配置,如果您使用 Spring,它将在 application context 中配置为单例。
SessionFactory
caches generate SQL statements and other
mapping metadata that Hibernate uses at runtime.
SessionFactory
缓存生成 Hibernate 在运行时使用的 SQL 语句和其他映射元数据。
Cached data that has been read in one unit of work and may be reused in a future unit of work.
已在一个工作单元中读取并可能在未来的工作单元中重用的缓存数据。
You can obtain object of session factory from Configuration class
您可以从 Configuration 类中获取会话工厂的对象
SessionFactory sessionFactory =
Configuration.buildSessionFactory();
Here in your conf. you have configured sessionFactory using AnnotationSessionFactoryBean class
在你的 conf. 您已经使用 AnnotationSessionFactoryBean 类配置了 sessionFactory
bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
and you have set some properties of session factory those are needed.
并且您已经设置了会话工厂的一些需要的属性。
HibernateTemplate
is a class provided by Spring :
HibernateTemplate
是 Spring 提供的一个类:
Helper class that simplifies Hibernate data access code. Automatically converts HibernateExceptions into DataAccessExceptions, following the org.springframework.dao exception hierarchy.
简化 Hibernate 数据访问代码的 Helper 类。按照 org.springframework.dao 异常层次结构自动将 HibernateExceptions 转换为 DataAccessExceptions。
回答by JegsVala
SessionFactoryis as Interface it delivery the of session object for whole application or entire hibernate application.
There will be generally one SessionFactoryand can be shared by all application thread. SessionFactoryis thread-safe.
SessionFactoryis second level cache of data that is reusable between transaction at a process or cluster level.
Continue.......
SessionFactory作为接口,它为整个应用程序或整个休眠应用程序提供会话对象。
一般会有一个SessionFactory,可以被所有应用线程共享。SessionFactory是线程安全的。
SessionFactory是数据的二级缓存,可在进程或集群级别的事务之间重用。
Continue.......
回答by Arun P Johny
SessionFactory contains all the hibernate mapping informations and it is responsible for creation and maintenance of the hibernate session in a transaction.
SessionFactory 包含所有的hibernate 映射信息,它负责在一个事务中创建和维护hibernate 会话。