-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: DELETE query - how to limit records by rownumber?
PostPosted: Tue Mar 30, 2010 8:55 am 
Newbie

Joined: Tue Mar 30, 2010 7:45 am
Posts: 1
Hello all,
How to limit records to be deleted with jpa delete query by rownumber because setFirstResult() doesn't work with delete (named) query?

Problem:
When is history entry written into table, I want always to keep last <n> records in table (delete older records from n+1 to maxRecords).
THIS DOESN'T WORK (all records are deleted regardless setFirstResult() ):
Code:
/*  @NamedQuery(name = "history.delete",
      query = "DELETE FROM History hist WHERE hist.username = :username ORDER BY hist.timestamp DESC") */
Query queryDelHistory = em.createNamedQuery("history.delete");
queryDelHistory.setParameter("username", username);
queryDelHistory.setFirstResult(n);
int deletedRecords = queryDelHistory.executeUpdate();

THIS WORKS (correct records deleted one by one, not very effiicient):
Code:
/*  @NamedQuery(name = "history.read",
    query = "SELECT hist FROM History hist WHERE hist.username = :username ORDER BY hist.timestamp DESC")*/
Query queryReadHistory = em.createNamedQuery("history.read");
queryReadHistory.setParameter("username", username);
queryReadHistory.setFirstResult(n);
List<History> historyList= queryReadHistory.getResultList();
for (History history:historyList ) {
        em.remove(history);
}


My dependencies:
Hibernate: 3.3.2 GA, Annotations 3.4.0 GA, EntityManager 3.4.0 GA
MySQL Community 5.1.43

Many thanks in advance,
Boris


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.