Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc: hibernate.dialect_resolvers vs HibernateJpaVendorAdapter.setDatabase [SPR-16428] #20974

Closed
spring-projects-issues opened this issue Jan 27, 2018 · 6 comments
Assignees
Labels
in: data status: backported type: task
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Jan 27, 2018

Giovanni Lovato opened SPR-16428 and commented

Hibernate property hibernate.dialect_resolvers let specify custom Dialect resolvers if no hibernate.dialect is set. I cannot get the property to work with Spring Data JPA and debugging I've found what I guess is the issue, i.e. HibernateJpaVendorAdapter at Line 130 always sets the dialect property and doing so Hibernate will not take into account the dialect_resolvers property.

if (getDatabasePlatform() != null) {
    jpaProperties.put(AvailableSettings.DIALECT, getDatabasePlatform());
}
else {
    Class<?> databaseDialectClass = determineDatabaseDialectClass(getDatabase());
    if (databaseDialectClass != null) {
        jpaProperties.put(AvailableSettings.DIALECT, databaseDialectClass.getName());
    }
}

Affects: 4.3.14, 5.0.3

Issue Links:

  • #20972 Deprecate outdated HibernateTemplate operations in favor of lambdas

Referenced from: commits e1fa65a, f187ca6

Backported to: 4.3.15

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 27, 2018

Giovanni Lovato commented

What’s the purpose of the else branch at all? If the dialect isn’t set by the user, Hibernate’s already has its own default resolver that will select the right one, even more precisely than this (which just returns the latest dialect class, without checking the database version).

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 29, 2018

Juergen Hoeller commented

So you are setting the "database" property on HibernateJpaVendorAdapter but do not want the dialect to be directly derived from it? Could you simply drop that property since that's pretty much its sole purpose in HibernateJpaVendorAdapter? In case of Database.DEFAULT, no Hibernate property is going to be set since determineDatabaseDialectClass will return null, which in turn means that Hibernate's standard dialect resolution mechanism kicks in.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 29, 2018

Giovanni Lovato commented

I'm not setting any other property than hibernate.dialect_resolvers, I'm not referencing HibernateJpaVendorAdapter anywhere. This is my application.yml:

spring:
  datasource:
    jndi-name: java:/datasources/docker/postgres/default
  jpa:
    open-in-view: false
    show-sql: true
    generate-ddl: true
    properties:
      hibernate:
        dialect_resolvers: it.axians.oss.data.jpa.dialect.ExtendedDialectResolver

The workaround I'm using now is to specify also hibernate.dialect: # empty so that the property set by Spring will be overwritten with an empty string and Hibernate will ignore that, triggering the resolvers. It works but it's very counter-intuitive and I needed a lot of debugging to figure it out.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 29, 2018

Juergen Hoeller commented

It turns out that Spring Boot is setting the database by default: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-jpa-properties. It looks like you could also set spring.jpa.database to default for Hibernate's default algorithm, next to your custom hibernate.dialect_resolvers configuration, avoiding the awkward need to set hibernate.dialect to empty.

From a Spring Framework perspective, this is essentially a documentation issue for anybody configuring HibernateJpaVendorAdapter directly. Let's repurpose this ticket for an explicit note about HibernateJpaVendorAdapter.setDatabase vs hibernate.dialect_resolvers, clarifying that they are mutually exclusive.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 29, 2018

Giovanni Lovato commented

That's okay for me. Thank you for the explanation!

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 29, 2018

Juergen Hoeller commented

I've added corresponding notes to the AbstractJpaVendorAdapter.setDatabase and the general HibernateJpaVendorAdapter javadocs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data status: backported type: task
Projects
None yet
Development

No branches or pull requests

2 participants