请懂NHibernate的进!,告诉小弟什么问题 //Record类 namespace Test.NHB { public class Record { public virtual int ID { get; set; } public virtual DateTime Time { get; set; } public virtual Person person { get; set; } public virtual Department department { get; set; } } }
//Record映射 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name ="Test.NHB.Record,Test.NHB" table ="Record" lazy ="true "> <id name ="ID" column ="Record"> <generator class ="native"></generator> </id> <property name ="Time" column ="Time"></property> <many-to-one name ="person" column="PersonID" cascade="all" fetch="join"></many-to-one> </class> </hibernate-mapping> //Person类 namespace Test.NHB { public class Person { public virtual int PersonID { get; set; } public virtual string Name { get; set; } } }
//person映射 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name ="Test.NHB.Person,Test.NHB" table ="Person"> <id name="PersonID"> <generator class ="native"></generator> </id> <property name ="Name" column="[Name]"></property> </class> </hibernate-mapping>
//根据ID可以查询 public IList<Record> GetRecordByID(int id) { //Factory()]; session = Factory(); ICriteria c = session.CreateCriteria<Record>(); c.Add(Expression.Eq("person.PersonID", id)); return c.List <Record >(); } //以下报错,但根据PersonId查询可以,根据姓名不可以 public IList<Record> GetRecordByName(string Name) { session = Factory(); ICriteria c = session.CreateCriteria<Record>(); c.Add(Expression.Eq("person.Name", Name)); return c.List <Record>(); }