NHibernate多表查询问题。 在线等,急,急,急。。。
我现在有一张书籍表,和一张书籍类别表,主外键关系。
我现在想查所有数据,把类别也显示出来。
书籍编号 书籍名称 书籍类别
110 天龙八部 武侠
111 逆龙道 都市
BookInfo表
id,bookISBN,bookName,bid这个是 类别表的id.
BookType表
id,type
用NHibernate HQL语句。
- C# code
IList list = null; ArrayList arr = new ArrayList(); try { ISession session = SessionFactory.OpenSession(_AssemblyName); //string sql = "From LongFiction as a left join Fetch a.LongTypeInfo"; string sql = "From LongFiction as a,LongTypeInfo as b where a.LongTypeInfo =b.id"; list = session.CreateQuery(sql).List(); IEnumerator ie = list.GetEnumerator(); while (ie.MoveNext()) { Object[] objs = (Object[])ie.Current; LongFiction l = (LongFiction)objs[0]; LongTypeInfo info = (LongTypeInfo)objs[1]; UserCollection collection = new UserCollection(l, info); arr.Add(collection); } } catch (Exception ex) { throw; } return arr;目前HQL语句不对。 list count是0 一条数据都没查出来。。。
[解决办法]
多表查询
[解决办法]
设置好BookInfo与BookType的多对一关系
查询出bookinfo即可,实体类bookinfo会有一个属性及对应的booktype