
I had problems to use Hibernate(JPA) to work with Firebird a while ago. Every connection opened was not being closed, I couldn’t ever think to use a pool of connection in my web service and the temporary tables was being created next to other tables. In this post, I will focus on the problem with temporary tables.
Hibernate creates temporary tables in other databases too but there is a specifically problem with Firebird. After sometime (after a lot of websites), I figured out that the Hibernate dialect for Firebird doesn’t know about global temporary tables, so it creates them as normal tables instead.
So, the solution for this problem is implement a class that will deal with the Dialect and in this class take control the creation of the temporary tables.
| public class Firebird21Dialect extends FirebirdDialect { | |
| @Override | |
| public boolean dropTemporaryTableAfterUse() { | |
| return true; | |
| } | |
| @Override | |
| public String getCreateTemporaryTablePostfix() { | |
| return " ON COMMIT PRESERVE ROWS"; | |
| } | |
| @Override | |
| public String getCreateTemporaryTableString() { | |
| return "CREATE GLOBAL TEMPORARY TABLE "; | |
| } | |
| @Override | |
| public boolean supportsTemporaryTables() { | |
| return true; | |
| } | |
| } |
In the persistence.xml add this attribute that references our new class:
| <?xml version="1.0" encoding="UTF-8"?> | |
| <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> | |
| <persistence-unit name="MyPersistenceUnit"> | |
| <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | |
| <properties> | |
| <!-- ... --> | |
| <property name="hibernate.dialect" value="br.com.tassioauad.myapp.resource.Firebird21Dialect" /> | |
| <!-- ... --> | |
| </properties> | |
| </persistence-unit> | |
| </persistence> |
Hibernate 5.2.1.Final has no claims to these methods.
CurtirCurtir
Thank you for your great contribution, Tiago!
CurtirCurtir