Hibernate: use default HibernateJpaConfiguration (#2325)
Condifure properties via HibernatePropertiesCustomizer Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -20,6 +20,7 @@ import org.springframework.http.HttpHeaders;
|
|||||||
import org.springframework.test.context.TestPropertySource;
|
import org.springframework.test.context.TestPropertySource;
|
||||||
|
|
||||||
@TestPropertySource(properties = {
|
@TestPropertySource(properties = {
|
||||||
|
"spring.flyway.enabled=true", // if hibernate is used there could be db inconsistencies when executing tests with and without flyway
|
||||||
"hawkbit.server.security.allowedHostNames=localhost",
|
"hawkbit.server.security.allowedHostNames=localhost",
|
||||||
"hawkbit.server.security.httpFirewallIgnoredPaths=/index.html" })
|
"hawkbit.server.security.httpFirewallIgnoredPaths=/index.html" })
|
||||||
@Feature("Integration Test - Security")
|
@Feature("Integration Test - Security")
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Contributors to the Eclipse Foundation
|
||||||
|
*
|
||||||
|
* This program and the accompanying materials are made
|
||||||
|
* available under the terms of the Eclipse Public License 2.0
|
||||||
|
* which is available at https://www.eclipse.org/legal/epl-2.0/
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: EPL-2.0
|
||||||
|
*/
|
||||||
|
package org.eclipse.hawkbit.mgmt;
|
||||||
|
|
||||||
|
public class sdfs {
|
||||||
|
}
|
||||||
@@ -10,12 +10,6 @@
|
|||||||
package org.eclipse.hawkbit.repository.jpa;
|
package org.eclipse.hawkbit.repository.jpa;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import org.eclipse.hawkbit.repository.jpa.model.EntityPropertyChangeListener;
|
import org.eclipse.hawkbit.repository.jpa.model.EntityPropertyChangeListener;
|
||||||
|
|
||||||
@@ -30,33 +24,23 @@ import org.hibernate.event.spi.EventType;
|
|||||||
import org.hibernate.integrator.spi.Integrator;
|
import org.hibernate.integrator.spi.Integrator;
|
||||||
import org.hibernate.jpa.boot.spi.IntegratorProvider;
|
import org.hibernate.jpa.boot.spi.IntegratorProvider;
|
||||||
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
|
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
|
||||||
import org.springframework.beans.factory.ObjectProvider;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
|
import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
|
||||||
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
|
|
||||||
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
|
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
|
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
|
||||||
import org.springframework.transaction.jta.JtaTransactionManager;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* General Hibernate configuration for hawkBit's Repository.
|
* General Hibernate configuration for hawkBit's Repository.
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class JpaConfiguration extends JpaBaseConfiguration {
|
public class JpaConfiguration {
|
||||||
|
|
||||||
private final TenantIdentifier tenantIdentifier;
|
private final TenantIdentifier tenantIdentifier;
|
||||||
private final boolean enableLazyLoadNoTrans;
|
private final boolean enableLazyLoadNoTrans;
|
||||||
|
|
||||||
protected JpaConfiguration(
|
protected JpaConfiguration(
|
||||||
final DataSource dataSource, final JpaProperties properties,
|
|
||||||
final ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider,
|
|
||||||
final TenantAware.TenantResolver tenantResolver,
|
final TenantAware.TenantResolver tenantResolver,
|
||||||
@Value("${hibernate.enable_lazy_load_no_trans:true}") final boolean enableLazyLoadNoTrans) {
|
@Value("${hibernate.enable_lazy_load_no_trans:true}") final boolean enableLazyLoadNoTrans) {
|
||||||
super(dataSource, properties, jtaTransactionManagerProvider);
|
|
||||||
tenantIdentifier = new TenantIdentifier(tenantResolver);
|
tenantIdentifier = new TenantIdentifier(tenantResolver);
|
||||||
this.enableLazyLoadNoTrans = enableLazyLoadNoTrans;
|
this.enableLazyLoadNoTrans = enableLazyLoadNoTrans;
|
||||||
}
|
}
|
||||||
@@ -66,30 +50,18 @@ public class JpaConfiguration extends JpaBaseConfiguration {
|
|||||||
return tenantIdentifier;
|
return tenantIdentifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Bean
|
||||||
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
|
HibernatePropertiesCustomizer hibernatePropertiesCustomizers() {
|
||||||
return new HibernateJpaVendorAdapter() {
|
return hibernateProperties -> {
|
||||||
|
// override the default naming strategy
|
||||||
private final HibernateJpaDialect jpaDialect = new HibernateJpaDialect();
|
hibernateProperties.put("hibernate.physical_naming_strategy", org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl.class.getName());
|
||||||
|
hibernateProperties.put(MultiTenancySettings.MULTI_TENANT_IDENTIFIER_RESOLVER, tenantIdentifier);
|
||||||
@Override
|
hibernateProperties.put("hibernate.multiTenancy", "DISCRIMINATOR");
|
||||||
public HibernateJpaDialect getJpaDialect() {
|
|
||||||
return jpaDialect;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Map<String, Object> getVendorProperties(final DataSource dataSource) {
|
|
||||||
final Map<String, Object> properties = new HashMap<>(4);
|
|
||||||
|
|
||||||
properties.put(MultiTenancySettings.MULTI_TENANT_IDENTIFIER_RESOLVER, tenantIdentifier);
|
|
||||||
properties.put("hibernate.multiTenancy", "DISCRIMINATOR");
|
|
||||||
// LAZY_LOAD - Enable lazy loading of lazy fields when session is closed - N + 1 problem occur.
|
// LAZY_LOAD - Enable lazy loading of lazy fields when session is closed - N + 1 problem occur.
|
||||||
// So it would be good if in future hawkBit run without that
|
// So it would be good if in future hawkBit run without that
|
||||||
// Otherwise, if false, call for the lazy field will throw LazyInitializationException
|
// Otherwise, if false, call for the lazy field will throw LazyInitializationException
|
||||||
properties.put("hibernate.enable_lazy_load_no_trans", enableLazyLoadNoTrans);
|
hibernateProperties.put("hibernate.enable_lazy_load_no_trans", enableLazyLoadNoTrans);
|
||||||
properties.put("hibernate.integrator_provider", (IntegratorProvider) () -> Collections.singletonList(new Integrator() {
|
hibernateProperties.put("hibernate.integrator_provider", (IntegratorProvider) () -> Collections.singletonList(new Integrator() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void integrate(
|
public void integrate(
|
||||||
@@ -105,6 +77,6 @@ public class JpaConfiguration extends JpaBaseConfiguration {
|
|||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
return properties;
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user