java - SSH 根据id字段不能更新数据问题
typecho
typecho 2017-06-12 09:24:19
[Java讨论组]
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 implements
        ModelDriven{
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跳转

">

进入修改表单


        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

struct2.xml



        
            list_{2}.action
            list_{2}.action
            list_{2}.action
            list_{2}.action
            /{2}/{1}/{1}Success.jsp
            /{2}/update/update.jsp
        

    

实体类的字段还有映射

   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);




    
        
            
            
        
        
            
                用户名
            
        
        
            
                密码
            
        
        
            
                姓名
            
        
        
            
                性别
            
        
        
            
                生日
            
        
        
            
                作者区域
            
        
        
            
                通讯地址
            
        
        
            
                职称
            
        
        
            
                职业
            
        
        
            
                工作单位
            
        
        
            
                学历
            
        
        
            
                专业方向
            
        
        
            
                文章标题
            
        
        
            
                E-mail
            
        
        
            
                电话
            
        
        
            
                个人简介
            
        
        
            
                创建时间
            
        
        
            
                修改时间
            
        
        
            
                
                    稿件作者
                
            
            
        
    

typecho
typecho

Following the voice in heart.

全部回复(2)
仅有的幸福

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

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

看下basedao的方法吧

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号