急啊!!Nhibernate报错:dbo.UserInfo is not mapped [SELECT dbo.UserInfo.U_Name, dbo.UserInfo.U_NickName, dbo.UserInfo.U_LoginPas, dbo.UserTypeInfo.T_Name FROM dbo.UserInfo CROSS JOIN dbo.UserTypeInfo]
本人代码如下:一般网上都是 大小写不认真导致的这种问题,但是我这不是,查了好久。各位帮忙解决下,多谢!
<!--用户类型实体类-->
public class UserTypeInfo
{
public virtual int T_Id { get; set; }
public virtual string T_Name { get; set; }
public virtual ISet<UserInfo> UserInfo { get; set;}
}
<!--用户类型实体类Mapp文件-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model.Entities"> <class name="Model.Entities.UserTypeInfo,Model" table="UserTypeInfo">
<!-- 主键,generator为自增长--> <id name="T_Id" column="T_Id" type="Int32" unsaved-value="0"> <generator class="native"></generator> </id> <!--各个字段 -->
<property name="T_Name" column="T_Name" type="string" length="50" not-null="false"></property> <!--一个UserTypeInfo对应多个UserInfo-->
<set name="UserInfo" table="UserInfo" generic="true" inverse="true"> <key column="UserTypeInfo" foreign-key="FK_UserTypeInfoUserInfo"/> <one-to-many class="Model.Entities.UserInfo,Model"/> </set> </class></hibernate-mapping>
<!--用户信息实体类--> public class UserInfo
{
//U_Id, U_Name, U_NickName, U_LoginPas, UserTypeInfo
public virtual int U_Id { get; set; }
public virtual string U_Name { get; set; }
public virtual string U_NickName { get; set; }
public virtual string U_LoginPas { get; set; }
//public virtual Int32 UserTypeInfo { get; set;}
public virtual UserTypeInfo UserTypeInfo { get; set; }
}
<!--用户信息实体类Mapp文件--><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model.Entities"> <class name="Model.Entities.UserInfo,Model" table="UserInfo"> <id name="U_Id" column="U_Id" type="Int32" unsaved-value="0"> <generator class="native"/> </id> <property name="U_Name" column ="U_Name" type="string" not-null="true"></property>
<property name="U_NickName" column="U_NickName" type="string" not-null="true"></property>
<property name="U_LoginPas" column="U_LoginPas" type="string" not-null="true"></property>
<!--<property name="UserTypeInfo" column="UserTypeInfo" type="Int32" not-null="false"></property>-->
<!--多个UserInfo属于一个UserTypeInfo-->
<many-to-one name="UserTypeInfo" column="UserTypeInfo" not-null="true" class="Model.Entities.UserTypeInfo,Model"
foreign-key="FK_UserTypeInfoUserInfo"/>
</class></hibernate-mapping>
<!--业务逻辑层代码(页面调用业务逻辑层的方法就不写出来了)-->
public static class UserInfoService
{
private static readonly IUserInfo ul = DataAccess.CreateUser();
public static IList<UserInfo> GetAllUserInfo()
{
return ul.GetAllUserInfoByHql();
} }
<!--数据访问层代码--> public class UserInfoDAL:IUserInfo
{
private EntityControl<UserInfo> entityControl;
public UserInfoDAL()
{
entityControl = new EntityControl<UserInfo>();
}
public IList<UserInfo> GetAllUserInfoByHql()
{
return entityControl.GetEntitiesList("SELECT dbo.UserInfo.U_Name, dbo.UserInfo.U_NickName, dbo.UserInfo.U_LoginPas, dbo.UserTypeInfo.T_Name FROM dbo.UserInfo CROSS JOIN dbo.UserTypeInfo");
}
}
封装的公共的方法:
public IList<T> GetEntitiesList(string Hql)
{
if (!session.IsConnected)
{
session.Reconnect();
}
using (ITransaction transaction = session.BeginTransaction())
{
IList<T> entities = null;
try
{
entities = session.CreateQuery(Hql).List<T>();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw ex;
}
finally
{
session.Disconnect();
}
return entities;
}
}
本人代码如下:一般网上都是 大小写不认真导致的这种问题,但是我这不是,查了好久。各位帮忙解决下,多谢!
<!--用户类型实体类-->
public class UserTypeInfo
{
public virtual int T_Id { get; set; }
public virtual string T_Name { get; set; }
public virtual ISet<UserInfo> UserInfo { get; set;}
}
<!--用户类型实体类Mapp文件-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model.Entities"> <class name="Model.Entities.UserTypeInfo,Model" table="UserTypeInfo">
<!-- 主键,generator为自增长--> <id name="T_Id" column="T_Id" type="Int32" unsaved-value="0"> <generator class="native"></generator> </id> <!--各个字段 -->
<property name="T_Name" column="T_Name" type="string" length="50" not-null="false"></property> <!--一个UserTypeInfo对应多个UserInfo-->
<set name="UserInfo" table="UserInfo" generic="true" inverse="true"> <key column="UserTypeInfo" foreign-key="FK_UserTypeInfoUserInfo"/> <one-to-many class="Model.Entities.UserInfo,Model"/> </set> </class></hibernate-mapping>
<!--用户信息实体类--> public class UserInfo
{
//U_Id, U_Name, U_NickName, U_LoginPas, UserTypeInfo
public virtual int U_Id { get; set; }
public virtual string U_Name { get; set; }
public virtual string U_NickName { get; set; }
public virtual string U_LoginPas { get; set; }
//public virtual Int32 UserTypeInfo { get; set;}
public virtual UserTypeInfo UserTypeInfo { get; set; }
}
<!--用户信息实体类Mapp文件--><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model.Entities"> <class name="Model.Entities.UserInfo,Model" table="UserInfo"> <id name="U_Id" column="U_Id" type="Int32" unsaved-value="0"> <generator class="native"/> </id> <property name="U_Name" column ="U_Name" type="string" not-null="true"></property>
<property name="U_NickName" column="U_NickName" type="string" not-null="true"></property>
<property name="U_LoginPas" column="U_LoginPas" type="string" not-null="true"></property>
<!--<property name="UserTypeInfo" column="UserTypeInfo" type="Int32" not-null="false"></property>-->
<!--多个UserInfo属于一个UserTypeInfo-->
<many-to-one name="UserTypeInfo" column="UserTypeInfo" not-null="true" class="Model.Entities.UserTypeInfo,Model"
foreign-key="FK_UserTypeInfoUserInfo"/>
</class></hibernate-mapping>
<!--业务逻辑层代码(页面调用业务逻辑层的方法就不写出来了)-->
public static class UserInfoService
{
private static readonly IUserInfo ul = DataAccess.CreateUser();
public static IList<UserInfo> GetAllUserInfo()
{
return ul.GetAllUserInfoByHql();
} }
<!--数据访问层代码--> public class UserInfoDAL:IUserInfo
{
private EntityControl<UserInfo> entityControl;
public UserInfoDAL()
{
entityControl = new EntityControl<UserInfo>();
}
public IList<UserInfo> GetAllUserInfoByHql()
{
return entityControl.GetEntitiesList("SELECT dbo.UserInfo.U_Name, dbo.UserInfo.U_NickName, dbo.UserInfo.U_LoginPas, dbo.UserTypeInfo.T_Name FROM dbo.UserInfo CROSS JOIN dbo.UserTypeInfo");
}
}
封装的公共的方法:
public IList<T> GetEntitiesList(string Hql)
{
if (!session.IsConnected)
{
session.Reconnect();
}
using (ITransaction transaction = session.BeginTransaction())
{
IList<T> entities = null;
try
{
entities = session.CreateQuery(Hql).List<T>();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw ex;
}
finally
{
session.Disconnect();
}
return entities;
}
}
去jave hibernate问
感谢lchy110 我把文件删除了在增加就报错了!找了一天也没能找到答案! 具体操作是 选中文件--〉属性-- 在文件属性里有个生成操作 设置这个属性的值
比如说,是不是映射名称中不能有下划线?例如“NP_AssessmentPoint”这个表映射的时候。