带有Query的org.springframework.orm.hibernate4.HibernateQueryException(Spring mvc)

时间:2015-11-24 15:34:06

标签: java mysql spring hibernate spring-mvc

我有一个查询。 (这是我完整的DAO方法)

@Override
public void addUserRole(UserRole userRole) {
    Transaction tx = sessionFactory.getCurrentSession().beginTransaction();
    String query = "insert into 'user_roles'('role','username') values(%s,%s)"; 
    Session session = this.sessionFactory.getCurrentSession();
    List roleAdd = session.createQuery(String.format(query, "'ROLE_USER'", "'acid'")).list();

    tx.commit();

}

它给了我一个例外

type Exception report message Request processing failed; nested exception is org.springframework.orm.hibernate4.HibernateQueryException: expecting IDENT, found ''user_roles'' near line 1, column 13 

[insert into 'user_roles'('role','username') values('ROLE_USER','acid')]; nested exception is org.hibernate.hql.internal.ast.QuerySyntaxException: expecting IDENT, found ''user_roles'' near line 1, column 13 

[insert into 'user_roles'('role','username') values('ROLE_USER','acid')] description The server encountered an internal error that prevented it from fulfilling this request.

1 个答案:

答案 0 :(得分:0)

以下是您的方法应该是什么样的:

@Override
public void addUserRole(UserRole userRole) {
    Transaction tx = sessionFactory.getCurrentSession().beginTransaction();
    String query = "insert into 'user_roles'('role','username') values(%s,%s)"; 
    Session session = this.sessionFactory.getCurrentSession();
    int resultCount = session.createSQLQuery(String.format(query, "'ROLE_USER'", "'acid'")).executeUpdate();

    tx.commit();
}