Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 5 ignores @Table schema param

In my application there is an entity:

@Entity
@Table(schema = "hr", name = "personal_data")
public class PersonalData {
}

and connection string defined in Spring's application.properties:

spring.datasource.url=jdbc:mysql://localhost/mobile?UseUnicode=true&characterEncoding=utf8

If I invoke the following code:

TypedQuery<E> typedQuery = em.createQuery("from PersonalData pd where pd.employeeId = ?1", PersonalData.class);
typedQuery.setParameter(1, 123);
return typedQuery.getSingleResult();

it will result in this SQL:

select * from personal_data personalda0_ where personalda0_.employee_id=?

Which will fail with the exception

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mobile.personal_data' doesn't exist

because the table personal_data is defined in the hr database and there is no such table in mobile.

This was working fine(i.e. table name in SQL was prefixed with database name) in Hibernate 4.3.13 and stopped when the application was migrated to Spring Boot 2.0 which uses Hibernate 5.2.14. Is there any way to achieve the old behaviour in Hibernate 5.x?

like image 427
Tomasz W Avatar asked Mar 29 '18 09:03

Tomasz W


1 Answers

I can say that there is a misunderstanding between Hibernate 5 and MySQL, a long story here Hibernate 5.0.6 Ignores schema in MySQL

One Solution is proposed is to use the name of schema in the place of catalog so instead of :

@Table(schema = "hr", name = "personal_data")
       ^^^^^^

You can use :

@Table(catalog = "hr", name = "personal_data")
       ^^^^^^^

Also take a look at this :

  • 5.0 Migration Guide
like image 139
YCF_L Avatar answered Oct 19 '22 02:10

YCF_L