3. NHibernate Basics-something you have to know

Source: Internet
Author: User

First introduce the framework structure (this has a concept can be);

Then we will introduce a very important concept (be sure to look good)!!

This section provides an introduction to the NHibernate architecture, beginning with a look at the location of the framework in the application:

Let's start with a simple diagram:

As you can see, NHibernate is in the middle of applications and databases, does it work? As an example:

NHibernate is like the librarian of the library, whether you borrow a book or return a book, just to tell the administrator, the administrator to the rest of the matter for you to solve.

Let's take a more detailed picture:

This diagram simply said, can see, NHibernate has two "components", session and Sessionfactory, we will introduce in detail, here is a brief introduction: Each session corresponds to the operation of the database, that is, When you need to manipulate the database, you need to get a session object, the session encapsulates the context of the operation database, and the session is not thread-safe; Sessionfactory is the factory that generated the session, Each sessionfactory corresponds to a database, so it is not recommended that you instantiate a Sessionfactory object, which is thread-safe (we recommend implementing it in a single case for each database-like our demo).

Finally come a complex diagram:

Do you remember the Hibernate.hbm.xml defined in the demo?

By configuring the configuration file, you can manipulate different types of databases.

Next, we introduce an important concept!!!!!

An object has three states in the NHibernate framework: transient, persistent, and detached, respectively:

---Transient: transient state, short and temporary.

[Official explanation]

The instance are not, and have never been associated with any persistence context. It has no persistent identity (primary key value).

[Personal Understanding]

New class, this class has never been persisted, that is, after creating a class, the object that has not yet been associated with the nhibernate, simply understood, is that you are new to the class. Of course you can turn this class into a persistent class by ISession.

---persistent: persistent state, Permanent

[Official explanation]

The instance is currently associated with a persistence context. It has an persistent identity (primary key value) and, perhaps, a corresponding row in the database. For a particular persistence context, NHibernate guarantees This persistent identity is equivalent to CLR IDE Ntity (in-memory location of the object).

[Personal Understanding]

If you understand the transient state, it is much simpler, the persistent class is that the class in the database has corresponding records, perhaps just through the session saved to the database or just read out, in short, there is no relationship to the ISession object, if you modify the object at this time, That would be directly reflected in the database, similar to bidirectional operations in Silverlight.

---Detached: detached state, broken apart

[Official explanation] The instance is once associated with a persistence context, but that context is closed, or the instance was Serializ Ed to another process. It has an persistent identity and, perhaps, a corrsponding row in the database. Fordetached instances, NHibernate makes no guarantees about the relationship between persistent identity and CLR Identity.

[Personal Understanding]

When the isession of a persisted class is closed, the object becomes detached from the persistent state, which is quite understandable.

These three states are actually very important, only you understand the three states, in the subsequent study will feel very relaxed.

3. NHibernate Basics-something you have to know

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.