2015-06-06 150 views
6

所以這是這種情況..我正在創建一個使用struts2和hibernate的web應用程序。我正在創建一個用戶可以更改密碼的更改密碼頁面。但我得到錯誤爲「org.hibernate.exception.ConstraintViolationException: could not execute statement」..下面是我的代碼和堆棧跟蹤。org.hibernate.exception.ConstraintViolationException:無法執行語句

// 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類(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) 

我是一個新手,Struts和休眠。所以我請求你所有的幫助..我可以ñ提供更多的信息,如果需要..我嘗試了絕大多數的解決方案,但沒有一個幫助

回答

3

錯誤很明顯,密碼(舊的或新的)未在您的操作POJO中設置。所以你得到一個NULL似乎不能插入你的數據庫(對於你的數據庫列非空約束)。

密碼標籤的名稱與您的POJO的名稱不匹配,這允許參數攔截器和模型驅動的攔截器填充您的POJO。(不然怎麼支柱可以GUSS哪個字段必須放在哪個屬性)

因此改變你的標籤,(這樣做對其他標籤太):

<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

請同時參閱Does the ModelDriven interface poses a security explot in struts2?

+0

謝謝你的解決方案。您的回答更新了我的數據庫,但我又收到了glassfish錯誤 '嚴重:4995843 [http-listener-1(2)] ERROR org.apache.struts2.dispatcher.Dispatcher - 處理請求期間發生異常:DML操作不支持[UPDATE example.Pass set newpwd =:password WHERE empid =:id]' –

+0

這是一個hibernate錯誤,不是struts問題。我建議你問一個新的問題。據我記得,當你嘗試選擇更新查詢時會發生這種情況。 _May be_你應該爲_uniqueResult_調用創建一個新的查詢 –

+0

順便說一句,你不應該更新你的數據庫,然後選擇重複,你應該首先在你的數據庫上做出選擇,如果它是唯一的更新 –

相關問題