我想定义一个one-to-many在user,purchase表中.(两个表通过Name列确定关系)在user表中已在user.hbm.xml 中加入如下定义:
<set name="Purchase" table="purchase" cascade="all" lazy="false">
<key column = "Name" />
<one-to-many class="Data1.Purchase,Data1" />
</set>并在user.cs中加入如下属性
private IDictionary _Purchase= new Hashtable();
public IDictionary Purchase {
get
{
return _Purchase;
}
set
{
_Purchase = value;
}
}那我还需不需要在purchase.hbm.xml中加入:manyt to one定义呢?
<many-to-one name="User" class="Data1.User,Data1" column="Name" />
是否需要在purchase.cs中加入,如果加入下面的内容,是否需要删除原先在purchase.cs中定义的Name属笥?
private User _User;
public User User
{
get
{
if ( _User == null ) _User = new Hashtable();
return _User;
}
set { _User = value; }
}是否完成了上面的部分就算完成了one-to-many的定义呢??????
还有没有别的东西需要设定?如果我想将从两个表中的数据查询出来显示在DataGrid中
则下面的代码是否可行?
public IList GetUsersOneToMany()
{
IList users = null;
ITransaction tx = null;
try
{
if(!session.IsConnected)
{
session.Reconnect();
}
tx = session.BeginTransaction(); ICriteria c=session.CreateCriteria(typeof(User));
users=c.List ();
session.Disconnect();
}
catch (Exception ex)
{
throw ex;
tx.Rollback();
session.Disconnect();
// handle exception.
}
return users; }
<set name="Purchase" table="purchase" cascade="all" lazy="false">
<key column = "Name" />
<one-to-many class="Data1.Purchase,Data1" />
</set>并在user.cs中加入如下属性
private IDictionary _Purchase= new Hashtable();
public IDictionary Purchase {
get
{
return _Purchase;
}
set
{
_Purchase = value;
}
}那我还需不需要在purchase.hbm.xml中加入:manyt to one定义呢?
<many-to-one name="User" class="Data1.User,Data1" column="Name" />
是否需要在purchase.cs中加入,如果加入下面的内容,是否需要删除原先在purchase.cs中定义的Name属笥?
private User _User;
public User User
{
get
{
if ( _User == null ) _User = new Hashtable();
return _User;
}
set { _User = value; }
}是否完成了上面的部分就算完成了one-to-many的定义呢??????
还有没有别的东西需要设定?如果我想将从两个表中的数据查询出来显示在DataGrid中
则下面的代码是否可行?
public IList GetUsersOneToMany()
{
IList users = null;
ITransaction tx = null;
try
{
if(!session.IsConnected)
{
session.Reconnect();
}
tx = session.BeginTransaction(); ICriteria c=session.CreateCriteria(typeof(User));
users=c.List ();
session.Disconnect();
}
catch (Exception ex)
{
throw ex;
tx.Rollback();
session.Disconnect();
// handle exception.
}
return users; }
NHibernate.ADOException: problem in find ---> NHibernate.PropertyAccessException: The type NHibernate.Collection.Bag can not be assigned to a property of type System.Collections.IDictionary setter of Data1.User.Purchase ---> System.ArgumentException: 对象类型无法转换为目标类型。
能不能说得详细一点啊?不是很明白!!!!提示这个错误应该怎么解决呢?
NHibernate.ADOException: problem in find ---> NHibernate.PropertyAccessException: The type NHibernate.Collection.Bag can not be assigned to a property of type System.Collections.IDictionary setter of Data1.User.Purchase ---> System.ArgumentException: 对象类型无法转换为目标类型。
http://community.csdn.net/Expert/TopicView.asp?id=3624566
你误会了,其实我现在最急需解决的问题是如何解决NHibernate的one-to-many如何实现的问题.现在我在实现时总是报这个错误:
NHibernate.ADOException: problem in find ---> NHibernate.PropertyAccessException: The type NHibernate.Collection.Bag can not be assigned to a property of type System.Collections.IDictionary setter of Data1.User.Purchase ---> System.ArgumentException: 对象类型无法转换为目标类型。
>> The type NHibernate.Collection.Bag can not be assigned to a property of type
>> System.Collections.IDictionary setter of Data1.User.Purchase --->
>> System.ArgumentException: 对象类型无法转换为目标类型Bag -> 对应IList类型
现在有一个问题,我保在user.hbm.xml中添加了one-to-many关系
<bag name="Purchase" cascade="all" inverse="true">
<key column="Name"/>
<one-to-many class="Data1.Purchase, Data1" />
</bag> 在user.cs中添加了:
private IList _Purchase;
public IList Purchase
{
get
{
return _Purchase;
}
set
{
_Purchase = value;
}
}在purchase中没有做任何修改,但出现如下错误:我试将purchase绑定到Datagrid中,但发现其列顺序是不定的,这是为什么?
然后试将user绑定到Datagrid中时,就是示:
NHibernate.ADOException: Could not execute query ---> System.Data.SqlClient.SqlException: 将 nvarchar 值 'aaaaaaaa ' 转换为数据类型为 int 的列时发生语法错误。我估计这个错误是因为其Name列与ID列混淆所产生的,但是我不知道如何让DataGrid按规定的顺序显示各列......
不好意思,我真的不明白了,所在数据层实行IList到DataTable的转换,但仍然出现问题啊...NHibernate.ADOException: Could not execute query ---> System.Data.SqlClient.SqlException: 将 nvarchar 值 'aaaaaaaa ' 转换为数据类型为 int 的列时发生语法错误。
public DataTable GetUsers()
{
IList users = null;
ITransaction tx = null;
DataTable dr=new DataTable() ;
dr.Columns.Add ("ID");
dr.Columns.Add ("UserName");
dr.Columns.Add ("Password");
dr.Columns.Add ("EmailAddress");
dr.Columns.Add ("LastLogon");
dr.Columns.Add ("Purchase1");
dr.Columns.Add ("Purchase2");
try
{
if(!session.IsConnected)
{
session.Reconnect();
} tx = session.BeginTransaction();
users=c.List ();
//通过ISession的IQuery接口来操作
IQuery q=session.CreateQuery ("from User u where u.ID > :id1 and u.UserName=:name1");
q.SetParameter ("id1",112);
q.SetParameter ("name1","teny"); users=q.List ();
session.Disconnect(); foreach(User userItem in users)
{
foreach (Purchase purchaseItem in userItem.Purchase )
{
dr.Rows.Add( new object[]{ userItem.ID ,userItem.UserName,userItem.Password ,userItem.EmailAddress ,userItem.LastLogon,purchaseItem.Purchase1 ,purchaseItem.Purchase2 });
} }
}
到底是怎么回事,请问我在User.cs中要对purchase对象做什么处理吗????