我有三个表:
student_t:id name
student_course_t:student_id,course_id
course_t:id name
我想实现这样的删除:比如说student_course_t中有数据1 1,我想把它删除……也就是删除多对多关系中中间表的单条数据的sql语句……
student_t:id name
student_course_t:student_id,course_id
course_t:id name
我想实现这样的删除:比如说student_course_t中有数据1 1,我想把它删除……也就是删除多对多关系中中间表的单条数据的sql语句……
delete from student_course_t where student_id='1' and course_id='1'
如果是删除student_t和这个表course_t中的数据先要删除student_course_t 对应的数据因为存在约束条件
先删除主键表,再删除外键表至于hql语句,网上很多。照着写就是了
corresponds to your MySQL server version for the right syntax to use near 'wher
exists (select courseid from course_t c where sc.course_id = c.courseid an' at
line 1
_t c where sc.course_id = c.courseid and c.courseid = 1 );
这是执行的语句
或者直接用createSqlQuery()执行sql不就行啦
final String sql = "delete from Course c where student_id = "+studentid+" and course_id = "+courseid;
return (Boolean) this.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
int index = session.createSQLQuery(sql).executeUpdate();
session.close();
System.out.println(index);
return (index == 1);
}
});
}