删除的方法
public boolean deleteMusic(final String hql,final int id) {
try {
this.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session){
SQLQuery sq=session.createSQLQuery(hql);
sq.setInteger(0,id);
return sq.executeUpdate();
}
});
bool = true;
} catch (Exception e) {
bool = false;
System.out.println("MusicDaoImpl......deleteMusic(String hql, int id)"+e);
}
return bool;
}报的错误
Hibernate: delete from TMusic t where t.id = ?
2010-01-13 14:49:02,125 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 1064, SQLState: 42000
2010-01-13 14:49:02,125 ERROR [org.hibernate.util.JDBCExceptionReporter] - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't where t.id = 11' at line 1
MusicDaoImpl......deleteMusic(String hql, int id)org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute native bulk manipulation query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query用的是mysql数据库
public boolean deleteMusic(final String hql,final int id) {
try {
this.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session){
SQLQuery sq=session.createSQLQuery(hql);
sq.setInteger(0,id);
return sq.executeUpdate();
}
});
bool = true;
} catch (Exception e) {
bool = false;
System.out.println("MusicDaoImpl......deleteMusic(String hql, int id)"+e);
}
return bool;
}报的错误
Hibernate: delete from TMusic t where t.id = ?
2010-01-13 14:49:02,125 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 1064, SQLState: 42000
2010-01-13 14:49:02,125 ERROR [org.hibernate.util.JDBCExceptionReporter] - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't where t.id = 11' at line 1
MusicDaoImpl......deleteMusic(String hql, int id)org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute native bulk manipulation query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query用的是mysql数据库
MySQL version 貌似有问题!
你执行的是delete操作怎么创建一个查询语句类
this.hql = "delete from TMusic t where t.id = ?";
this.bool = this.musicDao.deleteMusic(this.hql,id);
SQLQuery sq=session.createSQLQuery(hql);
看错了要改成
Query sq=session.createQuery(hql);
就没有错误了!
如果不改的话就
写成sql语句也不会有问题
所以你的并不是hql,而是sql;
this.hql = "delete from table where id = ?"; 注意问号不要写出中文的了。
如果用对象导航的hql就可以这样:
hql = "delete from Table t where t.id = ?";
session.createQuery(hql);