Hibernate generates invalid sql "select max (id) from my_table"

I made a small application with Hibernate. only from samples available from the HB site.

Hibernate says DB:

drop table if exists some_db.my_table create table some_db.my_table ...... select max(id) from my_table 

when I switch from HSQL database to MySQL.

I have the error "DEBUG ohejdbc.spi.SqlExceptionHelper - You have an error in the SQL syntax, check the manual that matches the version of your MySQL server for the correct syntax to use next to" my_table "on line 1 [n / a]"

because HB is trying hard to say

 select max(id) from my_table 

instead

 select max(id) from some_db.my_table 

which is the correct syntax when it comes to

 public void testBasicUsage() { // create a couple of events... Session session = sessionFactory.openSession(); session.beginTransaction(); session.save( new Event( ..... ) ); // <<-------------------HERE session.save(new Event( ..... )); session.getTransaction().commit(); session.close(); 

and changing the dialect does not help.

I tried switching to different versions of hb, for example

 <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.6.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> 

but that doesn't help either.

change of connection (standard "root" / "access"), for example

 jdbc:mysql://localhost:3306/some_db 

instead

 jdbc:mysql://localhost:3306 

doesn't help either.

this seems like a mistake, but what would be the solution?

+4
source share
2 answers

You really need to publish an XML mapping to annotated entities / hibernate.

The most likely reason is that you did not specify the schema property in the annotation.

those. something along the lines

 @Table(schema="some_db") 
+2
source

You can use the schema parameter:

 <generator class="increment"> <param name="schema">some_db</param> </generator> 
0
source

Source: https://habr.com/ru/post/1434151/


All Articles