The basic feature of NHibernate is to accomplish the mapping of object-oriented programming language to relational database, and to use the Persisted object Po (persistent object) to complete the persistence operation in NHibernate. The operation of the PO must be in Session management to synchronize to the database, but here the session does not mean httpsession, it can be understood that the connnection,s
The content of this section
Operational Data Overview
1. New Object
2. Deleting objects
3. Updating objects
4. Save the updated object
Conclusion
Operational Data OverviewWhat we often call a unit of work, typically performing 1 or more operations, is either committed or discarded/rolled back for these operations. Thinking about using LINQ to SQL, everything is operating in memory, and only the Datacontext.submitchanges () method is called to commit the changed
The contents of this section
Associating Query Introduction
One-to-many Association Query
1. Native SQL Association Query
2.HQL Association Query
3.Criteria API Association Query
Conclusion
Associating Query Introduction
There are three ways to choose from NHibernate: NHibernate Query Language (hql,nhibernate query Language), conditional query (criteria a
sql: Stored ProcedureDROP Table Departmentgocreate Table Department (Id INT IDENTITY (PRIMARY) key,depname VARCHAR (), PhoneNumber VARCHAR (()) gocreate PROCEDURE [dbo]. [Getdepartmentid] (@Id INT) As BEGIN SELECT * from Department WHERE department.id= @Id endgoexec Getdepartmentid 1GO Reference: Http://stackoverflow.com/questions/6373110/nhibernate-use-stored-procedure-or-mapping Fluent
Using server controls such as ObjectDataSource and GridView can indeed achieve very oop (however, I prefer to bind with Repeater, which is a little troublesome, and this is very fast)
However, if dependency injection is required for applications in a multi-layer framework, ObjectDataSource needs to be bound to the interface.
ObjectDataSource only supports classes and cannot instantiate interfaces. This will lead to the dependency of the multi-layer structure.
Google found it for half a day. You
During routine development, NHibernate transactions are often used.
Because NHibernate does not support nested transaction operations, ASP. net mvc usually uses a transaction to encapsulate database operations in a request.
The following are some of the most beautiful statements I think.
View sourceprint? 01 using System. Web. Mvc;
02 using nhib.pdf;
03
04 namespace WebMVC. Filters
05 {
06 public class Tran
Use the HQL in NHibernate to query the data.Code:1 /// 2 /// Find Events 3 /// 4 Private void Btn_select_click (object sender, EventArgs e)5 { 6 7 }1. Query, and use parameters:1 //* * * Query * * *2IQuery query = session. CreateQuery ("From person p where p.id =? and P.name =: Name");3 4 //using parameters in NHibernate5 //one is "?" and one uses ":". If you use? ,
Add to:1 /// 2 ///wait for party B to respond A3 /// 4 Private voidButton2_click_1 (Objectsender, EventArgs e)5 {6 //use NHibernate to store7Chinfo C =Newchinfo ();8 9C.ctinfoid =ID;TenC.goodsinfo ="White Jade Bracelet"; OneC.buyersinfo ="Party"; AC.sellerinfo ="Party B"; -C.infocreatetime = DateTime.Now;//The field Infocreatetime is a datetime type, so it cannot be ToString here. -C.currentgoodsstat
the name of the result file generated by the class library project.Isessionfactory factory = cfg. buildsessionfactory ();Isession session = factory. opensession ();Itransaction transaction = session. begintransaction ();User newuser = new user ();Newuser. ID = "joe_cool ";Newuser. Username = "Joseph cool ";Newuser. Password = "ABC123 ";Newuser. emailaddress = "joe@cool.com ";Newuser. lastlogon = datetime. now;// Tell nhibtasks that this object shocould be savedSession. Save (newuser );
// Com
[Subject to nhibernate-PreAlpha-Build-2]
We can see that session and sessionfactory are the core parts of Nhibernate.
Sessionfactory maintains connections to the persistent mechanism (database) and manages them. It also saves the ing information of all persistent objects.
Sessionfactory is created by configuration. buildsessionfactory. This object generally uses the singleton mode.
Session is used
We ' ve got a large system that's loosely bound to it data Source (Navision) via Unity-we ' re getting the opportunity to s WAP it out and has our own database.So we ' ve had a look around and really like the look of the Fluent Nhibernate-we ' re trying to get a proof of concept going an D swap out a couple of the services.We want to use nhibernates HiLo algorithm-unfortunately we ' ve inherited string ID's from Navision which prefixs its ID ' s (exam
Sql:select * FROM table T where (t.name like '% Zhang San% ' or t.schoolname like '% Zhang San% ' or t.cityname like '% Zhang San ')You can write it with nhibernate criteria.var criteria = Nhibernatesessionreader.createcriteria (typeof (Contract));if (!string. IsNullOrEmpty (KeyWord)){Criteria. ADD (Expression.like ("name", KeyWord, Matchmode.anywhere)|| Expression.like ("school", "%" + KeyWord + "%")|| Expression.like ("CityName", "%" + KeyWord + "%"
; private string sex; private string weight; Private Master Master; Public Dog () {} public virtual string id {get {return Id;} set {id = value;} } public virtual string name {get {return Name;} set {name = value;} } public virtual string sex {get {return Sex;} set {sex = value;} } public virtual string Weight {get {return Weight;} set {weight = value; }} public Virtual Master master//here In addition to the b
I. Creating an update DDL
nhibernate.config configuration file
Model class Library is as follows:
Blog.cs class and Blog.hbm.xml entity mapping files
public class Blog
{public
blog ()
{
Tags = new hashset
Category.cs class and Category.hbm.xml entity mapping files
public class Category
{public
Category ()
{
Posts = new hashset
Comment.cs class and Comment.hbm.xml entity mapping files
public class Comment {public virtua
project structure as shown:
Product.cs Model Class
public class Product
{public
virtual Guid Id {get; set;}
Public virtual string Name {get; set;}
Public virtual string Category {get; set;}
public virtual int price {get; set;}
}
Entity mapping file Product.hbm.xml
Global configuration file Hibernate.cfg.xml
Program.cs Main program code
Class Program {static void Main (string[] args) {//nhibernate
The contents of this section
Operational Data Overview
1. New Object
2. Delete objects
3. Update objects
4. Save updated objects
Conclusion
Operational Data Overview
We often refer to a unit of work that usually performs 1 or more operations, which are either committed or discarded/rolled back. Think about using LINQ to SQL, everything is in memory, and only the Datacontext.submitchanges () method is invoked to submit the changed data to the database, and LINQ to SQL commits or rolls bac
The contents of this section
Introduced
Scenario 1: Adding directly
Scenario 2: Clever use of components
Example analysis
Conclusion
Introduced
Through the previous 7 of the study, a bit boring ~ ~ ~ ~ ~ This article to learn a skill, we think about if I want to implement a FullName attribute in the customer class (that is, the combination of FirstName and LastName) How to do?
Scenario 1: Adding directly
"I know! Modify the customer class to add a FullName property! namely customer.full
Many people used the nhibernate to operate a one-to-many relationship, using for use in the corresponding class: public class classes{ private, ISet Public virtual ISet { get {return _students;} set {_students = value;} } } looks normal, but the runtime will have an error as follows: cannot type ' NHibernate.Collection.Generic.PersistentGenericSet ' 1[ Nhstusmanagerapp.student] "to cast the object to type ' System.Collection
Error: Could not get or update next Value[sql:] NhibernateWorkaround: The database dialect is misconfigured in the database configuration file. I used the error when I was using SQL Server database servers but wrote in the database configuration file of Oracle's database dialect. Change to SQL Server's database dialect, all OK.Note: There are a number of situations where could not get or update next Value[sql:] NHibernate error. It is also possible th
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.