hibernate還有人用嗎,Hibernate會話工廠

 2023-11-19 阅读 25 评论 0

摘要:Hibernate SessionFactory is the factory class through which we get sessions and perform database operations. Hibernate SessionFactory是工廠類,通過它我們可以獲取會話并執行數據庫操作。 Hibernate會話工廠 (Hibernate SessionFactory) hibernate還有人用嗎,

Hibernate SessionFactory is the factory class through which we get sessions and perform database operations.

Hibernate SessionFactory是工廠類,通過它我們可以獲取會話并執行數據庫操作。

Hibernate會話工廠 (Hibernate SessionFactory)

hibernate還有人用嗎,Hibernate SessionFactory provides three methods through which we can get Session object – getCurrentSession(), openSession() and openStatelessSession().

Hibernate SessionFactory提供了三種獲取Session對象的方法: getCurrentSession()openSession()openStatelessSession()

HibernateSessionFactory getCurrentSession (Hibernate SessionFactory getCurrentSession)

Hibernate SessionFactory getCurrentSession() method returns the session bound to the context. But for this to work, we need to configure it in hibernate configuration file like below.

Hibernate SessionFactory getCurrentSession()方法返回綁定到上下文的會話。 但是要使其正常工作,我們需要在Hibernate配置文件中對其進行配置,如下所示。

<property name="hibernate.current_session_context_class">thread</property>

If its not configured to thread, then we will get below exception.

如果未將其配置為線程,那么我們將獲得以下異常。

Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1012)at com.journaldev.hibernate.main.HibernateSessionExample.main(HibernateSessionExample.java:16)

Since this session object belongs to the hibernate context, we don’t need to close it. Once the session factory is closed, this session object gets closed.

由于此會話對象屬于Hibernate上下文,因此我們不需要關閉它。 會話工廠關閉后,該會話對象將關閉。

Hibernate Session objects are not thread safe, so we should not use it in multi-threaded environment. We can use it in single threaded environment because it’s relatively faster than opening a new session.

Hibernate Session對象不是線程安全的,因此我們不應在多線程環境中使用它。 我們可以在單線程環境中使用它,因為它比打開新會話相對更快。

HibernateSessionFactory openSession (Hibernate SessionFactory openSession)

Hibernate SessionFactory openSession() method always opens a new session. We should close this session object once we are done with all the database operations.

Hibernate SessionFactory openSession()方法總是打開一個新的會話。 一旦完成所有數據庫操作,就應該關閉該會話對象。

We should open a new session for each request in multi-threaded environment. For web application frameworks, we can choose to open a new session for each request or for each session based on the requirement.

我們應該在多線程環境中為每個請求打開一個新會話。 對于Web應用程序框架,我們可以根據需要選擇為每個請求或每個會話打開一個新會話。

HibernateSessionFactory openStatelessSession (Hibernate SessionFactory openStatelessSession)

Hibernate SessionFactory openStatelessSession() method returns instance of StatelessSession. There is another overloaded method where we can pass java.sql.Connection object to get a stateless session object from hibernate.

Hibernate SessionFactory openStatelessSession()方法返回StatelessSession實例。 還有另一個重載方法,我們可以傳遞java.sql.Connection對象從Hibernate中獲取無狀態會話對象。

StatelessSession in Hibernate does not implement first-level cache and it doesn’t interact with any second-level cache. Since it’s stateless, it doesn’t implement transactional write-behind or automatic dirty checking or do cascading operations to associated entities.

Hibernate中的StatelessSession不實現第一級緩存,并且不與任何第二級緩存交互。 由于它是無狀態的,因此不會實現事務后寫或自動臟檢查或對關聯實體進行級聯操作。

Collections are also ignored by a stateless session. Operations performed via a stateless session bypass Hibernate’s event model and interceptors. It’s more like a normal JDBC connection and doesn’t provide any benefits that come from using hibernate framework.

無狀態會話也會忽略集合。 通過無狀態會話執行的操作會繞過Hibernate的事件模型和攔截器。 它更像是一個普通的JDBC連接,并且沒有提供使用Hibernate框架帶來的任何好處。

However, stateless session can be a good fit in certain situations. For example where we are loading bulk data into database and we don’t want hibernate session to hold huge data in first-level cache memory.

但是,無狀態會話在某些情況下可能非常適合。 例如,當我們將大量數據加載到數據庫中時,我們不希望Hibernate會話將大量數據保存在一級緩存中。

A simple program showing Hibernate SessionFactory methods usage is given below.

下面給出一個顯示Hibernate SessionFactory方法用法的簡單程序。

package com.journaldev.hibernate.main;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.StatelessSession;import com.journaldev.hibernate.util.HibernateUtil;public class HibernateSessionExample {public static void main(String[] args) {SessionFactory sessionFactory = HibernateUtil.getSessionFactory();//Current Session - no need to closeSession currentSession = sessionFactory.getCurrentSession();//open new sessionSession newSession = sessionFactory.openSession();//perform db operations//close sessionnewSession.close();//open stateless sessionStatelessSession statelessSession = sessionFactory.openStatelessSession();//perform stateless db operations//close sessionstatelessSession.close();//close session factorysessionFactory.close();}}

That’s all for SessionFactory in Hibernate and it’s different methods to obtain session object.

這就是Hibernate中SessionFactory的全部內容,并且它是獲取會話對象的不同方法。

翻譯自: https://www.journaldev.com/3522/hibernate-sessionfactory

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/183106.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息