org.hibernate.exception.SQLGrammarException: could not execute statement
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'userName' in 'field list'
改前:
Hibernate: insert users(user_name,PASSWORD,email,flag_id) values (userName,password,email,1)
改后:
Hibernate: insert users(user_name,PASSWORD,email,flag_id) values ('userName','password','email',1)
在value中 多了单引号,红色标识的会报错。
下方两个例子分别是改前和改后
在('"+user.getUserName()+中多了‘ 号
@Override
public int save(User user) {
//INSERT users(user_name,PASSWORD,email,flag_id) VALUE('1','1','1',1);
String sql = "insert users(user_name,PASSWORD,email,flag_id) values ("+user.getUserName()+","+user.getPassword()+","+user.getEmail()+",1)";
int PK = sessionFactory.getCurrentSession().createSQLQuery(sql).executeUpdate();
return PK;
}
@Override
public int save(User user) {
//INSERT users(user_name,PASSWORD,email,flag_id) VALUE('1','1','1',1);
String sql = "insert users(user_name,PASSWORD,email,flag_id) values ('"+user.getUserName()+"','"+user.getPassword()+"','"+user.getEmail()+"',1)";
int PK = sessionFactory.getCurrentSession().createSQLQuery(sql).executeUpdate();
return PK;
}