a839398 2018-01-12 09:27 采纳率: 66.7%
浏览 1135
已采纳

NHibernate没法把对象存进数据库的表,该表有一个外键

我是一个初学者。
NHibernate没法把对象存进数据库的role表,也取不出数据,该表有一个外键关联到user表。我认为是这个表外键导致存不进去的。因为我另一部分程序顺利地访问了我的数据库的user表。以下是存进数据库的程序代码和报的错
public class RoleManager
{
public void AddRole(Role role)
{
using (var session = NHibernateHelper.getSession())
{
using (var transaction = session.BeginTransaction())
{
session.Save(role);//报错
transaction.Commit();
}
}

}
class RoleMap:ClassMap
{
public RoleMap()
{
Id(x => x.Id).Column("Id");//主键
Map(x => x.Name).Column("name");
Map(x => x.Level).Column("level");
Map(x => x.IsMan).Column("isman");
Map(x => x.RolePrefabName).Column("roleprefabname");
References(x => x.User).Column("userid");//外键
Table("role");
}
}
public class Role
{
public virtual int Id { set; get; }//主键
public virtual string Name{ set; get; }
public virtual int Level { set; get; }
public virtual bool IsMan { set; get; }
public virtual string RolePrefabName { set; get; }
public virtual Users User { set; get; }//外键
}
报错信息
2018-01-11 17:35:27,710 [10] INFO BaiLaoDaServer.BaiLaoDaPeer [(null)] - handler from operation code is isExit:1
2018-01-11 17:35:46,440 [10] INFO BaiLaoDaServer.BaiLaoDaPeer [(null)] - handler from operation code is isExit:0
2018-01-11 17:35:46,450 [10] INFO BaiLaoDaServer.Handlers.LoginHandler [(null)] - BaiLaoDaPeer.user=11ytbai
2018-01-11 17:35:46,480 [6] INFO BaiLaoDaServer.BaiLaoDaPeer [(null)] - handler from operation code is isExit:3
2018-01-11 17:35:46,480 [6] ERROR Photon.SocketServer.PeerBase [(null)] - NHibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave. Type: BaiLaoDaCommon.Model.Users, Entity: BaiLaoDaCommon.Model.Users
在 NHibernate.Engine.ForeignKeys.GetEntityIdentifierIfNotUnsaved(String entityName, Object entity, ISessionImplementor session)
在 NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand cmd, Object value, Int32 index, ISessionImplementor session)
在 NHibernate.Param.CriteriaNamedParameterSpecification.Bind(IDbCommand command, IList1 multiSqlQueryParametersList, Int32 singleSqlParametersOffset, IList1 sqlQueryParametersList, QueryParameters queryParameters, ISessionImplementor session)
在 NHibernate.Param.CriteriaNamedParameterSpecification.Bind(IDbCommand command, IList`1 sqlQueryParametersList, QueryParameters queryParameters, ISessionImplementor session)
在 NHibernate.SqlCommand.SqlCommandImpl.Bind(IDbCommand command, ISessionImplementor session)
在 NHibernate.Loader.Loader.PrepareQueryCommand(QueryParameters queryParameters, Boolean scroll, ISessionImplementor session)
在 NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer)
在 NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer)
在 NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer)
在 NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
在 NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)
在 NHibernate.Impl.CriteriaImpl.List(IList results)
在 NHibernate.Impl.CriteriaImpl.ListT
在 BaiLaoDaServer.DB.Managers.RoleManager.GetRolesByUser(Users user) 位置 D:\PhontonServerWorkspace\BaiLaoDaServer\BaiLaoDaServer\DB\Managers\RoleManager.cs:行号 23
在 BaiLaoDaServer.Handlers.RoleHandler.OnHandlerMessage(OperationRequest request, BaiLaoDaPeer peer) 位置 D:\PhontonServerWorkspace\BaiLaoDaServer\BaiLaoDaServer\Handlers\RoleHandler.cs:行号 34
在 BaiLaoDaServer.BaiLaoDaPeer.OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) 位置 D:\PhontonServerWorkspace\BaiLaoDaServer\BaiLaoDaServer\BaiLaoDaPeer.cs:行号 39
在 Photon.SocketServer.PeerBase.OnReceive(Byte[] data, SendParameters sendParameters) 位置 h:\svncontent\photon-socketserver-sdk_3.4\src\Photon.SocketServer\PeerBase.cs:行号 963
在 Photon.SocketServer.PeerBase.OnReceiveInternal(Byte[] data, SendParameters sendParameters, Int32 rtt, Int32 rttVariance, Int32 numFailures) 位置 h:\svncontent\photon-socketserver-sdk_3.4\src\Photon.SocketServer\PeerBase.cs:行号 749
2018-01-11 17:36:14,502 [16] INFO BaiLaoDaServer.BaiLaoDaPeer [(null)] - handler from operation code is isExit:3
2018-01-11 17:36:14,506 [16] INFO BaiLaoDaServer.DB.Managers.RoleManager [(null)] - sdfg/1/0/girl_stand/11ytbai
2018-01-11 17:36:14,514 [16] ERROR Photon.SocketServer.PeerBase [(null)] - NHibernate.Exceptions.GenericADOException: could not insert: [BaiLaoDaCommon.Model.Role][SQL: INSERT INTO role (name, level, isman, roleprefabname, userid) VALUES (?, ?, ?, ?, ?)] ---> MySql.Data.MySqlClient.MySqlException: Column 'userid' cannot be null
在 MySql.Data.MySqlClient.MySqlStream.ReadPacket()
在 MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
在 MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
在 MySql.Data.MySqlClient.MySqlDataReader.NextResult()
在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
在 MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
在 NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd)
在 NHibernate.Id.Insert.AbstractSelectingDelegate.PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder)
--- 内部异常堆栈跟踪的结尾 ---
在 NHibernate.Id.Insert.AbstractSelectingDelegate.PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder)
在 NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session)
在 NHibernate.Action.EntityIdentityInsertAction.Execute()
在 NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
在 NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
在 NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
在 NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
在 NHibernate.Impl.SessionImpl.Save(Object obj)
在 BaiLaoDaServer.DB.Managers.RoleManager.AddRole(Role role) 位置 D:\PhontonServerWorkspace\BaiLaoDaServer\BaiLaoDaServer\DB\Managers\RoleManager.cs:行号 35
在 BaiLaoDaServer.Handlers.RoleHandler.OnHandlerMessage(OperationRequest request, BaiLaoDaPeer peer) 位置 D:\PhontonServerWorkspace\BaiLaoDaServer\BaiLaoDaServer\Handlers\RoleHandler.cs:行号 44
在 BaiLaoDaServer.BaiLaoDaPeer.OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) 位置 D:\PhontonServerWorkspace\BaiLaoDaServer\BaiLaoDaServer\BaiLaoDaPeer.cs:行号 39
在 Photon.SocketServer.PeerBase.OnReceive(Byte[] data, SendParameters sendParameters) 位置 h:\svncontent\photon-socketserver-sdk_3.4\src\Photon.SocketServer\PeerBase.cs:行号 963
在 Photon.SocketServer.PeerBase.OnReceiveInternal(Byte[] data, SendParameters sendParameters, Int32 rtt, Int32 rttVariance, Int32 numFailures) 位置 h:\svncontent\photon-socketserver-sdk_3.4\src\Photon.SocketServer\PeerBase.cs:行号 749

  • 写回答

7条回答 默认 最新

  • threenewbee 2018-01-12 10:20
    关注

    session.Save(role);
    这里调试下role的id字段是不是没有赋值?

    MySql.Data.MySqlClient.MySqlException: Column 'userid' cannot be null 提示userid为空。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52