5.1 Automatic data schema generation

When you first configure Hibernate, you can enable a lot of interesting settings. I did not bring them before, so as not to spray. But at the end of the level I think about some of them to tell.

The first such setting is the hbm2ddl.auto. It can have 5 different values:

validate Validation: Hibernate will check if the column and field names and types match in the database and in the annotations. This is the most common mode.
update Update: Hibernate will update the tables in the database if they or their columns are different than expected.
create Recreate: Hibernate will delete all tables in the database and recreate them based on the data from the annotations.
create-drop Creation-deletion. At the beginning of the work, Hibernate will create all the tables, at the end of the work, it will delete them after itself.
none Hibernate won't do anything at all. If somewhere the base does not match the expectation, then errors will be thrown during the execution of queries.

5.2 Logging requests

The second very useful setting of Hibernate is the logging of all its requests to the database: all requests to the database are duplicated in the console. This is a very useful feature if you are making changes to Hibernate related code.

First, you will better understand how your queries are converted to SQL. Secondly, it is easier and earlier you will be able to find errors. And they definitely will. Hibernate doesn't always work the way we expect. This is especially often associated with annotations: you understand them in your own way, and Hibernate in your own way.

The setting that enables logging is called hibernate.show_sql. If you set its value to true, then queries to the database will be written to the console. The parameter is also used in conjunction with it hibernate.format_sql, which allows you to set a convenient SQL query format in the log.

Another way to log requests to the database is to use the standard logger . Everything gave in the fact that Hibernate already writes its queries to the standard logger, but only with the scope - DEBUG. You need to change two properties in your standard logger:

logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace

Changing the level of BasicBinder to tracewill add query parameters to us, however, in a slightly unusual form - sequential enumeration after the query itself.

The third approach is to use a special proxy driver for the database .

For example, log4jdbcor p6spy. Both proxies are working and have starters, although log4jdbcthere were no commits for a long time at the time of writing.


<dependency>
    <groupId>com.integralblue</groupId>
    <artifactId>log4jdbc-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>com.github.gavlyukovskiy</groupId>
    <artifactId>p6spy-spring-boot-starter</artifactId>
</dependency>

I will not go into details, I just want you to know that this is possible.

5.3 SQL dialects

And some more background information.

Hibernate supports a very large number of DBMSs. Each of them implements a standard set of SQL functions and some more of their own. Or different versions of SQL. Therefore, to work with these DBMS, you need to tell Hibernate which dialect of the SQL language to use.

Here is a list of the most popular dialects:

PostgreSQL org.hibernate.dialect.PostgreSQLDialect
SAP DB org.hibernate.dialect.SAPDBDialect
Sybase org.hibernate.dialect.SybaseDialect
Informix org.hibernate.dialect.InformixDialect
Microsoft SQL Server 2008 org.hibernate.dialect.SQLServer2008Dialect
MySQL org.hibernate.dialect.MySQLDialect
Oracle (any version) org.hibernate.dialect.OracleDialect
Oracle 11g org.hibernate.dialect.Oracle10gDialect

Of course, there can be any number of such dialects. You can write your own DBMS and your own dialect for it.

Why is it important to specify the correct dialect?

Each database may have slightly different data types. Therefore, in order for Hibernate to work perfectly exactly the way you expect from it, you need to tell it which dialect of the SQL language it needs to use.

Here is an interesting picture that shows how everything is connected in reality:

5.4 Popular settings

To make your life easier, here is a complete list of several settings:

MySQL 8.0
hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
hibernate.connection.driver_class=com.mysql.cj.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/test
hibernate.connection.username=root
hibernate.connection.password=secret
hibernate.show_sql=true
hibernate.hbm2ddl=validate
MySQL 5.0
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/test
hibernate.connection.username=root
hibernate.connection.password=secret
hibernate.show_sql=true
hibernate.hbm2ddl=validate
PostgreSQL
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.driver_class= org.postgresql.Driver
hibernate.connection.url= jdbc:postgresql://localhost/test
hibernate.connection.username=root
hibernate.connection.password=secret
hibernate.show_sql=true
hibernate.hbm2ddl=validate

MySQL 8.0 supports more features than MySQL 5.0, so if you want Hibernate to make the most of all of them, then specify the correct dialect.

The H2 database is usually stored in memory, so mem:testis both the name of the SQL server and the name of the schema you will be working with.

undefined
1
Task
Module 4. Working with databases, level 9, lesson 4
Locked
task0906
task0906