配置文件:TbAgent <bag name="TbSend" table="tb_send" >
<key column="MobilePhone" not-null="true" />
<one-to-many class="sms.Model.TbSend,sms.Model"/>
</bag>TbSend<many-to-one name="TbAgent" column="RecPhone" class="HyundaiSms.Model.TbAgent,HyundaiSms.Model" unique="true" />RecPhone,MobilePhone 分别是2个类中的属性,本来我是关系设置成 tbsend.recphone = tbagent.mobilephone,但是跟踪到的时tbsend.recphone = tbagent.id(id是这个类中的一个属性),不知道为什么?怎么解决
<key column="MobilePhone" not-null="true" />
<one-to-many class="sms.Model.TbSend,sms.Model"/>
</bag>TbSend<many-to-one name="TbAgent" column="RecPhone" class="HyundaiSms.Model.TbAgent,HyundaiSms.Model" unique="true" />RecPhone,MobilePhone 分别是2个类中的属性,本来我是关系设置成 tbsend.recphone = tbagent.mobilephone,但是跟踪到的时tbsend.recphone = tbagent.id(id是这个类中的一个属性),不知道为什么?怎么解决
获取到的tbagent.id 就是父表主键,怎么改关系貌似都不起左右,不知道怎么改
望神人指教
或者给发一遍与此类似结构的教程也可以
先谢谢了
id int
proid int
description nvarchar 256
adddate nvarchar 32
isok int
filename nvarchar 512
tbugreport表结构(“多”端):
id int
bugid int: 对应tbug中id字段
type int
adddate nvarchar 32
title nvarchar 256
content text 16
tbug的映射类:CBugusing System;
using System.Collections;
namespace nhibetest
{
/// <summary>
/// CBug 的摘要说明。
/// </summary>
public class CBug
{
private int _id,_proid,_isok;
private string _description,_adddate,_filename;
private ICollection _reportcollection;
public CBug()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public int id
{
get
{
return this._id;
}
set
{
this._id=value;
}
}
public int proid
{
get
{
return this._proid;
}
set
{
this._proid=value;
}
} public int isok
{
get
{
return this._isok;
}
set
{
this._isok=value;
}
}
public string description
{
get
{
return this._description;
}
set
{
this._description=value;
}
}
public string adddate
{
get
{
return this._adddate;
}
set
{
this._adddate=value;
}
}
public string filename
{
get
{
return this._filename;
}
set
{
this._filename=value;
}
}
public ICollection reportcollection/////对应tbugreport中的相关记录
{
set
{
this._reportcollection=value;
}
get
{
return this._reportcollection;
}
}
}
}
用于影射类与数据库之间的配置文件:CBug.hbm.xml<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="nhibetest.CBug,nhibetest" table="tbug">
<id name="id" column="id" type="Int32" unsaved-value="null">
<generator class="identity"/>
</id>
<property name="proid" column="proid" type="Int32"/>
<property name="isok" column="isok" type="Int32"/>
<property name="description" column="description" type="String" />
<property name="adddate" column="adddate" type="String"/>
<property name="filename" column="filename" type="String"/>
<!--配置tbugreport中的对应子集,一对多-->
<set name="reportcollection" inverse="true" table="tbugreport" cascade="all">
<key column="bugid" /><!-- 对应tbugreport表中的bugid字段-->
<one-to-many class="nhibetest.CBugreport,nhibetest" />
</set>
</class>
</hibernate-mapping>tbugreport的映射类文件:using System;namespace nhibetest
{
/// <summary>
/// CBugreport 的摘要说明。
/// </summary>
public class CBugreport
{
private int _id,_bugid,_type;
private string _adddate,_title,_content;
private CBug _bug;
public CBugreport()
{
//
// TODO: 在此处添加构造函数逻辑
//
} public int id
{
get
{
return this._id;
}
set
{
this._id=value;
}
}
public int bugid
{
get
{
return this._bugid;
}
set
{
this._bugid=value;
}
}
public int type
{
get
{
return this._type;
}
set
{
this._type=value;
}
}
public string adddate
{
get
{
return this._adddate;
}
set
{
this._adddate=value;
}
}
public string title
{
get
{
return this._title;
}
set
{
this._title=value;
}
}
public string content
{
get
{
return this._content;
}
set
{
this._content=value;
}
}
public CBug bug///////////对应相关表tbug 中的一条记录
{
set
{
this._bug=value;
}
get
{
return this._bug;
}
} }
}
配置影射的文件CBugreport.hbm.xml <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="nhibetest.CBugreport,nhibetest" table="tbugreport">
<id name="id" column="id" type="Int32" unsaved-value="null">
<generator class="identity"/>
</id>
<!--
<property name="bugid" column="bugid" type="Int32"/>
-->
<property name="type" column="type" type="Int32"/>
<property name="title" column="title" type="String" />
<property name="adddate" column="adddate" type="String"/>
<property name="content" column="content" type="String"/>
<!--配置tbug中的对应子集,多对一-->
<many-to-one name="bug"
column="bugid" <!--为表tbugreport中的bugid字段,该字段不需要另外映射-->
not-null="true"
class="nhibetest.CBug,nhibetest"
cascade="all"
outer-join="auto"
update="true"
insert="true"
/>
</class>
</hibernate-mapping>
我的关系是:子表关联对象不是父表中的主键,而是另一个字段;但是设置后抓到的sql语句一直是连接父表的主键,这就是问题
实际数据表里的数据都是一对多的情况
我试了多对多的关系,也不行。
不知道是哪里设置错误了
property-ref(可选):指定关联类的一个属性名称,这个属性会和外键相对应。如果没有指定,会使用对方关联类的主键。这个属性通常在遗留的数据库系统使用,可能有外键指向对方关联表的某个非主键字段(但是应该是一个唯一关键字)的情况下,是非常不好的关系模型。比如说,假设Customer类有唯一的CustomerId,它并不是主键。这一点在NHibernate源码中有了充分的体验。
结贴