One-to-multiple nhing of Nhibernate and nhing of Nhibernate

Source: Internet
Author: User

One-to-multiple nhing of Nhibernate and nhing of Nhibernate

In nhib, The ing file corresponds to the relationship in the database table. All Nhibernate instances also have one-to-one ing, one-to-many ing, and many-to-many ing. First, let's look at one-to-many ing. one-to-many ing means that the relationship between two tables in the database is one-to-many. For example, the relationship between students and classes is one-to-many, one class has multiple students, and one student belongs to only one class. The dictionary type and dictionary table are also one-to-many relationships. Use the dictionary type and dictionary table as an example:

One-to-Multiple Association ing can be divided into one-to-one and two-way. One-to-one ing maintains their relationships on one side, and two-way ing maintains relationships on both sides. First, let's look at the one-to-many Association ing.

Dictionary type: DictionTypeEntity and dictionary entity:

Public class DictionaryEntity {// <summary> // ID // </summary> public virtual Guid ID {get; set ;} /// <summary> /// Key /// </summary> public virtual string Key {get; set ;} /// <summary> /// key Value /// </summary> public virtual string Value {get; set ;} /// <summary> /// Type /// </summary> public virtual string Type {get; set ;} /// <summary> /// TimeStamp /// </summary> public virtual string TimeStamp {get; set ;} /// <summary> /// operation user /// </summary> public virtual string AddUser {get; set ;}/// <summary> /// remarks, description /// </summary> public virtual string Remark {get; set ;}}
<Pre name = "code" class = "csharp"> /// <summary> // This entity class and database table 't_ dictionarymanage '. /// </summary> public class DictionaryTypeEntity {# region Object Property // <summary> // Type // </summary> public virtual string Type {get; set ;}//< summary> /// type name /// </summary> public virtual string TypeName {get; set ;} /// <summary> /// TimeStamp /// </summary> public virtual string TimeStamp {get; set ;} /// <summary> /// operation user /// </summary> public virtual string AddUser {get; set ;} # endregion // <summary> // dictionary Object list set // </summary> public virtual IList <DictionaryEntity> DictionaryEntitys {get; set ;}}


 

 

Hbm. xml file corresponding to dictionary type DictionTypeEntity:

<? Xml version = "1.0" encoding = "UTF-8"?> <Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.2"> <class name = "Model. entity. dictionaryTypeEntity, model "table =" T_DictionaryManage "> <id name =" Type "type =" String "unsaved-value =" null "> <column name =" Type "length =" 20" unique = "true"/> <generator class = "assigned"/> </id> <property name = "TypeName" type = "String"> <column name = "TypeName" length = "50" not-null = "true"/> </property> <property name = "TimeStamp" t Ype = "String"> <column name = "'timestamp'" length = "20" not-null = "true"/> </property> <property name = "AddUser" type = "String"> <column name = "AddUser" length = "20" not-null = "false"/> </property> <span style = "white-space: pre "> </span> <! -- Dictionary entity --> <span style = "white-space: pre "> </span> <bag name =" DictionaryEntitys "inverse =" true "cascade =" all "> <span style =" white-space: pre "> </span> <key column =" Type "/> <span style =" white-space: pre "> </span> <one-to-learn class =" Model. entity. dictionaryEntity "/> <span style =" white-space: pre "> </span> </bag> </class> 


Hbm. xml file corresponding to dictionary entity DictionEntity:

<? Xml version = "1.0" encoding = "UTF-8"?> <Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.2"> <class name = "Model. entity. dictionaryEntity, model "table =" T_Dictionary "> <cache usage =" read-write "/> <id name =" ID "type =" Guid "> <column name =" ID "length = "150" index = "PK_Dictionary"/> <generator class = "guid"/> </id> <property name = "Key" type = "String"> <column name = "KeyName" length = "20" not-null = "false"/> </property> <property name = "Value" type = "String"> <Column name = "ValueName" length = "20" not-null = "false"/> </property> <property name = "Type" type = "String"> <column name = "DictionaryType" length = "20" not-null = "false"/> </property> <! -- Multiple ends and dictionary types (remove bidirectional Association ing) --> <! -- <Role-to-one name = "Type" class = "Model. entity. dictionaryTypeEntity "column =" DictionaryType "/> --> <property name =" TimeStamp "type =" String "length =" 20 "> <column name =" DateTimeStamp "not-null = "false"/> </property> <property name = "AddUser" type = "String"> <column name = "AddUser" length = "20" not-null = "false "/> </property> <property name =" Remark "type =" String "> <column name =" Remark "length =" 50 "not-null =" false "/> </property> </class> 



In this way, all records of this type in the dictionary table are also queried when you query a dictionary type. This shows that two tables are not used for joint queries.

Nhibernate supports lazy loading. If lazy = "true" is set, the corresponding records in the dictionary table will not be queried at the beginning and will be found only when they are actually used, in this way, we can improve some efficiency.


Nhib1_one-to-multiple query, showing the content of two tables
How to add a one-to-multiple Hibernate ing

When saving a new Reply, the Reply topic attribute must have a value, for example
Reply reply = new Reply ();
Reply. setid (1 );
Reply. setTitle ("reply: xx ");
Reply. setContent (" SB ");
// The key is in the following sentence:
Reply. setTopic (topic); // This sentence ensures that reply is associated with the corresponding topic object.
Topic is a Topic object, and the id must be an existing id of the database.

Upstairs cascade = 'Save-Update'
Cascade save
After the cascade attribute is configured
If the topic attribute associated with reply does not have the corresponding ID in the database
Then hibernate will cascade topic objects to the database.

The landlord's question should not be answered by the upstairs brothers.
Even if cascade is not configured, as long as the topic corresponding to reply is a persistent or free object
Hibernate will also add a value for the reply topicid field in the database.
Just reply. setTopic ();

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.