Amateur grass talking about the difference between the session and HttpSession in Sessionfactory

Source: Internet
Author: User
Tags sessions

First of all, tell a sad story ...

One day, a programmer who thought he had a Java ee in his mouth, went to Alibaba's Java interview for a long time without writing the code, and then, when asked a very simple question, the result didn't answer well. Then let's talk about the problem first ...

Q: What is the sessionfactory of hibernate? What is the session again?

Well, would you. Can you make it clear? If the answer is yes, then you don't have to look down .... If you still want to look down, then,

You just read it.

Before answering this question, let's look at some concepts.

One, hibernate is what.

Hibernate is an open source Object Relational mapping framework that has a very lightweight object encapsulation for JDBC, making it possible for Java programmers to manipulate the database with arbitrary object programming thinking. Hibernate can be applied to any use of JDBC, either in Java client programs or in servlet/jsp Web applications. ----Baidu Encyclopedia

In fact, my own understanding is that using hibernate, the most common scenario is to use its ORM mechanism, and then the traditional relational database operations, converted to object-oriented way to operate. Why do you do that? Before the ORM is used. A simple example, you fill in the form, filling a lot of data, name, sex, age ... Hobbies, and so on, and then you use JDBC to connect to the database, and then a piece of data from the request or a place to read, if there are 100, you need to define 100 variables to hold these form data, and then save to the database, at the time of saving, Also want to write the next SQL statement, pass 100 values again ... If it's 1000. Think about it tired, I am now a company, learning to use PHP, storage data, it is this way down, the heart is very tired.

So if you have ORM, you just map the table of the corresponding database to an object (such as student). Then, on the name of the input corresponding to the form, fill in the corresponding attribute of the corresponding object (such as Student.name), and then, at the time of saving, define a student object instance in the action submitted to, Then the data will be automatically saved in this object (I used Struts2, don't pull this, just to illustrate the efficiency). Next, you just need to save the object and OK. Is it convenient to feel a lot.

Second, what is Sessionfactory

The Sessionfactory interface is responsible for initializing hibernate. It acts as a proxy for the data storage source and is responsible for creating the Session object. The factory model is used here. Note that sessionfactory is not lightweight, because typically a project requires only one sessionfactory, and you can specify a sessionfactory for each database when you need to manipulate multiple databases. ----Baidu Encyclopedia

My understanding is that, in fact, as the name suggests, Sessionfactory, is used to create session sessions (specifically next) of the factory. Think of a particularly large factory that is designed to help you create one if you need to use the session. (Suddenly remember a cold joke, if you don't have an object to do.) The way is, new one comes out. Well you didn't laugh, I know). But it should be noted that, generally speaking, a Web project (System), if it involves a database (you do projects in school, generally a bar ...) hahaha), then this project will only need one sessionfactory is enough. All the sessions involved in this project are to be managed by this sessionfactory.

Next, create a sessionfactory that typically has two ways to create, 1. Read configuration information from the XML file to build the sessionfactory,2. Reading configuration information from a Java property file build Sessionfactory (this is not familiar, not how to use, Sorry).

My blog: Code encyclopedia: www.codedq.net; amateur grass: www.xttblog.com; love to share: www.ndislwf.com or ifxvn.com.

First type:

1 "Instantiate the configuration object, read the hibernate.cfg.xml under the SRC directory by default, and configure the file

[Java] view plain copy print? Configuration config = new Configuration (). Configure (); Or, in Configure (), indicate the file name and path [Java] view plain copy print? Configuration config = new Configuration (). Configure ("Hibernate.cfg.xml");

2 Now this config object, which already includes all parameters for the Hibernate runtime, can build a unique sessionfactory through the Buildsessionfactory () method of the configuration instance:

[Java] view plain copy print? Sessionfactory sessionfactory = Config.buildsessionfactory ();

3 after you get this Sessionfactory object, you can start another topic.

Third, what is the session

Mention to the session, the first thought is HTTP this dongdong session, think of HTTP this stateless protocol, no way to save any access to the object information, so there is a session of this dongdong, used to record some visitors information. But, take a good turn, twist, twist, twist say three times.

My blog: Code encyclopedia: www.codedq.net; amateur grass: www.xttblog.com; love to share: www.ndislwf.com or ifxvn.com.

The session in the hibernate is not the conversation that is called in HTTP, the HttpSession object is commonly referred to as the user sessions ...

and the session in the Hibernate. is used to represent an interaction (session) between an application and a database. In this session, the general Persistence Method (CRUD) is included, and don't say you don't know what CRUD is. Also, the session is a lightweight object (thread insecure) that typically binds each session instance to a database transaction, which means that each execution of a database transaction should first create a new session instance, and then close the session after using session. (Remember to close ha)

My blog: Code encyclopedia: www.codedq.net; amateur grass: www.xttblog.com; love to share: www.ndislwf.com or ifxvn.com.

Next, you create a Session object, based on the Sessionfactory object you obtained in the second step.

[Java] view plain copy print? Session session=sessionfactory.opensession (); Then after this session, you can use it for the specific operation of the database, to give a simple code. To say a word, generally in the use of session, will use the transaction, especially the need to modify the time of the data. Code on:

[Java]   View plain  copy  print? Public void save (student student )  {       transaction transaction= getsession (). BeginTransaction ()   //Open Transaction        getsession (). Save (student);//Just pass a Student object instance          

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.