您的位置:首页技术文章
文章详情页

java - SSH 根据id字段不能更新数据问题

【字号: 日期:2023-11-08 13:43:41浏览:12作者:猪猪

问题描述

org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.lcy.po.AuthorAdmin.authorUsername; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.lcy.po.AuthorAdmin.authorUsername at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:648) at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at org.springframework.orm.hibernate3.HibernateTemplate.update(HibernateTemplate.java:705) at org.springframework.orm.hibernate3.HibernateTemplate.update(HibernateTemplate.java:701) at com.lcy.dao.author.admin.AuthorAdminDaoImpl.updateAuthorAdmin(AuthorAdminDaoImpl.java:48) at com.lcy.service.author.admin.AuthorAdminServiceImpl.updateAuthorAdmin(AuthorAdminServiceImpl.java:47) at com.lcy.action.author.admin.AuthorAdminAction.updateAuthorAdmin(AuthorAdminAction.java:124) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452) at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254) at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263) at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:619)Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.lcy.po.AuthorAdmin.authorUsername at org.hibernate.engine.Nullability.checkNullability(Nullability.java:95) at org.hibernate.event.def.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:292) at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:151) at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219) at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:49) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028) at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:407) ... 71 more

dao的方法

public void updateAuthorAdmin(AuthorAdmin aa) {try { basedao.getTemplate().update(aa); System.out.println('dao updateAuthorAdmin suc');} catch (Exception e) { System.out.println('dao updateAuthorAdmin error'); e.printStackTrace(); throw new RuntimeException();}@Override public AuthorAdmin getAuthorAdmin(Long id) {AuthorAdmin authorAdmin = basedao.getTemplate().get(AuthorAdmin.class,id);if (authorAdmin == null) { System.out.println('dao getAuthorAdmin没有获得对象');}System.out.println('dao getAuthorAdmin成功获得对象');return authorAdmin; }

action层

public class AuthorAdminAction extends BaseAction implementsModelDriven<AuthorAdmin>{private AuthorAdmin authoradmin = new AuthorAdmin(); @Override public AuthorAdmin getModel() {return authoradmin; }public String getAuthorAdmin() {authoradmin= iauthoradminservice.getAuthorAdmin(authoradmin.getAuthorId());System.out.println('action getAuthorAdmin suc');return 'getSuccess'; }public String updateAuthorAdmin() {iauthoradminservice.updateAuthorAdmin(authoradmin);System.out.println('action updateAuthorAdmin suc');return 'updateSuccess'; }}

JSP页面根据id跳转

<a href='https://www.haobala.com/wenda/${pageContext.request.contextPath}/get_AuthorAdmin?authorId=<s:property value='#obj.authorId'/>'></a>

进入修改表单

<s:form action='update_AuthorAdmin' method='post'><s:hidden name='authorId' value='%{model.authorId}'></s:hidden><s:textfield name='%{model.authorUsername}' label='用户名'></s:textfield><s:textfield name='%{model.authorPwd}' label='密码'></s:textfield><s:textfield name='%{model.authorName}' label='姓名'></s:textfield><s:textfield name='%{model.authorSex}' label='性别'></s:textfield><s:textfield name='%{model.authorBirth}' label='生日'></s:textfield><s:textfield name='%{model.authorRegion}' label='作者区域'></s:textfield><s:textfield name='%{model.authorAddress}' label='通讯地址'></s:textfield><s:textfield name='%{model.authorPost}' label='职称'></s:textfield><s:textfield name='%{model.authorJob}' label='职业'></s:textfield><s:textfield name='%{model.authorCompany}' label='工作单位'></s:textfield><s:textfield name='%{model.authorDegree}' label='学历'></s:textfield><s:textfield name='%{model.authorMajor}' label='专业方向'></s:textfield><s:textfield name='%{model.authorTitle}' label='文章标题'></s:textfield><s:textfield name='%{model.authorMail}' label='E-mail'></s:textfield><s:textfield name='%{model.authorPhone}' label='电话'></s:textfield><s:textfield name='%{model.authorInfo}' label='个人简介'></s:textfield><s:submit value='修改'></s:submit> </s:form>

struct2.xml

<package name='authorAdmin' extends='struts-default'><action name='*_*' method='{1}{2}'> <result name='saveSuccess' type='redirectAction'>list_{2}.action</result> <result name='updateSuccess' type='redirectAction'>list_{2}.action</result> <result name='loginSuccess' type='redirectAction'>list_{2}.action</result> <result name='removeSuccess' type='redirectAction'>list_{2}.action</result> <result name='listSuccess'>/{2}/{1}/{1}Success.jsp</result> <result name='getSuccess'>/{2}/update/update.jsp</result></action> </package>

实体类的字段还有映射

private Long authorId; private String authorUsername; private String authorPwd; private String authorName; private String authorSex; private String authorBirth; private String authorRegion; private String authorAddress; private String authorPost; private String authorJob; private String authorCompany; private String authorDegree; private String authorMajor; private String authorTitle; private String authorMail; private String authorPhone; private String authorInfo; private Timestamp gmtCreate; private Timestamp gmtModified; private Set documentInfos = new HashSet(0);

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-mapping PUBLIC '-//Hibernate/Hibernate Mapping DTD 3.0//EN''http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'><!-- Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping> <class name='com.lcy.po.AuthorAdmin' table='author_admin' catalog='ssh'><id name='authorId' type='java.lang.Long'> <column name='author_id' /> <generator /></id><property name='authorUsername' type='java.lang.String'> <column name='author_username' length='20' not-null='true'><comment>用户名</comment> </column></property><property name='authorPwd' type='java.lang.String'> <column name='author_pwd' length='20' not-null='true'><comment>密码</comment> </column></property><property name='authorName' type='java.lang.String'> <column name='author_name' length='20'><comment>姓名</comment> </column></property><property name='authorSex' type='java.lang.String'> <column name='author_sex' length='2'><comment>性别</comment> </column></property><property name='authorBirth' type='java.lang.String'> <column name='author_birth' length='20'><comment>生日</comment> </column></property><property name='authorRegion' type='java.lang.String'> <column name='author_region' length='50'><comment>作者区域</comment> </column></property><property name='authorAddress' type='java.lang.String'> <column name='author_address' length='100'><comment>通讯地址</comment> </column></property><property name='authorPost' type='java.lang.String'> <column name='author_post' length='50'><comment>职称</comment> </column></property><property name='authorJob' type='java.lang.String'> <column name='author_job' length='50'><comment>职业</comment> </column></property><property name='authorCompany' type='java.lang.String'> <column name='author_company' length='50'><comment>工作单位</comment> </column></property><property name='authorDegree' type='java.lang.String'> <column name='author_degree' length='40'><comment>学历</comment> </column></property><property name='authorMajor' type='java.lang.String'> <column name='author_major' length='50'><comment>专业方向</comment> </column></property><property name='authorTitle' type='java.lang.String'> <column name='author_title' length='20'><comment>文章标题</comment> </column></property><property name='authorMail' type='java.lang.String'> <column name='author_mail' length='100'><comment>E-mail</comment> </column></property><property name='authorPhone' type='java.lang.String'> <column name='author_phone' length='11'><comment>电话</comment> </column></property><property name='authorInfo' type='java.lang.String'> <column name='author_info' length='65535'><comment>个人简介</comment> </column></property><property name='gmtCreate' type='java.sql.Timestamp'> <column name='gmt_create' length='19'><comment>创建时间</comment> </column></property><property name='gmtModified' type='java.sql.Timestamp'> <column name='gmt_modified' length='19'><comment>修改时间</comment> </column></property><set name='documentInfos' inverse='true'> <key><column name='author_id' not-null='true'> <comment>稿件作者</comment></column> </key> <one-to-many /></set> </class></hibernate-mapping>

问题解答

回答1:

先看一下报错,authorUsername是一个non-null属性,有一个非空校验,你提交了个空值,所以报错了。可能导致的原因:

1.表单无输入,或输入错误2.属性名称前后不匹配,传递过程中,赋值失败,造成丢值3.脏数据,读取出来的数据存在问题,更新插入的时候空值失败回答2:

看下basedao的方法吧

标签: java
相关文章: