web-dev-qa-db-ja.com

org.hibernate.exception.ConstraintViolationException:ステートメントを実行できませんでした

つまり、これが当てはまります。struts2とhibernateを使用してWebアプリケーションを作成しています。ユーザーが自分のパスワードを変更できるパスワード変更ページを作成しています。しかし、「org.hibernate.exception.ConstraintViolationException: could not execute statement "..以下は私のコードとスタックトレースです。

// JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>H2H: Change Password</title>
    </head>
    <body>
            <s:form action="changep">               
            <s:textfield name="empid" label="Emp ID" />
            <s:textfield name="uname" label="Username"/>
            <s:password name="opwd" label="Old Password"/>
            <s:password name="npwd" label="New Password"/>
            <s:password name="cpwd" label="Confirm Password"/>
            <s:submit name="Change Password" align="center"/>
        </s:form>
    </body>
</html>

//hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.Microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://192.168.100.25:1433;databaseName=db_h2h</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password">123</property>
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping resource="hibernate.hbm.xml" class="example.Login"/>
    <mapping resource="pass.hbm.xml" class="example.Pass"/>    
  </session-factory>
</hibernate-configuration>

//pass.hbm.xml

<?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">
<hibernate-mapping>
    <class dynamic-insert="false" dynamic-update="false" mutable="true" 
           name="example.Pass" optimistic-lock="version" polymorphism="implicit" 
           select-before-update="false" table="masters.tbl_users">
        <id name="empid" type="int">
            <column name="empid" />
            <generator class="assigned" />
        </id>
        <property name="empname" type="Java.lang.String">
            <column name="empname" />
        </property>                     
        <property name="newpwd" type="Java.lang.String">
            <column name="pass" />
        </property>
    </class>
</hibernate-mapping>

//struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.Apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="subin" namespace="" extends="struts-default">

        <action name="login" class="example.ShowLoginAction">
            <result name="success">/index.jsp</result>
        </action>

        <action name="pass" class="example.ShowPassAction">
            <result name="success">/changepass.jsp</result>
        </action>

        <action name="dashboard" class="example.ShowDashboardAction">
            <result name="success">/dashboard.jsp</result>
        </action>

        <action name="verify" class="example.LoginAction">
            <result name="success">/dash.jsp</result>
            <result name="fail">/fail.jsp</result>
        </action>

        <action name="changep" class="example.PassAction">
            <result name="success">/dash.jsp</result>
            <result name="fail">/fail.jsp</result>
            <result name="input">/fail.jsp</result>
        </action>
    </package>

</struts>

// POJOクラス(Pass.Java)

package example;

public class Pass {
    private int empid;
    private String empname;
    private String oldpwd;
    private String newpwd;
    private String cnfpwd;

    public int getEmpid() {
        return empid;
    }

    public void setEmpid(int empid) {
        this.empid = empid;
    }

    public String getEmpname() {
        return empname;
    }

    public void setEmpname(String empname) {
        this.empname = empname;
    }

    public String getOldpwd() {
        return oldpwd;
    }

    public void setOldpwd(String oldpwd) {
        this.oldpwd = oldpwd;
    }

    public String getNewpwd() {
        return newpwd;
    }

    public void setNewpwd(String newpwd) {
        this.newpwd = newpwd;
    }

    public String getCnfpwd() {
        return cnfpwd;
    }

    public void setCnfpwd(String cnfpwd) {
        this.cnfpwd = cnfpwd;
    }

}

// ActionSupport Class(PassAction.Java)

package example;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class PassAction extends ActionSupport implements ModelDriven<Pass> {

    Pass pass = new Pass();

    public PassAction() {
    }

    public String execute() throws Exception {
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction t = session.beginTransaction();
        System.out.println("transx " + t);

        String hql = "UPDATE Pass set newpwd = :password WHERE empid= :id";
        Query query = session.createQuery(hql);        
        query.setParameter("password", pass.getNewpwd());
        query.setParameter("id", pass.getEmpid());
        int res=query.executeUpdate();

        System.out.println("Rows Updated: "+res);

        query.setMaxResults(1);
        t.commit();




        Pass p = (Pass) query.uniqueResult();
        if (p == null) {
            return "fail";
        }
        return "success";

    }

    public Pass getModel() {
        return pass;
    }

}

//ShowPassAction.Java

package example;

import com.opensymphony.xwork2.ActionSupport;

public class ShowPassAction extends ActionSupport {

    public ShowPassAction() {
    }

    public String execute() throws Exception {
        return "success";
    }

}

//スタックトレース

ERROR:   Cannot insert the value NULL into column 'Pass', table 'DB_H2H.Masters.tbl_Users'; column does not allow nulls. UPDATE fails.
Severe:   187355 [http-listener-1(5)] ERROR org.Apache.struts2.dispatcher.Dispatcher - Exception occurred during processing request: could not execute statement
Severe:   org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.Java:129)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.Java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.Java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.Java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.Java:190)
    at org.hibernate.hql.internal.ast.exec.BasicExecutor.doExecute(BasicExecutor.Java:109)
    at org.hibernate.hql.internal.ast.exec.BasicExecutor.execute(BasicExecutor.Java:78)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.Java:445)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.Java:347)
    at org.hibernate.internal.SessionImpl.executeUpdate(SessionImpl.Java:1282)
    at org.hibernate.internal.QueryImpl.executeUpdate(QueryImpl.Java:118)
    at example.PassAction.execute(PassAction.Java:37)
    at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62)
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
    at Java.lang.reflect.Method.invoke(Method.Java:497)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.Java:450)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.Java:289)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:252)
    at org.Apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.Java:256)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.Java:167)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.Java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.Java:265)
    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:246)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.Java:138)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.Java:239)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.Java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.Java:239)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.Java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.Java:191)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at org.Apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.Java:73)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at org.Apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.Java:91)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at org.Apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.Java:252)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.Java:100)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.Java:141)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.Java:145)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.Java:171)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.Java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.Java:161)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at org.Apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.Java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.Java:193)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.Java:189)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.Java:246)
    at org.Apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.Java:54)
    at org.Apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.Java:563)
    at org.Apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.Java:434)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:256)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:214)
    at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:316)
    at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:160)
    at org.Apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.Java:734)
    at org.Apache.catalina.core.StandardPipeline.invoke(StandardPipeline.Java:673)
    at com.Sun.enterprise.web.WebPipeline.invoke(WebPipeline.Java:99)
    at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:174)
    at org.Apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.Java:415)
    at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:282)
    at com.Sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.Java:459)
    at com.Sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.Java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.Java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.Java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.Java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.Java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.Java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.Java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.Java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.Java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.Java:77)
    at org.glassfish.grizzly.nio.
Severe:   transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.Java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.Java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.Java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.Java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.Java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.Java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.Java:545)
    at Java.lang.Thread.run(Thread.Java:745)
Caused by: com.Microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'Pass', table 'DB_H2H.Masters.tbl_Users'; column does not allow nulls. UPDATE fails.
    at com.Microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.Java:196)
    at com.Microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.Java:1454)
    at com.Microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.Java:388)
    at com.Microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.Java:338)
    at com.Microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.Java:4026)
    at com.Microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.Java:1416)
    at com.Microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.Java:185)
    at com.Microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.Java:160)
    at com.Microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.Java:306)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.Java:187)

私はストラットと休止状態の初心者です..だから私はあなたのすべての助けを求めます..必要に応じてより多くの情報を与えることができます..私はSOで与えられた解決策のほとんどを試しましたが、どれも助けませんでした

6

エラーは明らかです。パスワード(古いまたは新しい)がアクションPOJOに設定されていません。したがって、[〜#〜] null [〜#〜]を取得していますが、これはDBに挿入できないようです(null以外の制約の場合)あなたのDB列)。

パスワードタグの名前がPOJOの名前と一致しません。これにより、パラメーターインターセプターとモデル駆動型インターセプターがPOJOを埋めることができます。 (そうでなければ、ストラットがどのフィールドをどのプロパティに配置する必要があるかを突くことができます)

したがって、タグを次のように変更します(他のタグにも変更します)。

 <s:password name="oldpwd" label="Old Password"/>
 <s:password name="newpwd" label="New Password"/>

以下を参照してください。

https://struts.Apache.org/docs/parameters-interceptor.html

https://struts.Apache.org/docs/model-driven.html

また、参照してください ModelDrivenインターフェイスはstruts2でセキュリティエクスプロットを引き起こしますか?

3
Alireza Fattahi