Hibernate使用和删除HQL [英] Hibernate HQL delete with and
本文介绍了Hibernate使用和删除HQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public boolean deleteVote(Login user,int pid){
Session session = getSession();
尝试{
String hql =从投票中删除,其中uid =:uid和pid =:pid;
Query query = session.createQuery(hql);
System.out.println(user.getUid()+and pid:+ pid);
query.setString(uid,user.getUid());
query.setInteger(pid,pid);
System.out.println(query.executeUpdate());
} catch(例外e){
Outprint:
uid:123和pid:1
Hibernate:从uid =?和pid =?
1
当我直接在SQL中尝试时,SQL语法正在工作。直接SQL语法:
从投票中删除where uid ='123'AND pid = 1
映射:
< class name =package.model.Votetable =votes>
< id name =vidcolumn =vid>
< generator class =increment/>
< / id>
< property name =pidcolumn =pid/>
< property name =uidcolumn =uid/>
< property name =tidcolumn =tid/>
< property name =votescolumn =votes/>
< / class>
表:
< code $ CREATE TABLE IF NOT EXISTS`votes`(
`vid` int(11)NOT NULL
`pid` int(11)NOT NULL,
`uid` varchar 20)NOT NULL,
`tid` int(11)NOT NULL,
`votes` int(11)NOT NULL DEFAULT'1',
PRIMARY KEY(`vid`),
KEY'pcid'(`pid`,`uid`),
KEY`uid`(`uid`),
KEY`tid`(`tid`)
)发动机= InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 10;
ALTER TABLE`votes`
ADD CONSTRAINT`votes_ibfk_3` FOREIGN KEY(`pid`)REFERENCES`poll`(`pid`)ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT`votes_ibfk_4` FOREIGN KEY(`uid`)REFERENCES`user`(`uid`)ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT`votes_ibfk_5` FOREIGN KEY(`tid`)REFERENCES`teams`(`tid `)ON DELETE CASCADE ON UPDATE CASCADE;
INSERT INTO`votes`(`vid`,`pid`,`uid`,`tid`,`votes`)VALUES
(20,1,'123',1, 1);
我觉得这很容易,因为目前为止我的一切看起来都不错。
任何帮助表示赞赏。
解决方案您需要开始并提交一个事务。
Transaction transaction = session.beginTransaction( );
尝试{
//您的代码
字符串hql =从投票中删除,其中uid =:uid和pid =:pid;
Query query = session.createQuery(hql);
System.out.println(user.getUid()+and pid:+ pid);
query.setString(uid,user.getUid());
query.setInteger(pid,pid);
System.out.println(query.executeUpdate());
//您的代码结束
transaction.commit();
} catch(Throwable t){
transaction.rollback();
抛出t;
}
您也可能需要在更改前关闭会话在数据库中可见。
Hibernate doesn't delete my row:
public boolean deleteVote(Login user, int pid){ Session session = getSession(); try{ String hql = "delete from Vote where uid= :uid AND pid= :pid"; Query query = session.createQuery(hql); System.out.println(user.getUid() + " and pid: " + pid); query.setString("uid", user.getUid()); query.setInteger("pid", pid); System.out.println(query.executeUpdate()); }catch(Exception e){
Outprint:
uid: 123 and pid: 1 Hibernate: delete from votes where uid=? and pid=? 1
The SQL Syntax is working when I'm trying directly in SQL. Direct SQL Syntax:
delete from votes where uid= '123' AND pid= 1
Mapping:
<class name="package.model.Vote" table="votes"> <id name="vid" column="vid" > <generator class="increment"/> </id> <property name="pid" column="pid" /> <property name="uid" column="uid" /> <property name="tid" column="tid" /> <property name="votes" column="votes" /> </class>
Table:
CREATE TABLE IF NOT EXISTS `votes` ( `vid` int(11) NOT NULL `pid` int(11) NOT NULL, `uid` varchar(20) NOT NULL, `tid` int(11) NOT NULL, `votes` int(11) NOT NULL DEFAULT '1', PRIMARY KEY (`vid`), KEY `pcid` (`pid`,`uid`), KEY `uid` (`uid`), KEY `tid` (`tid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ; ALTER TABLE `votes` ADD CONSTRAINT `votes_ibfk_3` FOREIGN KEY (`pid`) REFERENCES `poll` (`pid`) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT `votes_ibfk_4` FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT `votes_ibfk_5` FOREIGN KEY (`tid`) REFERENCES `teams` (`tid`) ON DELETE CASCADE ON UPDATE CASCADE; INSERT INTO `votes` (`vid`, `pid`, `uid`, `tid`, `votes`) VALUES (20, 1, '123', 1, 1);
I guess it's something pretty easy because everything looks okay for me so far. I got no error or anything else, just that no delete is happening.
Any help is appreciated.
解决方案You need to begin and commit a transaction.
Transaction transaction = session.beginTransaction(); try { // your code String hql = "delete from Vote where uid= :uid AND pid= :pid"; Query query = session.createQuery(hql); System.out.println(user.getUid() + " and pid: " + pid); query.setString("uid", user.getUid()); query.setInteger("pid", pid); System.out.println(query.executeUpdate()); // your code end transaction.commit(); } catch (Throwable t) { transaction.rollback(); throw t; }
It is also possible that you need to close the session before the changes will be visible in the database.
这篇关于Hibernate使用和删除HQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文