-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for native query for multiple JPA vendors (#2129)
Signed-off-by: Avgustin Marinov <[email protected]>
- Loading branch information
1 parent
b9c10ac
commit e0d5d4e
Showing
10 changed files
with
112 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...pository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/Jpa.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright (c) 2024 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.repository.jpa; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
import jakarta.persistence.Query; | ||
|
||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE) | ||
public class Jpa { | ||
|
||
public enum JpaVendor { | ||
ECLIPSELINK, | ||
HIBERNATE // NOT SUPPORTED! | ||
} | ||
|
||
public static final JpaVendor JPA_VENDOR = JpaVendor.ECLIPSELINK; | ||
|
||
public static char NATIVE_QUERY_PARAMETER_PREFIX = switch (JPA_VENDOR) { | ||
case ECLIPSELINK -> '?'; | ||
case HIBERNATE -> ':'; | ||
}; | ||
|
||
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; | ||
}; | ||
} | ||
|
||
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); | ||
} | ||
} | ||
|
||
private static String formatEclipseLinkNativeQueryInClause(final Collection<String> elements) { | ||
return "?" + String.join(",?", elements); | ||
} | ||
} |
20 changes: 0 additions & 20 deletions
20
...hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaConstants.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.