Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate HQL delete with and

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.

like image 660
user1671980 Avatar asked Nov 21 '12 08:11

user1671980


People also ask

How to write delete query in HQL?

HQL delete query : HQL delete query is as same as the update. The HQL delete is used to delete the bulk records from the database. If our requirement is to delete a single record, we can go with hibernate delete operation.

How to delete in Hibernate query?

Delete - The 'Delete' issued a SELECT statement because hibernate needs to know if the object exists in the database or not. If the object exists in the database, hibernate considers it as detached and then re-attches it to the session and processes delete lifecycle.

How to perform delete operation in Hibernate?

In Hibernate, an entity can be removed from a database by calling the Session#delete() or Session#remove() . Using these methods, we can remove a transient or persistent object from datastore.


2 Answers

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.

like image 119
Bantak Avatar answered Sep 29 '22 03:09

Bantak


From the Output you provided

uid: 123 and pid: 1
Hibernate: delete from votes where uid=? and pid=?
1

it is clear that query.executeUpdate() is returning 1. The method returns the number of entities updated or deleted. This means that 1 row has been updated or deleted, which is okay.

Try doing a session.flush() to flush the session, or a session.evict() to remove the object from the session.

like image 43
Raul Rene Avatar answered Sep 29 '22 01:09

Raul Rene