Not specifying Hibernate dialect - java

I am new to Hibernate. While reading Hibernate, I came across the Dialect property. Whatever database we will use in our application, we need to set dialect related to that database and Hibernate will generate appropriate query related to that database.
Just want to know if it is the mandatory property to be set? If it is not and not specified in the hibernate.cfg.xml file, then how will Hibernate generate the SQL queries i.e. which database compliant SQL query will be generated?

No it is not mandatory as per documentation
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html#configuration-optional-dialects
, I had not try with the same. While your answer of others points I will also like to get answer from some good experienced here. :)

I think it's not mandatory but it's a good practice to set it in your hibernate.cfg.xml as
<property name="dialect">your dialect</property>
posible values:
DB2 org.hibernate.dialect.DB2Dialect
DB2 AS/400 org.hibernate.dialect.DB2400Dialect
DB2 OS390 org.hibernate.dialect.DB2390Dialect
PostgreSQL org.hibernate.dialect.PostgreSQLDialect
MySQL org.hibernate.dialect.MySQLDialect
MySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect
Oracle (any version) org.hibernate.dialect.OracleDialect
Oracle 9i org.hibernate.dialect.Oracle9iDialect
Oracle 10g org.hibernate.dialect.Oracle10gDialect
Sybase org.hibernate.dialect.SybaseDialect
Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Server org.hibernate.dialect.SQLServerDialect
SAP DB org.hibernate.dialect.SAPDBDialect
Informix org.hibernate.dialect.InformixDialect
HypersonicSQL org.hibernate.dialect.HSQLDialect
Ingres org.hibernate.dialect.IngresDialect
Progress org.hibernate.dialect.ProgressDialect
Mckoi SQL org.hibernate.dialect.MckoiDialect
Interbase org.hibernate.dialect.InterbaseDialect
Pointbase org.hibernate.dialect.PointbaseDialect
FrontBase org.hibernate.dialect.FrontbaseDialect
Firebird org.hibernate.dialect.FirebirdDialect

This is why...
You do not need the dialect property if you set up your database connection with hibernate configuration because hibernate does it for you:
hibernate.connection.driver
hibernate.connection.url
user configs and etc...
However, if you set up the connection with regular data source code and use hibernate you need to specify the hibernate.dialect property, because then the connection will not know what hibernate dialect.
dataSource.setdriverClassName
datasource.setUrl...
password configs and etc...

Related

How to set a different HyperSQL schema when using Tomcat JDBC?

I'm attempting to set a custom (non-PUBLIC) schema for my HyperSQL database which I connect to with the Tomcat JDBC driver. I can't seem to get the connections to not use 'PUBLIC'.
I've tried setting various URL parameters and the 'connectionProperties' in the PoolProperties; example:
jdbc:hsqldb:hsql://localhost:0/test;currentSchema=schemaTest

Spring Boot migration script syntax problems due to H2 version update

I've updated the version of H2 database (used only for testing) from 1.4.196 to 2.1.210 and the migration scripts now fails due to syntax errors.
Migration scripts are 2 types - pre (pure SQL) and post liquibase (XML based) introduction to the project.
Here is what is defiend in the test yaml config:
spring:
datasource:
url: jdbc:h2:mem:testdb;Mode=Oracle
platform: h2 // changed to sql.init.platform: h2
jpa:
hibernate:
ddl-auto: validate
I've tryed to add spring.datasource.driverClassName=org.h2.Driver and spring.jpa.database-platform=org.hibernate.dialect.H2Dialect proerpties, but that did not do the trick.
I very mych tink it is due to some configuration difference between the two version 1.4 and 2.1
COMMENT REPLY
The liquibase changeset that causes the error:
<changeSet id="20191112130000-1" author="zdravko">
<dropColumn tableName="NEWS_CONTENT" columnName="DAY"/>
</changeSet>
Error itself:
Reason: liquibase.exception.DatabaseException: Syntax error in SQL statement "ALTER TABLE PUBLIC.NEWS_CONTENT DROP COLUMN [*]DAY"; expected "identifier"; SQL statement:
ALTER TABLE PUBLIC.NEWS_CONTENT DROP COLUMN DAY [42001-210] [Failed SQL: (42001) ALTER TABLE PUBLIC.NEWS_CONTENT DROP COLUMN DAY]
DAY is a keyword in H2 and it is also a reserved word in the SQL Standard (even in completely outdated SQL-92 and in all newer versions), it means it cannot be used as unquoted identifier.
Liquibase supports H2 2.x.y and its keywords since the version 4.7.0, if you use some older version, you need to upgrade. If you use this or newer version, something is going wrong.
In that case you can check objectQuotingStrategy and use QUOTE_ALL_OBJECTS:
https://docs.liquibase.com/commands/config-ref/objectquotingstrategy.html
Also you can add ;NON_KEYWORDS=DAY to JDBC URL of H2, but it would be better to avoid it if you can, this setting isn't very reliable and should be used only when you can't do anything else.

mysql dialect in hsqldb

I'm using hibernate to manage DB operations and MySQL in my application - use org.hibernate.dialect.MySQLInnoDBDialect as the hibernate dialect. But for testing purposes I'm using HSQLDB 1.8.0.10.
I have problem with query like this (working good on mysql not on hsql):
SELECT DISTINCT(id) FROM table ORDER BY name;
I know that the problem is with distinct and order by (http://weblogs.sqlteam.com/jeffs/archive/2007/12/13/select-distinct-order-by-error.aspx ) and solution of this could be for example:
SELECT DISTINCT(id) FROM table GROUP BY id ORDER BY MAX(name);
But my question is, if there is any possibility to using MySQL dialect in HSQLDB and not have to using this solution?
HSQLDB implements the SQL Standard correctly and does not allow the ambiguous query. It is not possible to change its behaviour.
It is better to modify your MySQL queries to be standard compliant. This allows you to port your application to another database more easily.

running "virtual" database in Java code with Hibernate

I used to run my Java code with Hibernate to mysql.
Some logical changes led me to decide that I don't need to save the data in the database, but in java class that will handle it as a database (The data is delete in every startup of the application).
How can I do in Hibernate? is there a way to create an in memory database?
Consider H2 (recommended) or HSQLDB. You can certainly use Hibernate with them. In fact, HSQLDB was the database used by default in Hibernate for their test suite (I think they just changed to H2).
Take a look at Apache Derby.
You could also use SqlLite, which is an in-memory database. I'm just about to start using it with NHibernate for the same purpose in unit tests.
You just need to:
add the driver for the database you want to use and change the hibernate settings for this driver,
change the setting hibernate database dialect,
change the jdbc connection String
(add the jars for the database)
For example for Hypersonic H2 Database:
driverClassName = org.h2.Driver
dialect = org.hibernate.dialect.H2Dialect
connection string = jdbc:h2:mem:test

Dialect for SQL2005

I am new to Hibernate.
We are migrating one of our existing web application which uses Hibernate 3 and SQL 2000 server to SQL 2005 server.
Can I get help about which version of Hibernate to use with SQL 2005 as I could not find any dialect for SQL 2005 in Hibernate 3.0.5?
Thanks in advance.
I don't believe that the dialect recognizes the difference between SQL Server years; it's all just Microsoft SQL Server.
Which JDBC driver are you using? Microsoft's or jTDS? You only get the features that the driver supports.
And you shouldn't be using database specific features. Your code isn't portable that way.
I am using the standard SQLServer dialect (org.hibernate.dialect.SQLServerDialect) for SQLServer 2005 and I got no issues so far.
I'm using the JTDS JDBC Driver.
I'm also using the Schema Generator, no problems so far.

Categories

Resources