Hibernate: use default HibernateJpaConfiguration (#2325)

Condifure properties via HibernatePropertiesCustomizer

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-03-21 16:48:14 +02:00
committed by GitHub
parent 36d8ab47ad
commit 4e113f9483
3 changed files with 41 additions and 55 deletions

View File

@@ -20,6 +20,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.test.context.TestPropertySource;
@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.httpFirewallIgnoredPaths=/index.html" })
@Feature("Integration Test - Security")

View File

@@ -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 {
}

View File

@@ -10,12 +10,6 @@
package org.eclipse.hawkbit.repository.jpa;
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;
@@ -30,33 +24,23 @@ import org.hibernate.event.spi.EventType;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.jpa.boot.spi.IntegratorProvider;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
import org.springframework.context.annotation.Bean;
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.
*/
@Configuration
public class JpaConfiguration extends JpaBaseConfiguration {
public class JpaConfiguration {
private final TenantIdentifier tenantIdentifier;
private final boolean enableLazyLoadNoTrans;
protected JpaConfiguration(
final DataSource dataSource, final JpaProperties properties,
final ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider,
final TenantAware.TenantResolver tenantResolver,
@Value("${hibernate.enable_lazy_load_no_trans:true}") final boolean enableLazyLoadNoTrans) {
super(dataSource, properties, jtaTransactionManagerProvider);
tenantIdentifier = new TenantIdentifier(tenantResolver);
this.enableLazyLoadNoTrans = enableLazyLoadNoTrans;
}
@@ -66,45 +50,33 @@ public class JpaConfiguration extends JpaBaseConfiguration {
return tenantIdentifier;
}
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new HibernateJpaVendorAdapter() {
@Bean
HibernatePropertiesCustomizer hibernatePropertiesCustomizers() {
return hibernateProperties -> {
// override the default naming strategy
hibernateProperties.put("hibernate.physical_naming_strategy", org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl.class.getName());
hibernateProperties.put(MultiTenancySettings.MULTI_TENANT_IDENTIFIER_RESOLVER, tenantIdentifier);
hibernateProperties.put("hibernate.multiTenancy", "DISCRIMINATOR");
// 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
// Otherwise, if false, call for the lazy field will throw LazyInitializationException
hibernateProperties.put("hibernate.enable_lazy_load_no_trans", enableLazyLoadNoTrans);
hibernateProperties.put("hibernate.integrator_provider", (IntegratorProvider) () -> Collections.singletonList(new Integrator() {
private final HibernateJpaDialect jpaDialect = new HibernateJpaDialect();
@Override
public void integrate(
final Metadata metadata, final BootstrapContext bootstrapContext,
final SessionFactoryImplementor sessionFactory) {
sessionFactory.getServiceRegistry()
.getService(EventListenerRegistry.class)
.appendListeners(EventType.POST_UPDATE, new EntityPropertyChangeListener());
}
@Override
public HibernateJpaDialect getJpaDialect() {
return jpaDialect;
}
@Override
public void disintegrate(final SessionFactoryImplementor sessionFactory, final SessionFactoryServiceRegistry serviceRegistry) {
// do nothing
}
}));
};
}
@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.
// So it would be good if in future hawkBit run without that
// Otherwise, if false, call for the lazy field will throw LazyInitializationException
properties.put("hibernate.enable_lazy_load_no_trans", enableLazyLoadNoTrans);
properties.put("hibernate.integrator_provider", (IntegratorProvider) () -> Collections.singletonList(new Integrator() {
@Override
public void integrate(
final Metadata metadata, final BootstrapContext bootstrapContext,
final SessionFactoryImplementor sessionFactory) {
sessionFactory.getServiceRegistry()
.getService(EventListenerRegistry.class)
.appendListeners(EventType.POST_UPDATE, new EntityPropertyChangeListener());
}
@Override
public void disintegrate(final SessionFactoryImplementor sessionFactory, final SessionFactoryServiceRegistry serviceRegistry) {
// do nothing
}
}));
return properties;
}
}