In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what is the sessionfactory of hibernate?". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Q: what is hibernate's sessionfactory for? What does session do?
Before answering this question, let's take a look at some concepts.
What is hibernate? Hibernate is an open source object-relational mapping framework, which encapsulates JDBC with very lightweight objects, so that Java programmers can use object programming thinking to manipulate the database at will. Hibernate can be used in any situation that uses JDBC, not only in the client program of Java, but also in the Web application of Servlet/JSP. In fact, my own understanding of Baidu Encyclopedia is that the most common scenario for using hibernate is to use its ORM mechanism, and then convert the traditional relational database operation into an object-oriented operation. Why would you do that? Before using ORM. As a simple example, you fill in a lot of data on the form, name, gender, age. Hobbies, etc., and then you use JDBC to connect to the database, and then read the data from request or somewhere one by one. If there are 100, it means that you need to define 100 variables to temporarily store the form data, and then when you save it to the database, you have to write sql statements and pass 100 values again. If it's 1000. I'm tired when I think about it. So if you have ORM, all you have to do is map the corresponding database table to an object (such as student). Then, on the name of the corresponding input of the form, fill in the corresponding properties of the corresponding object (such as student.name), and then define an object instance of student in the submitted Action when saving, and the data will be automatically saved in this object. Next, you just need to save the object and ok it. Doesn't it feel much more convenient?
Second, what is the sessionfactorySessionFactory interface responsible for initializing Hibernate. It acts as a proxy for the data storage source and is responsible for creating Session objects. Factory mode is used here. It is important to note that SessionFactory is not lightweight, because in general, a project usually requires only one SessionFactory, and when you need to operate on multiple databases, you can specify a SessionFactory for each database. -my understanding of Baidu encyclopedia is that, as its name implies, sessionfactory is the factory used to create session sessions (specifically next). Think of a very large factory that is designed to help you create one when you need to use session. Suddenly I remember a cold joke. What if you don't have a date? The solution is to come out alone with new. Well, you're not laughing, I know. But it should be noted that, generally speaking, a web project (system) involves a database (if you are working on a project at school, it is usually one. Ha, then only one sessionfactory is needed for this project. All the session involved in this project are managed by this sessionfactory.
Next, create a sessionfactory, which can be created in two ways, 1. Read the configuration information from the XML file to build SessionFactory,2. Read the configuration information from the Java properties file to build the SessionFactory.
The first kind:
1 "instantiate the Configuration object. By default, read the hibernate.cfg.xml in the src directory and the configuration file.
Configuration config = new Configuration () .configure ()
Or specify the file name and path in configure ();
Configuration config = new Configuration () .configure ("hibernate.cfg.xml")
2 "now that the config object has included all the parameters of the Hibernate runtime, you can build a unique SessionFactory through the buildSessionFactory () method of the Configuration instance:
SessionFactory sessionFactory = config.buildSessionFactory ()
The second kind
1 "create a Configuration object, and Hibernate loads the configuration file hibernate.properties in classpath by default, as follows.
Configuration config = new Configuration ()
2 "due to the lack of information about the corresponding configuration mapping file in the configuration file, it needs to be loaded by coding here, which can be achieved through the addClass () method of the Configuration object, as shown in the following code.
Config.addClass (BranchForm.class)
The addClass () method is used to load entity classes.
3 "after loading the configuration file and mapping file, you will get a Configuration instance that includes all Hibernate run-time parameters. A unique SessionFactory can be built through the buildSessionFactory () method of the Configuration instance, as shown in the following code.
SessionFactory sessionFactory = config.buildSessionFactory ()
Build the SessionFactory in a static code block because it only needs to be executed once when the class is loaded.
3. What is Session when it comes to Session, the first thing that comes to mind is the session of http. Thinking of http, a stateless protocol, there is no way to store any information about access objects, so session appears, which is used to record some information about visitors. However, take a good turn and say it three times. The session in hibernate is not the session in http, and the HttpSession object is generally called the user session. What about Session in hibernate? Is used to represent an interaction (session) between the application and the database. In this Session, the general persistence method (CRUD) is included, not to mention that you don't know what CRUD is. Moreover, Session is a lightweight object (thread-unsafe), and each Session instance is usually bound to a database transaction, that is, each database transaction is executed, a new Session instance should be created first, and Session needs to be closed after using Session. (remember to turn it off) next, create a Session object and follow the sessionfactory object you got in step 2.
Session session=sessionFactory.openSession ()
Then after you get the session, you can use it to perform specific operations on the database, giving a simple code. By the way, transactions are generally used when using session, especially when you need to make changes to the data. The above code:
Public void save (Student student) {Transaction transaction= getSession (). BeginTransaction (); / Open transaction getSession (). Save (student); / / just pass an object instance of Student transaction.commit (); / / transaction commit getSession (). Close (); / / close session} "what the sessionfactory of hibernate does", thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.