Recently, I have been busy preparing for the trip, so the progress is coming slowly. I will leave tomorrow. Hey hey, let's write more than one pair before departure. If you read this series for the first time, you can take a look at the previous articles to learn more.
I. Opening: isessionfactory Configuration
Ii. entity ing: entity Mapping
Iii. Inheritance ing: inheritence Mapping
4. One-to-one mapping
V. One-to-multiple ing
Scenario and database design
In the first two articles, we introduced "one-to-one" and "one-to-many (many-to-one)". Maybe the first two articles use many, but many-to-many relationships, sometimes we will also encounter, such as the e-commerce station we have been demonstrating, the relationship between our orders and products is a very typical "many to many ". Let's look at our database design:
Here, orders do not need to be known for products, or they can be loaded together, so they can be delayed or not loaded. For orders, it should be loaded immediately to know all the items in the order. With expansion, we must add related attributes to our product and order:
Public abstract class Product { Public Virtual int Productid { Get ;Set ;} Private Iset < Order > M_orders = Null ; Public Iset < Order > Orders { Get { If ( This . M_orders = Null ){ This . M_orders =New Hashedset < Order > ();} Return this . M_orders ;} Set { This . M_orders = Value ;}} // Product other property } Public class Order { Public Order (){ This . Products =New Hashedset < Product > ();} Public Virtual int Orderid { Get ; Set ;} Public Iset < Product > Products { Get ; Set ;} // Order other property }
Ing
If you have read several previous articles, I think there should be no problem, because fluent nhib.pdf is really simple and smooth,CodeAs follows:
Public class Productmap : Classmap < Product > { Public Productmap () {ID (P => P. productid); hasmanytod < Order > (P => P. Orders). Asset (). lazyload (). parentkeycolumn ( "Productid" ). Childkeycolumn ( "Orderid" ). Table ( "Orderproduct" ); Map (P => P. createtime); Map (P => P. Name); Map (P => P. Price );}} Public class Ordermap : Classmap < Order > { Public Ordermap () {ID (O => O. orderid). generatedby. Identity (); hasmanytomany < Product > (O => O. Products). Asset (). Not. lazyload (). Cascade. All (). parentkeycolumn ( "Orderid" ). Childkeycolumn ( "Productid" ). Table ("Orderproduct" ); Map (O => O. Price); Map (O => O. State). customtype < Orderstate > (); Map (O => O. address); Map (O => O. coignee); Map (O => O. createtime); Map (O => O. zip); References < User > (O => O. User). Not. lazyload (). Column ( "Userid" );}}
Here we use a separate table to store this many-to-many relationship, so we need table ("table name ").
Parentkeycolumn and childkeycolumn are both relative to their own. You can also generate HBM to see many-to-many traditional writing methods.
Test
After the ing is completed, let's test it. We still use xunit for unit testing:
[ Fact ] Public void Createorder (){Using ( VaR Session = This . Sessionfactory. opensession () {session. transaction. Begin (); VaR Products = session. createcriteria < Product > (). List < Product > (); VaR User = session. Load < User > (1 ); VaR Order = New Order {User = user, address ="Shang Hai" , Coignee = "Candy" , State = Orderstate . Created, createtime = Datetime . Now, zip = "200336" }; Order. products. addall (products); Order. price = order. products. sum (P => P. price); Session. save (order); Session. transaction. commit ();}}
(The test code is not very complete.ArticleEndSource codeYou can download and learn .)
OK. Let's take a look at our test results. We need to: Green
Haha, a lot of SQL statements, good and good.
Summary
After a long delay, I finally finished talking about the relationship. There are still some articles later. I will wait for the tour to come back and yell.
But now, we can basically build a general BS project. I didn't expect to write it, so I built a preliminary e-commerce website.
: Click here to download the source code
PS: Give me Aladdin's oil, and let me eliminate the storm .....
PS2: it seems that not many people like it. Maybe the text is not very good. Continue to exercise. If any, contact me.