嵌套异常是java.lang.IllegalArgumentException:查询中没有位置参数

时间:2017-01-16 16:55:10

标签: java spring hibernate

我在一个学校项目中工作,我在后端使用Java / Spring / Hibernate工作。在处理数据库请求时,我收到了此错误。我似乎无法解决它。我尝试在1开始参数,但它是相同的错误。 谢谢你的帮助!

  

Etat HTTP 500 - 请求处理失败;嵌套异常是java.lang.IllegalArgumentException:查询中没有位置参数:FROM fr.netrust.model.User WHERE username =? AND密码=?

model.User

@Entity
@Table(name="user")
public class User implements Serializable {
    private static final long serialVersionIUD = 1L;

    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;
    private String username;
    private String password;

        public User() {
            super();
        }

        public User(String username, String password) {
            super();
            this.username = username;
            this.password = password;
        }

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public String getUsername() {
            return this.username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return this.password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

    }

UserDto

package fr.netrust.dto;

public class UserDto {

    private long id;
    private String username;
    private String password;

    public UserDto() {

    }

    public UserDto(long id, String username, String password) {
        super();
        id = this.id;
        username = this.username;
        password = this.password;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

在UserDAOImpl

package fr.netrust.dao.impl;

@Repository
@Transactional
public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    @Transactional(readOnly=true)
    public int isValidUser(String username, String password) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        Query sql = session.createQuery("FROM fr.netrust.model.User WHERE username=? AND password=?");
        sql.setString(0, username);
        sql.setString(1, password);
        User result = (User) sql.uniqueResult();
        tx.commit();
        session.flush();
        session.close();
        return (int) (result==null?0:result.getId());
    }

UserController中

package fr.netrust.web;

@Controller
public class UserController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserDaoAdapter userDaoAdpter;

    @RequestMapping(value = "/log", method = RequestMethod.GET)
    @ResponseBody
    public int log(HttpServletRequest req) throws SQLException {
        return userDao.isValidUser(req.getParameter("username"), req.getParameter("password"));

    } 
}

datasource.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/NeTrust"></property>
        <property name="username" value="descartes" />
        <property name="password" value="descartes" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="fr.netrust.web" />       
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/pages/" p:suffix=".jsp"/>

</beans>

[编辑]

@Override
@Transactional
public int isValidUser(String username, String password) {
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    Query sql = session.createQuery("FROM fr.netrust.model.User U WHERE U.username = :username AND U.password = :password");
    sql.setParameter("username", username);
    sql.setParameter("password", password);
    User result = (User) sql.uniqueResult();
    tx.commit();
    session.flush();
    session.close();
    return (int) (result==null?0:result.getId());
}

1 个答案:

答案 0 :(得分:0)

setString是基于1的索引,如下例所示:https://github.com/jasnow/job-hunter/blob/master/scraper.rb

所以

 string[] excludechar = { "|", "\\", "\"", "'", "/", "[", " ]", ":", "<", " >", "+", "=", ",", ";", "?", "*", " @" };
 var currentgroupname = curItemSiteName;
 for (int i = 0; i < excludechar.Length; i++)
       {
         if (currentgroupname.Contains(excludechar[i]))
             currentgroupname = currentgroupname.Replace(excludechar[i], "");       
       }
 site.RootWeb.SiteGroups.Add(currentgroupname)

应该有效。

编辑:我的坏是它的冬眠。

首先,使用命名参数,例如:http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

使用hibernate,您必须为表使用别名。因此查询必须类似于:

sql.setString(1, username);
sql.setString(2, password);