问题描述
有两张表,一张userinfo表,一张good表。其中good表中Saler_id列与userinfo表中主键列User_id是主外键的关系。其中在Hibernate的映射文件中的代码如下<many-to-onename="userinfoBySalerId"class="org.epai.web.entity.Userinfo"fetch="select"><columnname="Saler_id"not-null="true"/></many-to-one>
但是我在向good表中插入数据的时候总是在Saler_id这列报错,我需要获得当前登录人的ID再插入到这列中,异常信息如下:org.hibernate.PropertyValueException:not-nullpropertyreferencesanullortransientvalue:org.epai.web.entity.Goods.userinfoBySalerIdatorg.hibernate.engine.Nullability.checkNullability(Nullability.java:72)atorg.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265)atorg.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)atorg.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)atorg.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)atorg.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)atorg.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)atorg.hibernate.impl.SessionImpl.save(SessionImpl.java:518)atorg.hibernate.impl.SessionImpl.save(SessionImpl.java:514)atorg.epai.web.dao.impl.BaseDAO.add(BaseDAO.java:19)atorg.epai.web.dao.impl.GoodsDAO.addgood(GoodsDAO.java:13)atorg.epai.web.service.GoodsService.addgood(GoodsService.java:12)atorg.epai.web.action.AddgoodsAction.execute(AddgoodsAction.java:66)atorg.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)atorg.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)atorg.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)atorg.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:710)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:803)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)atorg.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)atorg.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)atorg.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)atorg.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)atorg.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)atjava.lang.Thread.run(UnknownSource)因为在Hibernate生成的good表中声明的这列是JavacodeprivateUserinfouserinfoBySalerId;
这样的话,没办法把从session中获得的当前登录人的id插入到这列中请问各位这个问题怎么解决呢?
解决方案
解决方案二:
冲session中获取id,跟HIBERNATE配置没什么关系.你登陆成功后session.setAttribute("id",id);用的时候session.getAttribute("id");可以根据id获取Userinfo对象userInfo.然后把userInfo作为属性set到GOOD的Bean中,调用save(goodBean);就可以了.
解决方案三:
你的book关联到了不存在的userinfo,<columnname="Saler_id"not-null="true"/>去掉not-null属性吧如果不想去掉not-null属性的话,你在关联到Saler_id的时候出错了吧,这个貌似是逻辑上的错误噢
解决方案四:
引用1楼wangpeng88888888的回复:
冲session中获取id,跟HIBERNATE配置没什么关系.你登陆成功后session.setAttribute("id",id);用的时候session.getAttribute("id");可以根据id获取Userinfo对象userInfo.然后把userInfo作为属性set到GOOD的Bean中,调用save(goodBean);就可以了.
可是数据库中Saler_id列是long型的,把userinfo类型这样直接插入就会报错啊
解决方案五:
引用2楼wind1373290的回复:
你的book关联到了不存在的userinfo,<columnname="Saler_id"not-null="true"/>去掉not-null属性吧如果不想去掉not-null属性的话,你在关联到Saler_id的时候出错了吧,这个貌似是逻辑上的错误噢
那我应该怎么做呢?不去掉not-null的话我应该怎么改呢?
解决方案六:
h我也想知道问题`
解决方案七:
没得想的,在Action里把用户ID取出来,然后到数据库取UserInfo对象,然后放进去,最后保存,Hibernate就是这样的了.
解决方案八:
报错的原因是非空属性插入了一个空值。如果针对这个错误的话,将not-null="true"改为not-null="false".我看你的应用是一对多的关系。所以应当运用set和many-to-one双向关联。这样保存“一方”对象时,就会自动插入的,我也是刚学习hibernate,共同探讨