1

I am using hibernate.

I have table like this :

Table backup (ID, TIMESTAMP, DATA)

This table should have 20 rows max at any time. So when inserting row in this table. I should always check no of rows in the table. if its less than 20 then data can be inserted.

However if table size is already 20 then oldest row should be deleted and then the new row should be inserted.

And as this is a web application so there can be multiple threads.

Please suggest is there any build-in feature in hibernate or JPA to support this. If not then how can this be accomplished?

Thanks in advance.

Coder
  • 490
  • 4
  • 18
  • 3
    You could also create a "before insert" trigger. What DBMS are you using? – sp00m Apr 25 '14 at 11:52
  • @sp00m i think you mean DB not DBMS. the dbms has nothing to do with hibernate – Philipp Sander Apr 25 '14 at 11:53
  • 1
    @PhilippSander This solution would have nothing to do with Hibernate neither... [DBMS](http://en.wikipedia.org/wiki/Database) means MySQL, Oracle, PostgreSQL, etc. – sp00m Apr 25 '14 at 11:55
  • No such built-in feature. You have to manually handle this in your code to apply this business logic. – Jay Apr 25 '14 at 11:56
  • @sp00m no. DBMS is the application you use to manipulate the database, like sqldeveloper (for oracle), phpmyadmin (for mysql), ... – Philipp Sander Apr 25 '14 at 11:58
  • 1
    @PhilippSander Well, these aren't DBMS but kinds of IDE actually... – sp00m Apr 25 '14 at 12:11

1 Answers1

0

It is possible to use this

Query.setMaxResults()

since limit was never implemented in Hibernate like in SQL

There is a similar post here, it might help you..

How do you do a limit query in HQL?

Community
  • 1
  • 1
Nemothefish
  • 47
  • 10
  • setMaxResult is used to limit query results. I want to limit no of rows in table. – Coder Apr 25 '14 at 12:39
  • @nimit I dont understand what you are asking, are you saying that you would like to limit the number of rows in a table to a certain amount? if so, I think it is a DB thing, nothing to do with hibernate. – Zeus Apr 25 '14 at 13:41