I was surprised to discover that Hibernate makes use of temporary tables when performing deletes. The creation of the table was triggering an alert in a database monitor for an application. The monitor logged the creation of tables beginning with the prefix HT_ and a name that mirrored that of an entity tablename.
Initially I must admit that I was thrown very far off track and started looking through the schema creation scripts and codebase looking for something, even checking HT as initials against the developer names. Naturally we tried deleting the tables and soon enough they were recreated.
As with a lot of modern programming bugs the answer came through some frustrated Googling and the realisation that HT might stand for Hibernate Table. With that insight in place I was then able to find this excellent explanation of the situation and I get another self-righteous data point in my dislike of ORM.
This temporary table is required for two reasons, firstly because ORM’s do not actually do a proper mapping between the domain concepts of a relational store and objects and secondly because Hibernate wants to create a verisimilitude around the concept of a class hierarchy.
I don’t think there is a better solution than what Hibernate is offering here but I do think it is the wrong problem being solved in a clever way.
4 thoughts on “Hibernate temporary tables”
What if my database doesn’t allow to create Temp Table?
it gives below error.
could not insert/select ids for bulk update
org.hibernate.exception.SQLGrammarException: could not insert/select ids for bulk update
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
… 48 more
I’m not aware of a way around this, your user needs the right permissions.
I am facing same exception.. Any thougths/ideas to work around this issue. Thanks
There isn’t really a workaround, you need the permissions or Hibernate is not going to work.
If you can control the schema details then one thing you can do is not delete records but instead add a column to represent a “deletion marker”. However that will cause performance problems if the deleted rows go beyond about 10% of your data. You could also try using archive or active tables that simply contain the primary key of the entries you want to query against. That will create a join obviously but should be performant in most cases.
If you can’t make any changes to the database tier then you are out of luck and should probably use some other ORM layer or write the SQL yourself and use Hibernate’s direct execution features.