如何在hibernate中用条件写更新查询

时间:2012-08-08 07:37:19

标签: java sql hibernate

public String passlink(String companyName, String mailName) {

    Session session = Hibernateutil.getSessionFactory().openSession();

    Transaction transaction = null;

    try {
        transaction = session.beginTransaction();

        String queryString="UPDATE account SET companyname = :companyName WHERE companyname like 'm' AND Email LIKE :mailName";
        Query query=session.createQuery(queryString);

        query.setParameter("companyName", companyName);
        query.setParameter("mailName", mailName);
        int result = query.executeUpdate();

        System.out.println("Rows affected: " + result);

            System.out.println("Successfully updated");
        transaction.commit();

    } catch (Exception e) {
        value = "FAIL";
                   e.printStackTrace();
        //System.out.println("Error--> " + e.getMessage());
    }
    return value;

}

我写更新查询

String queryString =“UPDATE account SET companyname =:companyName WHERE companyname like'm'AND Email LIKE:mailName”;

像这样,但它会出现以下错误。

  

java.lang.IndexOutOfBoundsException:Index:0,Size:0       at java.util.ArrayList.RangeCheck(Unknown Source)       at java.util.ArrayList.get(Unknown Source)       在org.hibernate.hql.ast.HqlSqlWalker.postProcessUpdate(HqlSqlWalker.java:390)       在org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:164)       在org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)       在org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)       在org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)       在org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:425)       在org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:880)       在org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:861)       在org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:89)       在com.njob.dao.AccountDao.passlink(AccountDao.java:213)       在com.njob.action.companylink.doPost(companylink.java:34)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:709)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:802)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)       在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)       在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.processConnection(Http11Protocol.java:731)       在org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)       在org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)       at org.apache.tomcat.util.threads.ThreadPool $ ControlRunnable.run(ThreadPool.java:684)       在java.lang.Thread.run(未知来源)

2 个答案:

答案 0 :(得分:3)

将查询更改为

String queryString="UPDATE account SET companyname = :companyName WHERE companyname like 'm' AND email LIKE :mailName";

并使用

query.setParameter("companyName", companyName);
query.setParameter("mailName", mailName);

查看

答案 1 :(得分:1)

试试这个

 query.setParameter("companyName", companyName);

您使用的参数是companyName