After the introduction in the previous three articles, I believe you have some knowledge about fluent nhib.pdf. In our study, fluent has also entered the RTM version. The release of this version is less than half a month away from the RC version. It fixes many bugs and adds a large number of features. During daily updates, we also see a large number of unit tests, we believe that fluent nhib.pdf is relatively stable and mature. Compared with the RC version, RTM does not have much change in usage, so we will not explain it.
In the subsequent tutorial, we will use the RTM version for demonstration. We hope you can update it in time (click to download the latest version ).
Fluent nhib.pdf travel series Navigation:
I. Opening: isessionfactory Configuration
Ii. entity ing: entity Mapping
Iii. Inheritance ing: inheritence Mapping
Today we will talk about R ing in Orm. Most of our databases are relational databases, so we can say that the relationship is also a very important part in our database design, nhib.pdf also attaches great importance to this item, but in the traditional method, the configuration is troublesome, not to say that we can use fluent, but the XML in the traditional method does not look very beautiful, but the fluentCodeStyle, more in line with our developer habits.
Database relationship
Database relationships generally include:
1. One-to-one
2. one-to-many
3. many-to-many
Start
In combination with the examples of the first three series, we add a user table [user] this time to store user information and add a userdetail as the user details. The database design is as follows:
It is simple enough. The relationship between user and userdetail is one-to-one, and our entity class is constructed:
Public class User { Public Virtual int Userid { Get ; Set ;} Public Virtual string Username { Get ; Set ;} Public Virtual string Password { Get ; Set ;} Public Virtual Datetime Createtime { Get ; Set ;} Public Virtual Userdetail Detail { Get ; Set ;}} Public class Userdetail { Public Virtual User User { Get ; Set ;} Public Virtual int Userid { Get ; Set ;} Public Virtual Datetime Lastupdated { Get ; Set ;} Public Virtual Personname Name { Get ; Set ;}} Public class Personname { Public Virtual string Firstname {Get ; Set ;} Public Virtual string Lastname { Get ; Set ;}}
Hey, why are there three models? I found that the component mapping was not mentioned in the previous series, so I will talk about it today. This is a simple one-to-one design. First, we only need to implement ing. For other features, such as delayed loading, we will talk about it later. Run it first.
Ing
Fluent Nhibernate ing code:
Public class Usermap : Classmap < User > { Public Usermap () {ID (u => U. userid ). generatedby. identity (); Map (u => U. username); Map (u => U. password); Map (u => U. createtime); hasone < Userdetail > (U => U. Detail). Cascade. All (). propertyref ( "User" );}} Public class Userdetailmap : Classmap < Userdetail > { Public Userdetailmap () {ID (u => U. userid). Column ( "Userid" ). Generatedby. Foreign ( "User" ); Hasone < User > (D => D. User). Cascade. All (). Constrained (); Map (u => U. lastupdated). nullable (); component < Personname > (U => U. Name, P => {P. Map (O => O. firstname). Column ( "[First name]" ); P. Map (O => O. lastname). Column ( "[Last name]" );});}}
Note the following points in the Code: because the primary key ID used by userdetail is consistent with the user ID, we need to use Foregin to obtain the user ID. The foreign usage is a little different from the previous version. You need to specify the propertyname. Many associated methods are very similar to nhibloud. For example, cascade and cascade. All represent cascade = "all", indicating that all operations will operate associated objects at the same time.
After the ing, let's test:
[ Fact ] Public void Createusertest (){ VaR Factory = Fluentsessionfactory . Getcurrentfactory (); Using ( VaR Session = factory. opensession ()){ Datetime Createtime = Datetime . Parseexact ( " , "Yyyy-mm-dd hh: SS" , Null ); User User = New User () {Createtime = createtime, password = "Ilovecandy" , Username = "James" ,}; Userdetail Detail = New Userdetail {Name = New Personname {Firstname = "James" , Lastname = "Ying" }, Lastupdated = createtime,}; detail. User = user; user. Detail = detail; Session. Save (User); Session. Flush ();}}[Fact ] Public void Selectusertest (){ VaR Factory = Fluentsessionfactory . Getcurrentfactory (); Using ( VaR Session = factory. opensession ()){ User User = session. Get < User > (1 ); Assert . Equal ( "James" , User. Detail. Name. firstname );}}
After this article, the unit test will use xunit. You can click here to download it.
One insert test and one query test show the test results:
Output:
OK. The test is successful. Our one-to-one simple ing is complete, and the component ing is also completed. Next we will talk about delayed loading.
One-to-one loading Delay
Careful friends will surely find that the SQL statements output by us use joint queries, but sometimes for us, we only need users. I don't need to query userdetail, you may say that the following method is used for delayed loading:
Hasone <Userdetail> (U => U. Detail). Cascade. All (). lazyload ();
Although fluent is supported and compiled successfully, an exception is thrown when the isessionfactory is created, because Nhibernate does not support the lazy feature of one-to-one, in other words, Nhibernate does not support one-to-one loading delay. However, I checked a lot of information and said I could use it:
Hasone <Userdetail> (U => U. Detail). Cascade. All (). Fetch. Select ();
Hasone <User> (D => D. User). Cascade. All (). Constrained ();
Delayed loading, but the results are only divided into two SQL statements for query, not delayed loading, which can be viewed through SQL Server Profiler:
Nhibernate does not support one-to-one delayed loading. I don't know why, but we can perform delayed loading in a simple way.ArticleThe solution in "one-to-one associated delayed loading in Nhibernate" is provided. You can take a look.
Summary
As Association is an important part of data, you are prepared to split it into upper and lower layers for further explanation. Today, we talked about the simple one-to-one relationship in the association. In fact, the one-to-one relationship is not simple. During the first contact, we will inevitably encounter various problems. You are welcome to leave a message and discuss the issue together.