hibernate常用关联
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name="Contract.Domain.Teacher,CCRModel" table="TEACHER"> <id name="Id" type="int" column="Id" > <generator class="native" /> </id> <property name="TName" column="T_NAME" /> <bag name="Students" cascade="all"> <key column="TEACHER_ID" not-null="true"/> <one-to-many class="Contract.Domain.Student"/> </bag> </class> </hibernate-mapping> <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name="Contract.Domain.Student,CCRModel" table="STUDENT"> <id name="Id" type="int" column="Id" > <generator class="native" /> </id> <property name="SName" column="S_NAME" /> <many-to-one name="Teacher" class="Contract.Domain.Teacher" column="TEACHER_ID" not-null="true"/> </class> </hibernate-mapping> var session1 = Dao.GetSession(); Teacher t = new Teacher() { TName = "laoshi" }; Student s1 = new Student() { SName = "t1" }; Student s2 = new Student() { SName = "t2" }; t.Students = new List<Student>(); t.Students.Add(s1); t.Students.Add(s2); s1.Teacher = t; s2.Teacher = t; session1.SaveOrUpdate(t); session1.Flush(); var gt=session1.Get<Teacher>(106309155); var stu = session1.Get<Student>(106309157); return; 多对多 关系拆分 分为1和2和3三个步骤,其中BasIssuer和BasCreditRating两个都是一端各自拥有一个1对多的关系,而中间实体是多端配置有两个多对一的关系 1. <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name="Contract.Domain.BasIssuer,BASModel" table="BAS_ISSUER"> <id name="Id" column="ISSUER_ID" unsaved-value="null"> <generator class="sequence" > <param name="sequence">BAS_MODULE_SEQ</param> </generator> </id> <property name="IssuerCode" column="ISSUER_CODE" /> <property name="IssuerName" column="ISSUER_NAME" /> <!--<property name="Region" column="REGION_ID" />--> <property name="CreateUid" column="CREATE_UID" /> <property name="CreateUname" column="CREATE_UNAME" /> <property name="CreateOid" column="CREATE_OID" /> <property name="CreatePid" column="CREATE_PID" /> <property name="CreateDate" column="CREATE_DATE" /> <property name="LastModUid" column="LAST_MOD_UID" /> <property name="LastModUname" column="LAST_MOD_UNAME" /> <property name="LastModOid" column="LAST_MOD_OID" /> <property name="LastModPid" column="LAST_MOD_PID" /> <property name="LastModDatetime" column="LAST_MOD_DATETIME" /> <property name="Status" column="STATUS" /> <property name="IsDeleted" column="IS_DELETED"/> <!--<property name="CreditRate" column="CREDIT_RATE" />--> <!--<property name="CreditAgencyId" column="CREDIT_AGENCY_ID" />--> <property name="IssuerType" column="ISSUER_TYPE" /> <!--<property name="IndustrySectorId" column="INDUSTRY_SECTOR_ID" />--> <property name="CreatedEmployeeName" column="CREATED_EMPLOYEE_NAME" update="false"/> <property name="CreatedEmployeeId" column="CREATED_EMPLOYEE_ID" update="false"/> <many-to-one name="Region" class="Contract.Domain.BasRegion" column="REGION_ID"/> <many-to-one name="IndustrySector" class="Contract.Domain.BasSectorInfo" column="INDUSTRY_SECTOR_ID"/> <bag name="IssuerRating" cascade="all"> <key column="E_ID" /> <one-to-many class="Contract.Domain.RealCreditEntity,BASModel" /> </bag> </class> </hibernate-mapping> 2. <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name="Contract.Domain.BasCreditRating,BasModel" table="BAS_CREDIT_RATING"> <id name="Id" type="string" column="Id" unsaved-value="null"> <generator class="sequence" > <param name="sequence">BAS_MODULE_SEQ</param> </generator> </id> <property name="CreditRatingCode" > <column name="CREDIT_RATING_CODE"> <comment> 信用评级编码 </comment> </column> </property> <property name="CreditRatingName" > <column name="CREDIT_RATING_NAME"> <comment> 信用评级名称 </comment> </column> </property> <property name="RatingTypeLookupId" > <column name="RATING_TYPE_LOOKUP_ID"> <comment> 评级类型ID </comment> </column> </property> <!--<property name="CreditRatingAgencyId" > <column name="CREDIT_RATING_AGENCY_ID"> <comment> 评级机构ID </comment> </column> </property>--> <property name="CreditRatingValue" > <column name="CREDIT_RATING_VALUE"> <comment> 评级分数 </comment> </column> </property> <property name="Remarks" > <column name="REMARKS"> <comment> 备注 </comment> </column> </property> <property name="CreatedBy" > <column name="CREATED_BY"> <comment> 创建人 </comment> </column> </property> <property name="CreateDate" update="false"> <column name="CREATE_DATE"> <comment> 创建日期 </comment> </column> </property> <property name="UpdatedBy" > <column name="UPDATED_BY"> <comment> 更新人 </comment> </column> </property> <property name="UpdateDate" > <column name="UPDATE_DATE"> <comment> 更新日期 </comment> </column> </property> <property name="CreatedEmployeeName" update="false"> <column name="CREATED_EMPLOYEE_NAME"> <comment> created_employee_name </comment> </column> </property> <property name="CreatedEmployeeId" update="false"> <column name="CREATED_EMPLOYEE_ID"> <comment> created_employee_id </comment> </column> </property> <property name="Version" > <column name="VERSION"> <comment> 评级版本 </comment> </column> </property> <property name="IsDeleted" > <column name="IS_DELETED"> <comment> 假删除标记 </comment> </column> </property> <bag name="RatingIssuer" > <key column="C_ID" /> <one-to-many class="Contract.Domain.RealCreditEntity,BASModel" /> </bag> <many-to-one name="CreditRatingAgency" class="Contract.Domain.BasCreditRatingAgency" column="CREDIT_RATING_AGENCY_ID"/> </class> </hibernate-mapping> 3. <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name="Contract.Domain.RealCreditEntity,BASModel" table="RELA_CREDIT_ENTITY"> <id name="Id" column="ID" unsaved-value="null"> <generator class="sequence" > <param name="sequence">BAS_MODULE_SEQ</param> </generator> </id> <property name="RatingDate" column="RATING_DATE" /> <many-to-one name="Issuer" class="Contract.Domain.BasIssuer" column="E_ID"/> <many-to-one name="Rating" class="Contract.Domain.BasCreditRating" column="C_ID"/> </class> </hibernate-mapping>