Hibernate with SQLite
Hibernate is a very use-full framework, but sometimes you don't want to install a full-blown database, but for some reason hibernate does not default support SQLite. With some searching and extending however its possible. Here is a working solution I've made 
The most important file, and only file you'll extra need is SQLiteDialect:
Java: SQLiteDialect.java
And in your hibernate config:
Java:
or in config file:
XML:
The most important file, and only file you'll extra need is SQLiteDialect:
Java: SQLiteDialect.java
1 | import java.sql.Types;
|
And in your hibernate config:
Java:
1 | cfg.setProperty("hibernate.dialect", "package.SQLiteDialect"); |
or in config file:
XML:
1 | <property name="dialect">package.SQLiteDialect</property> |
11-'09 Provinciaal ziekenhuis
11-'09 Wifi & wifi
Comments
Neat, and simple. In fact, it's so simple, I don't understand why it's not there by default - did you double check that? Is there any particular reason they wouldn't add this?
Don't know...but I found it very strange that nHibernate (.net framework) did support SQLite and the hibernate framework didn't.
I can get so angry at framework developers.
Why do you need to specify your package twice? There is no purpose in that.
Why do you need to specify your package twice? There is no purpose in that.
What do you mean with twice?
Hi,
I'm new in hibernate framework.
Can you send your 'hibernate.cfg.xml' to have a complete example
tank you for your light
I'm new in hibernate framework.
Can you send your 'hibernate.cfg.xml' to have a complete example
tank you for your light
Many thanks for this article,
trying this dialect for SQLite works fine, until I try to use types like "numeric" or "decimal". Setting a breakpoint within the constructor of SQLiteDialect shows, that the "org.hibernate.MappingException: Could not determine type for: numeric, at..." exception is thrown before the constructor is called. After I change the column type to "integer" everything is fine until I try to run a query over that table.
I'm using xerial sqlite-jdbc 3.6.0 with hibernate 3.2.6.
What's wrong? Any hints?
trying this dialect for SQLite works fine, until I try to use types like "numeric" or "decimal". Setting a breakpoint within the constructor of SQLiteDialect shows, that the "org.hibernate.MappingException: Could not determine type for: numeric, at..." exception is thrown before the constructor is called. After I change the column type to "integer" everything is fine until I try to run a query over that table.
I'm using xerial sqlite-jdbc 3.6.0 with hibernate 3.2.6.
What's wrong? Any hints?
I'm using v056 of http://www.zentus.com/sqlitejdbc/
I've found my fault. I was using a wrong type "decimal" or "numeric", which must be called "big_decimal" instead. Thats fine so far. No I try to use a patch from "http://github.com/gwenn/sqlitejdbc" to support BigDecimal.
Thank you very much, it works perfectly.
If you do need to use a full hibernate config file, you can have a look at how to use properties from http://code.google.com/p/hibernate-sqlite/ In this project there is hibernate xml config file for sqlite.
If you do need to use a full hibernate config file, you can have a look at how to use properties from http://code.google.com/p/hibernate-sqlite/ In this project there is hibernate xml config file for sqlite.
Dialect is gr8 but i still get the error :
528 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - could not complete schema update
org.hibernate.exception.GenericJDBCException: could not get table metadata: %TableName%
If no table exists in sqlite database SchemaUpdate works fine. But if table exists an error occurs. How to fix it for sqlite? What else should i add to the config file except
<property name="hbm2ddl.auto">update</property>
528 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - could not complete schema update
org.hibernate.exception.GenericJDBCException: could not get table metadata: %TableName%
If no table exists in sqlite database SchemaUpdate works fine. But if table exists an error occurs. How to fix it for sqlite? What else should i add to the config file except
<property name="hbm2ddl.auto">update</property>
Update isn't supported, change too:
code:
That should work
code:
1
| <property name="hbm2ddl.auto">create</property> |
That should work
Thx for responding.
If it is not supported, then why it creates tables (when update value is specified) but only when tables don't exist?
If it is not supported, then why it creates tables (when update value is specified) but only when tables don't exist?
basic function is to create (and drop) tables, altering would require some extensions to the sqlite driver.
What are the username, password and url?
Example:
code:
code:
1
2
3
4
5
6
7
8
9
| Configuration cfg = new Configuration();
cfg.setProperty("hibernate.show_sql", "true");
cfg.setProperty("hibernate.format_sql", "true");
cfg.setProperty("hibernate.dialect", "your.packageSQLiteDialect");
cfg.setProperty("hibernate.connection.driver_class", "org.sqlite.JDBC");
cfg.setProperty("hibernate.connection.url", "jdbc:sqlite:"+file.toString());
if (create) cfg.setProperty("hibernate.hbm2ddl.auto", "create");
cfg.addClass(....class);
sessionFactory = cfg.buildSessionFactory(); |
Thank you very much for the SQLiteDialect class, it was very helpful!
I think I found a bug when working with setFirstResult and setMaxResults:
The values for setFirstResult and setMaxResults are used in the wrong order, the query starts always at the same offset and the size of the resultset is increasing:
SQLite supports two different ways for specifying limit and offset where the parameters are used in reverse order:
http://stackoverflow.com/...-limit-offset-query-doubt
http://dev.mysql.com/doc/refman/5.1/en/select.html
The syntax LIMIT <count> OFFSET <skip> is the same as used in PostgreSQLDialect, but the class PostgreSQLDialect contains the following method to indicate the reverse order:
code:
The syntax LIMIT <skip>, <count> is the same as used in MySQL, but the class MySQLDialect does not override the bindLimitParametersInReverseOrder method.
The bug can be fixed with overriding the method bindLimitParametersInReverseOrder the same way as in the PostgreSQLDialect.
I think I found a bug when working with setFirstResult and setMaxResults:
The values for setFirstResult and setMaxResults are used in the wrong order, the query starts always at the same offset and the size of the resultset is increasing:
- Query with setFirstResult=200 and setMaxResults=100 queries the database with limit=200 and offset =100.
- Query with setFirstResult=300 and setMaxResults=100 queries the database with limit=300 and offset =100.
SQLite supports two different ways for specifying limit and offset where the parameters are used in reverse order:
http://stackoverflow.com/...-limit-offset-query-doubt
http://dev.mysql.com/doc/refman/5.1/en/select.html
The syntax LIMIT <count> OFFSET <skip> is the same as used in PostgreSQLDialect, but the class PostgreSQLDialect contains the following method to indicate the reverse order:
code:
1
2
3
| public boolean bindLimitParametersInReverseOrder() {
return true;
} |
The syntax LIMIT <skip>, <count> is the same as used in MySQL, but the class MySQLDialect does not override the bindLimitParametersInReverseOrder method.
The bug can be fixed with overriding the method bindLimitParametersInReverseOrder the same way as in the PostgreSQLDialect.