Nibernate ing configuration, simple notes, and configuration instructions.

Source: Internet
Author: User

Blogs and documents

Blog address http://www.cnblogs.com/GoodHelper/archive/2011/02/14/nhiberante_01.html part of the code and diagram in this article is copied from here.

 

It's a bit shameful. It's always just a cat-and-tiger pattern, but it's always a little poor in understanding, but I want to find the first line in the eye-catching process, after that, you don't have to remember anything. You can configure your eyes with closed eyes.

Here we use text, color, a small amount of documentation instructions, combined with instances, to parse each configuration instance.

I believe it will be helpful for my colleagues who have just learned about hibernate.

 

Summary:

Basic ing is not much said about the property generator. If the primary key is necessary, it is rarely used for joint primary keys.

PS University has studied foreign keys for databases, but the training of. NET and the suggestions of some of my superiors are that foreign keys are not needed, but I added some points when necessary ...... Added cascade and trigger

Because it is rarely used, I started to contact hibernate, and the foreign key is still dizzy for a while, maybe as dizzy as I do.

Add here

Example

Stuent is a foreign key table, and class is a primary key table.

public class Class{public virtual int? ID { get; set; }public virtual string Name { get; set; }}public class Student{public virtual int? ID { get; set; }public virtual string Name { get; set; }public virtual Class Class { get; set; }}

Name attribute name, class primary key table corresponding class, colum foreign key table foreign key, property_ref foreign key table foreign key corresponding class attribute, default is primary key.

Write with closed eyes

Ing relationship: This type of attribute, corresponding to the type, corresponding to the relationship, this type (Student) corresponds to the table (student) foreign key field class primary key attribute.

<Class name = "">

<Role-to-one name = "" class = "" column = "" property-ref = ""/>

</Class>

This is

The default value can be abbreviated

<Role-to-one name = "Class" column = "ClassID"/>

Abbreviated as the default value

Class(Optional): name of the associated class. The corresponding type can be found in reflection.

: (Optional) specifies an attribute of the associated class, which corresponds to the foreign key. If this parameter is not specified, the primary key of the associated class is used. That is to say.

 

The following are the original instructions of the document.

Property-refThe attribute should only be used to deal with the old database system. A foreign key may point to a non-primary key field (but it should be a unique keyword) in the target table. This is an ugly relational model. For example, supposeProductThe class has a unique serial number, which is not the primary key. (Unique attributes control the DDL generation process of NHibernate through SchemaExport .)

 

Or the previous relationship diagram.

public class Student    {        public virtual int? ID { get; set; }        public virtual string Name { get; set; }    }    public class Class    {        public virtual int?  { get; set; }        public virtual string Name { get; set; }        public virtual IList<>  { get; set; }    }

A class may have multiple students. A student is a collection of ILIST

Primary Key of this type () -- The foreign key () of the table () corresponding to each element () in the one-to-many m collection (IList <Student> ()

<Class name = "" table = "T_Class" lazy = "true">
<Id name = "" type = "int" column = "ClassID"> This ClassID is only used to configure the primary key of the Class.
<Generator class = "native"/>
</Id>

<Bag name = "">
<Key column = ""/> This ClassID actually refers to the field of the Student table corresponding to the class.
<One-to-learn class = ""/>
</Bag>

</Class>

 

.

 

1. Unidirectional primary key Association ing

There is nothing new in this relationship association configuration, mainly the configuration of the primary key.

No additional table fields are required for primary key Association. If the two rows are associated through this one-to-one relationship, the two rows share the same primary key keyword value. So if you want two objects to be associated one by one through the primary key, you must ensure that they are assigned the same ID value!

<Class name = "" table = "PERSON"> <id name = "Id" column = "PERSON_ID"> <generator class = "foreign"> <param name = "property"> Employee </param>, the primary key value is configured to be queried through a foreign key. The primary key value of Person is the primary key value of the Exmloyee class </generator> </id>... <one-to-one name = "" class = "Employee" constrained = "true"/> ing: Ignore the preceding primary key configuration, the primary key Id = the primary key of the attribute. one-to-one also has the-ref option. If it is the primary key, it can be saved. This option is set for one-to-one ing of the unique foreign key.
</Class>

The above is one-to-one.

To add the "Person" attribute to the "Employee" class, add the "Person" attribute to the ing.

<One-to-one name = "Person" class = "Person"/>

OK. The primary key ID of Emplyee is the ID of the Person attribute in the Employee class.

Use

<Role-to-one name = "Person" class = "Person" column = "PERSON_ID" unique = "true"/> can also achieve the same effect, indicating that it is one-to-one, column = "PERSON_ID" indicates the primary key of the Person class corresponding to the PERSON_ID of the Employee.

Unique foreign key Association ing

Unidirectional.

<many-to-one name="Person" class="Person" column="PERSON_ID" unique="true"/>

If the following sentence is added to the Person ing, this association is bidirectional:

<One-to-one name = "Employee" class = "Employee" property-ref = "Person"/>
Person primary key ID = primary key ID of the Person attribute of the Employee class


In the following example, many-to-many relationships are directly configured. in EF, many-to-many relationships are implicitly mapped.

One-way, many-to-many, two-way is also a truth
Multi-to-many, relational, Active configuration side, default ID attribute (Table primary key ).

T_User's primary key ID () =, = table's primary key ID (RoleID)
<Role-to-Role class = "Role" column = "RoleID"/> similar

// This ROLE is associated with the role id (primary key) by default)
In some of the previous vertices, if it is a primary key, it can be omitted without writing. If it is not a primary key, the property-ref configuration is added, but I have not tried this.
<Class name = "" table = "T_User" lazy = "true">
<Id name = "ID" type = "int" column = ""> This is the primary key configured for the User class and is irrelevant to the following link configuration.
<Generator class = "native"/>
</Id>

<Bag name = "Roles" table = "">
<Key column = ""/>
<Skip-to-skip class = "" column = ""/>
</Bag>

</Class>


Related Article

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.