Skip to content

Commit

Permalink
Hibernate support
Browse files Browse the repository at this point in the history
Signed-off-by: Avgustin Marinov <[email protected]>
  • Loading branch information
avgustinmm committed Dec 12, 2024
1 parent af50e8c commit 5eb9243
Show file tree
Hide file tree
Showing 41 changed files with 1,061 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -746,14 +746,4 @@ private DistributionSetType generateTestType() {
assertThat(testType.getMandatoryModuleTypes()).containsExactly(osType);
return testType;
}

private void createSoftwareModulesAlphabetical(final int amount) {
char character = 'a';
for (int index = 0; index < amount; index++) {
final String str = String.valueOf(character);
softwareModuleManagement.create(
entityFactory.softwareModule().create().name(str).description(str).vendor(str).version(str));
character++;
}
}
}
3 changes: 3 additions & 0 deletions hawkbit-repository/hawkbit-repository-jpa-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# hawkBit JPA EclipseLink Vendor integration

Implementation of [EclipseLink](http://www.eclipse.org/eclipselink/) JPA vendor.
47 changes: 47 additions & 0 deletions hawkbit-repository/hawkbit-repository-jpa-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!--
Copyright (c) 2015 Bosch Software Innovations GmbH and others
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
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.hawkbit</groupId>
<version>${revision}</version>
<artifactId>hawkbit-repository</artifactId>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>hawkbit-repository-jpa-api</artifactId>
<name>hawkBit :: Repository :: JPA API</name>

<properties>
<apt.source.dir>${project.build.directory}/generated-sources/apt/</apt.source.dir>
</properties>

<dependencies>
<dependency>
<groupId>org.eclipse.hawkbit</groupId>
<artifactId>hawkbit-repository-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>${eclipselink.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void afterCommit() {
@Override
@SuppressWarnings({ "squid:S1217" })
public void afterCompletion(final int status) {
log.debug("Transaction completed after commit with status {}", status == STATUS_COMMITTED ? "COMMITTED" : "ROLLEDBACK");
log.debug("Transaction completed after commit with status {}", status == TransactionSynchronization.STATUS_COMMITTED ? "COMMITTED" : "ROLLEDBACK");
}

private void afterCommit(final Runnable runnable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
*
* 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.repository.model;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

import org.eclipse.hawkbit.repository.Identifiable;

/**
* Core information of all entities.
*/
public interface BaseEntity extends Serializable, Identifiable<Long> {

static Long getIdOrNull(final BaseEntity entity) {
return entity == null ? null : entity.getId();
}

/**
* @return user that created the {@link BaseEntity}.
*/
String getCreatedBy();

/**
* @return time in {@link TimeUnit#MILLISECONDS} when the {@link BaseEntity} was created.
*/
long getCreatedAt();

/**
* @return user that updated the {@link BaseEntity} last.
*/
String getLastModifiedBy();

/**
* @return time in {@link TimeUnit#MILLISECONDS} when the {@link BaseEntity}
* was last time changed.
*/
long getLastModifiedAt();

/**
* @return version of the {@link BaseEntity}.
*/
int getOptLockRevision();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# hawkBit JPA EclipseLink Vendor integration

Implementation of [EclipseLink](http://www.eclipse.org/eclipselink/) JPA vendor.
74 changes: 74 additions & 0 deletions hawkbit-repository/hawkbit-repository-jpa-eclipselink/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!--
Copyright (c) 2015 Bosch Software Innovations GmbH and others
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
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.hawkbit</groupId>
<version>${revision}</version>
<artifactId>hawkbit-repository</artifactId>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>hawkbit-repository-jpa-eclipselink</artifactId>
<name>hawkBit :: Repository :: JPA EclipseLink Vendor</name>

<properties>
<apt.source.dir>${project.build.directory}/generated-sources/apt/</apt.source.dir>
</properties>

<dependencies>
<dependency>
<groupId>org.eclipse.hawkbit</groupId>
<artifactId>hawkbit-repository-jpa-api</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Static class generation -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>${eclipselink.version}</version>
</dependency>
</dependencies>
<!-- -->
<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.hibernate.orm.tooling</groupId>-->
<!-- <artifactId>hibernate-enhance-maven-plugin</artifactId>-->
<!-- <version>${hibernate.version}</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <configuration>-->
<!-- &lt;!&ndash;-->
<!-- enableLazyInitialization and enableDirtyTracking enabled by default.-->
<!-- anyway, in future they couldn't be set to false anyway-->
<!-- &ndash;&gt;-->
<!-- <enableAssociationManagement>false</enableAssociationManagement>-->
<!-- <enableExtendedEnhancement>false</enableExtendedEnhancement>-->
<!-- </configuration>-->
<!-- <goals>-->
<!-- <goal>enhance</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* 3.b.) the cause is not an {@link SQLException} and as a result cannot be
* mapped.
*/
public class HawkbitEclipseLinkJpaDialect extends EclipseLinkJpaDialect {
class HawkbitEclipseLinkJpaDialect extends EclipseLinkJpaDialect {

@Serial
private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -99,4 +99,4 @@ private static SQLException findSqlException(final RuntimeException jpaSystemExc

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,24 @@ public class Jpa {

public enum JpaVendor {
ECLIPSELINK,
HIBERNATE // NOT SUPPORTED!
HIBERNATE
}

public static final JpaVendor JPA_VENDOR = JpaVendor.ECLIPSELINK;

public static char NATIVE_QUERY_PARAMETER_PREFIX = switch (JPA_VENDOR) {
case ECLIPSELINK -> '?';
case HIBERNATE -> ':';
};
public static final char NATIVE_QUERY_PARAMETER_PREFIX = '?';

public static <T> String formatNativeQueryInClause(final String name, final List<T> list) {
return switch (Jpa.JPA_VENDOR) {
case ECLIPSELINK -> formatEclipseLinkNativeQueryInClause(IntStream.range(0, list.size()).mapToObj(i -> name + "_" + i).toList());
case HIBERNATE -> ":" + name;
};
return formatEclipseLinkNativeQueryInClause(IntStream.range(0, list.size()).mapToObj(i -> name + "_" + i).toList());
}

public static <T> void setNativeQueryInParameter(final Query deleteQuery, final String name, final List<T> list) {
if (Jpa.JPA_VENDOR == Jpa.JpaVendor.ECLIPSELINK) {
for (int i = 0, len = list.size(); i < len; i++) {
deleteQuery.setParameter(name + "_" + i, list.get(i));
}
} else if (Jpa.JPA_VENDOR == Jpa.JpaVendor.HIBERNATE) {
deleteQuery.setParameter(name, list);
for (int i = 0, len = list.size(); i < len; i++) {
deleteQuery.setParameter(name + "_" + i, list.get(i));
}
}

private static String formatEclipseLinkNativeQueryInClause(final Collection<String> elements) {
return "?" + String.join(",?", elements);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
*
* 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.repository.jpa;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
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.EclipseLinkJpaDialect;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.jta.JtaTransactionManager;

/**
* General EclipseLink configuration for hawkBit's Repository.
*/
@Configuration
public class JpaConfiguration extends JpaBaseConfiguration {

protected JpaConfiguration(
final DataSource dataSource, final JpaProperties properties,
final ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) {
super(dataSource, properties, jtaTransactionManagerProvider);
}

/**
* {@link MultiTenantJpaTransactionManager} bean.
*
* @return a new {@link PlatformTransactionManager}
* @see org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration#transactionManager(ObjectProvider)
*/
@Override
@Bean
public PlatformTransactionManager transactionManager(final ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
return new MultiTenantJpaTransactionManager();
}

@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new EclipseLinkJpaVendorAdapter() {

private final HawkbitEclipseLinkJpaDialect jpaDialect = new HawkbitEclipseLinkJpaDialect();

@Override
public EclipseLinkJpaDialect getJpaDialect() {
return jpaDialect;
}
};
}

@Override
protected Map<String, Object> getVendorProperties() {
final Map<String, Object> properties = new HashMap<>(7);
// Turn off dynamic weaving to disable LTW lookup in static weaving mode
properties.put(PersistenceUnitProperties.WEAVING, "false");
// needed for reports
properties.put(PersistenceUnitProperties.ALLOW_NATIVE_SQL_QUERIES, "true");
// flyway
properties.put(PersistenceUnitProperties.DDL_GENERATION, "none");
// Embed into hawkBit logging
properties.put(PersistenceUnitProperties.LOGGING_LOGGER, "JavaLogger");
// Ensure that we flush only at the end of the transaction
properties.put(PersistenceUnitProperties.PERSISTENCE_CONTEXT_FLUSH_MODE, "COMMIT");
// Enable batch writing
properties.put(PersistenceUnitProperties.BATCH_WRITING, "JDBC");
// Batch size
properties.put(PersistenceUnitProperties.BATCH_WRITING_SIZE, "500");
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.repository.jpa.configuration;
package org.eclipse.hawkbit.repository.jpa;

import java.io.Serial;
import java.util.Objects;
Expand All @@ -28,7 +28,7 @@
* {@link JpaTransactionManager} that sets the {@link TenantAware#getCurrentTenant()} in the eclipselink session. This has
* to be done in eclipselink after a {@link Transaction} has been started.
*/
public class MultiTenantJpaTransactionManager extends JpaTransactionManager {
class MultiTenantJpaTransactionManager extends JpaTransactionManager {

@Serial
private static final long serialVersionUID = 1L;
Expand Down
Loading

0 comments on commit 5eb9243

Please sign in to comment.