Before you start, it is necessary to talk aboutSession)AndTransaction).
What is session and transaction?
Session and transaction are the two most important objects provided by nhib.pdf. The Session object can communicate with the database and perform various operations. The transaction object provides a tool for us to manage multiple operations in one unit.
Session
The nhib1_session can be viewed as an abstract pipeline to the database. Now, you must create an adoconnection, open the connection, and pass the connection to the command object. The days for creating a datareader from the command object will never go. With nhib.pdf, you only need to request a session object from sessionfactory. Through the session object, you can add data to the database, update and delete existing data in the database, or read existing data in the database. All these operations can be executed in an object-oriented manner, without the need to handle the complexity of SQL strings and specific databases. The Session object allows us to communicate with the database instead of the SQL Server database, MySQL database, or Oracle database. Nhib.pdf completely abstracts these details for us.
Transaction
Transaction allows us to execute a lot of tasks in one unit of work. The result is that either all operations are successful, or even if a task fails to be executed, all tasks are returned to the original state. This kind of work method is also called "atomic operation ".
Transaction has four features: atomicity, consistency, isolation, and durability. We also use acid to describe these features. Transaction operations must meet acid requirements.
Use the session factory to create a session
Nhib.pdf uses a factory object to create a session instance. This factory object is calledSession Factory)". A session factory can create any number of session objects as needed. Creating session objects is very cheap.
On the other hand, the creation of the session factory is very expensive. Based on the complexity of the system, it can take quite a long time to create a session factory instance. This is whyProgramThe reason why only one session factory instance is created throughout the lifecycle.
A session factory is database-specific. If our program only needs to communicate with a separate database, we only need a session factory. On the other hand, if our program requires several different databases, each database requires a session factory.
Session factory isThread-safe). Running on different threadsCodeYou can use the same session factory to create a session object. In contrast, session objects can only be used in a single thread. In other words, the session object is not thread-safe.
It is very easy to create a Nhibernate session. Once the sessionfactory object is available, you can call the opensession method to create it:
VaR session = sessionfactory. opensession ();
Generally, regardless of the operation result, we want to open the session in the using statement to ensure that the session is closed and released. We also want to start a transaction to better predict the operation results.
Using (VAR session = sessionfactory. opensession () {using (VAR transaction = session. begintransaction () {// create, update, delete, or read data transaction. commit ();}}
Add data
Create a new object that can callSave MethodPersistent to database:
VaR newproductid = (INT) Session. Save (newproduct );
Note that the Save method returns the ID of the newly generated record. Because different policies generate IDS (INT, long, or GUID), the returned type is of the object type. We must convert the result to the expected type. We can also access the ID attribute of the object just persisted to obtain the value of the new ID to replace the return value of the Save method and convert it to the correct type.
VaR newproductid = newproduct. ID;
As an alternative to the Save method, we can also usePersist Method. The persist method does not return values. You can use the Object ID attribute to obtain the value of the newly generated Object ID. In this case, there is no type conversion.
Query data
You can use the get or load method of the session object to read a single record from the database based on the specified primary key value. The following code reads the product entity with ID 1 from the database:
VaR Product = session. Get <product> (1 );
To use get or load, we must know the Object ID.
If you want to obtain the list of objects of a given type, you can useQuery Method. This method is part of the LINQ to nhib.pdf driver.
To obtain the list of all categories, you can use the following code:
VaR allcategories = session. query <Category> (). tolist ();
Note that the tolist () method is called after the statement. A list of the IQUERY <Category> type is returned by the LINQ to nhib.pdf method. It is a delayed loading function. to load all records in advance by calling tolist (), you must force the execution.
Get and Load
If the load is delayed, get and load are very different.
To load the product object, use the following code:
VaR Product = session. Get <product> (...?);
The get method uses the primary key value of the object to be loaded as the parameter. Therefore, you can use the following code to load the product with ID 1:
VaR Product = session. Get <product> (1 );
If the requested object exists in the database, the get method will physically retrieve it from the database. If the entity with the given ID does not exist in the Database, null is returned.
If the load method is used, Nhibernate does not retrieve objects from the database, but creates a proxy object. The unique property of this proxy object to fill in data is ID, as shown in the following code:
VaR Product = session. Load <product> (1 );
Through the above Code, a proxy product entity with ID 1 is obtained. In this case, our code accesses Any attribute other than ID, and nhib.pdf loads entities from the database. In fact, this is what we call "delayed loading ".
So under what circumstances will the load method be used? When we need to access and operate entities, we use the get method of the session object, so we do not need to actually modify and access the details of the object using the load method. Let's look at a simple example. Suppose we want to update a product entity to change its category. A product belongs to a category. Therefore, the product entity has a category attribute. However, when operating products, we do not want to modify the categories at the same time, but only use them. The following code can be implemented:
VaR Product = session. Get <product> (productid); product. Category = session. Load <Category> (newcategoryid );
Nhibory generates a SELECT statement to load a product, but does not load category. It only creates a proxy entity and assigns a new category to the product.
Update Data
Because the Nhibernate Session Object tracks any changes to the loaded object, you do not need to call update or other methods to persistently modify the object to the database. At this time, the session object is refreshed and the modification will be automatically persisted by nhib.pdf. The following code can be used:
Using(VaRSession = sessionfactory. opensession ())Using(VaRTx = session. begintransaction ()){VaRProduct = session. Get <Product> (1); product. unitprice = 10.55 m;// New unit priceTX. Commit ();}
Delete data
To delete an object in a database, you must first load it and then pass it toDelete Method, As shown in the following code:
VaRProducttodelete = session. Load <Product> (Productid); Session. Delete (producttodelete );
Note that you can use the load method instead of the get method to avoid unnecessary database round-trips. When the session is refreshed, the above Code result is a simple SQL statement to be deleted and sent to the database. However, this applies to the set where the object to be deleted depends on other entities or child entities. In this case, the entity must be loaded into the memory.
Actual Time
After all theories, let's implement an example. In this example, we define a simple model and use automatic ing to map the model. Then we create a session factory and use it to create a session object.
1. Create an empty database in SMSs: nhibernatesessionsample.
2. Open Vs and create a console application project: nhibernatesessionsample.
3. add reference to the Nhibernate, Nhibernate. bytecode. Castle and fluentnhibernate programs for the project.
4. Add a domain folder to the project and add an order class to the folder.
Public classOrder{Public Virtual intId {Get;Set;}Public VirtualCustomer customer {Get;Set;}Public VirtualDatetimeOrderdate {Get;Set;}Public VirtualIlist<Lineitem> lineitems {Get;Set;}}
5. Add a default constructor to the order class to initialize the lineitems set, as shown in the following code:
PublicOrder () {lineitems =NewList<Lineitem> ();}
6. Add a virtual method to the order class to add line items, as shown in the following code:
Public Virtual voidAddlineitem (IntQuantity,StringProductcode ){VaRItem =NewLineitem {order =This, Quantity = quantity, productcode = productcode}; lineitems. Add (item );}
7. Add the lineitem class and customer class in the domain folder, as shown below:
Public classLineitem{Public Virtual intId {Get;Set;}Public VirtualOrderOrder {Get;Set;}Public Virtual intQuantity {Get;Set;}Public Virtual stringProductcode {Get;Set;}}
Public classCustomer{Public Virtual intId {Get;Set;}Public Virtual stringCustomername {Get;Set;}}
8. Add a class file orderingsystemconfiguration in the project. Add the following code to define which classes to map:
NamespaceNhibernatesessionsample {Public classOrderingsystemconfiguration:Defaultautomappingconfiguration{Public override boolShouldmap (TypeType ){ReturnType. namespace =Typeof(Customer). Namespace ;}}}
9. Define a static variable for the session factory in the program class, as shown below:
Private StaticIsessionfactorySessionfactory;
10. Add a static method configuresystem to the program class. In this method, configure nhib.pdf to use SQL Server as the database, and map the model and automatically create the database architecture using automatic ing:
Private Static void Configuresystem (){ Const string Connstring = "Server =.; database = nhibernatesessionsample ;" + "Integrated Security = sspi ;" ; VaR CFG = New Orderingsystemconfiguration (); VaR Model = AutoMap . Assemblyof < Customer > (CFG ); VaR Configuration = Fluently . Configure (). Database ( Mssqlconfiguration . Mssql2008. connectionstring (connstring). showsql). mappings (M => M. automappings. Add (model). buildconfiguration (); VaR Exporter = New Schemaexport (Configuration); exporter. Execute ( True , True , False ); Sessionfactory = configuration. buildsessionfactory ();}
Note the call of. showsql in the above Code. It configures nhib.pdf to output all SQL statements sent to the database to the console.
11. Add a static method createcustomers in the program class. This method creates two customer objects and stores them in the database using session objects, as shown in the following code:
Private Static voidCreatecustomers (){VaRCustomera =NewCustomer{Customername ="Microsoft"};VaRCustomerb =NewCustomer{Customername ="Apple Computer"};Using(VaRSession = sessionfactory. opensession ())Using(VaRTransaction = session. begintransaction () {session. Save (customera); Session. Save (customerb); transaction. Commit ();}}
12. In the main method, add the following code:
Static voidMain (String[] ARGs) {configuresystem (); createcustomers ();Console. Write ("\ R \ nhit enter to exit :");Console. Readline ();}
13. Run the program. The result is as follows:
The first few lines of SQL commands show that they are removed if the architecture already exists. By executing the preceding command, the database architecture is automatically completed in the database, and two data entries are inserted into the customer table.
Next, we create an order.
14. Add a static method createorder in the program class. The Code is as follows:
Private Static int Createorder (){ VaR Customer = New Customer {Customername = "Intel" }; VaR Order = New Order {Customer = Customer, orderdate = Datetime . Now,}; order. addlineitem (1, "Apple" ); Order. addlineitem (5, "Pear" ); Order. addlineitem (3, "Banana" ); Int Orderid; Using ( VaR Session = sessionfactory. opensession ()) Using (VaR Transaction = session. begintransaction () {session. Save (customer); orderid = ( Int ) Session. Save (order); transaction. Commit ();} Return Orderid ;}
15. In the main method, after createcustomers (), add createorder (); the following code is shown:
Static voidMain (String[] ARGs) {configuresystem (); createcustomers ();// Add it hereVaROrderid = createorder ();Console. Write ("\ R \ nhit enter to exit :");Console. Readline ();}
16. If you run the program at this time, an exception will occur, because by default, the hasmany relation configured by auto-mapper is none-inverse and not cascade. To configure the desired ing, add the following code in the configuresystem method (after VAR model = automap. assemblyof <customer> (CFG ):
Model. Override <Order> (MAP => map. hasmany (x => X. lineitems). Inverse (). Cascade. alldeleteorphan ());
17. Then run the program. Everything is OK, as shown in:
Note: line items of the three orders are automatically inserted into the database.
Next, load an order, delete its line item, and add a new one:
18. Add an updateorder static class to the program class. The Code is as follows:
Private Static voidUpdateorder (IntOrderid ){Using(VaRSession = sessionfactory. opensession ())Using(VaRTransaction = session. begintransaction ()){VaROrder = session. Get <Order> (Orderid); Order. lineitems. removeat (0); Order. addlineitem (2,"Cot"); Transaction. Commit ();}}
19. Call updateorder (orderid) in the main method );
20. Run the program, as shown in:
The first select statement loads order. The second SELECT statement loads the line items of the order. Then, an insert statement is used to add a new line item to the database. Finally, a delete statement removes the line item from the database.
Finally, let's Delete the order.
21. Add a deleteorder static method to the program class. The Code is as follows:
Private Static voidDeleteorder (IntOrderid ){Using(VaRSession = sessionfactory. opensession ())Using(VaRTransaction = session. begintransaction ()){VaROrder = session. Load <Order> (Orderid); Session. Delete (order); transaction. Commit ();}}
22. Call deleteorder (orderid) in the main method );
23. Run the program. The console output is as follows:
The first select statement loads order, and the second statement loads the line item of order. Before the order itself is deleted, all its line items are deleted first.
Summary
This Article first introduces sessions and transactions, and then introduces some theoretical knowledge about addition, deletion, query, and modification using Nhibernate, finally, an example of the console is used to add, delete, query, modify, and output the generated SQL statement. I believe that through this article, you have a certain understanding of the addition, deletion, query, and modification of nhib.pdf.