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

Account for case differences in DBPM contributor #6698

Merged
merged 4 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hapi-fhir-jpa-hibernate-services/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir</artifactId>
<artifactId>hapi-deployable-pom</artifactId>
<version>7.7.18-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>

<artifactId>hapi-fhir-jpa-hibernate-services</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* #%L
* HAPI FHIR JPA Hibernate Services
* %%
* Copyright (C) 2014 - 2025 Smile CDR, Inc.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package ca.uhn.hapi.fhir.sql.hibernatesvc;

import ca.uhn.fhir.context.ConfigurationException;
Expand Down Expand Up @@ -42,8 +61,11 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -109,7 +131,7 @@
public class DatabasePartitionModeIdFilteringMappingContributor
implements org.hibernate.boot.spi.AdditionalMappingContributor {

private final Set<String> myQualifiedIdRemovedColumnNames = new HashSet<>();
private final Set<TableAndColumnName> myQualifiedIdRemovedColumnNames = new HashSet<>();

/**
* Constructor
Expand Down Expand Up @@ -182,14 +204,15 @@ private void removePartitionedIdColumnsFromMetadata(
filterPartitionedIdsFromCompositeComponents(c);
}

for (Map.Entry<String, PersistentClass> nextEntry :
theMetadata.getEntityBindingMap().entrySet()) {
PersistentClass entityPersistentClass = nextEntry.getValue();
for (String nextEntityName :
new TreeSet<>(theMetadata.getEntityBindingMap().keySet())) {

PersistentClass entityPersistentClass =
theMetadata.getEntityBindingMap().get(nextEntityName);
Table table = entityPersistentClass.getTable();
for (ForeignKey foreignKey : table.getForeignKeys().values()) {
// Adjust relations with local filtered columns (e.g. ManyToOne)
filterPartitionedIdsFromLocalFks(
theClassLoaderService, theMetadata, foreignKey, table, nextEntry.getKey());
filterPartitionedIdsFromLocalFks(theClassLoaderService, theMetadata, foreignKey, table, nextEntityName);
}

for (Property property : entityPersistentClass.getProperties()) {
Expand Down Expand Up @@ -249,15 +272,16 @@ private void filterPartitionedIdsFromIdClassPks(
if (remove != null) {
iter.remove();
idRemovedColumns.addAll(property.getColumns());
idRemovedColumnNames.addAll(
property.getColumns().stream().map(Column::getName).collect(Collectors.toSet()));
idRemovedColumnNames.addAll(property.getColumns().stream()
.map(c -> c.getName().toUpperCase(Locale.ROOT))
.collect(Collectors.toSet()));
property.getColumns().stream()
.map(theColumn -> table.getName() + "#" + theColumn.getName())
.map(theColumn -> new TableAndColumnName(table.getName(), theColumn.getName()))
.forEach(myQualifiedIdRemovedColumnNames::add);
idRemovedProperties.add(property.getName());

for (Column next : entityPersistentClass.getTable().getColumns()) {
if (idRemovedColumnNames.contains(next.getName())) {
if (idRemovedColumnNames.contains(next.getName().toUpperCase(Locale.ROOT))) {
next.setNullable(true);
}
}
Expand Down Expand Up @@ -302,15 +326,17 @@ private void filterPartitionedIdsFromCompositeComponents(Component c) {

jakarta.persistence.Column column = field.getAnnotation(jakarta.persistence.Column.class);
String columnName = column.name();
myQualifiedIdRemovedColumnNames.add(tableName + "#" + columnName);
myQualifiedIdRemovedColumnNames.add(new TableAndColumnName(tableName, columnName));

PrimaryKey primaryKey = c.getTable().getPrimaryKey();
primaryKey
.getColumns()
.removeIf(t -> myQualifiedIdRemovedColumnNames.contains(tableName + "#" + t.getName()));
.removeIf(t -> myQualifiedIdRemovedColumnNames.contains(
new TableAndColumnName(tableName, t.getName())));

for (Column nextColumn : c.getTable().getColumns()) {
if (myQualifiedIdRemovedColumnNames.contains(tableName + "#" + nextColumn.getName())) {
if (myQualifiedIdRemovedColumnNames.contains(
new TableAndColumnName(tableName, nextColumn.getName()))) {
nextColumn.setNullable(true);
}
}
Expand Down Expand Up @@ -357,13 +383,15 @@ private void filterPartitionedIdsFromLocalFks(
ToOne manyToOne = (ToOne) value;
Set<String> columnNamesToRemoveFromFks = filterPropertiesFromToOneRelationship(
theClassLoaderService, theMetadata, theTable, theEntityTypeName, manyToOne);
removeColumns(theForeignKey.getColumns(), t1 -> columnNamesToRemoveFromFks.contains(t1.getName()));
removeColumns(
theForeignKey.getColumns(),
t1 -> columnNamesToRemoveFromFks.contains(t1.getName().toUpperCase(Locale.ROOT)));
} else {

theForeignKey
.getColumns()
.removeIf(t -> myQualifiedIdRemovedColumnNames.contains(
theForeignKey.getReferencedTable().getName() + "#" + t.getName()));
.removeIf(t -> myQualifiedIdRemovedColumnNames.contains(new TableAndColumnName(
theForeignKey.getReferencedTable().getName(), t.getName())));
}
}

Expand All @@ -384,16 +412,20 @@ private Set<String> filterPropertiesFromToOneRelationship(
Set<String> columnNamesToRemoveFromFks =
determineFilteredColumnNamesInForeignKey(entityType, propertyName, targetTableName);

removeColumns(manyToOne.getColumns(), t1 -> columnNamesToRemoveFromFks.contains(t1.getName()));
removeColumns(
manyToOne.getColumns(),
t1 -> columnNamesToRemoveFromFks.contains(t1.getName().toUpperCase(Locale.ROOT)));

columnNamesToRemoveFromFks.forEach(t -> myQualifiedIdRemovedColumnNames.add(theTable.getName() + "#" + t));
columnNamesToRemoveFromFks.forEach(
t -> myQualifiedIdRemovedColumnNames.add(new TableAndColumnName(theTable.getName(), t)));
return columnNamesToRemoveFromFks;
}

private void filterPartitionedIdsFromUniqueConstraints(UniqueKey uniqueKey, Table table) {
uniqueKey
.getColumns()
.removeIf(t -> myQualifiedIdRemovedColumnNames.contains(table.getName() + "#" + t.getName()));
.removeIf(t ->
myQualifiedIdRemovedColumnNames.contains(new TableAndColumnName(table.getName(), t.getName())));
}

private void filterPartitionedIdsFromRemoteFks(PersistentClass entityPersistentClass) {
Expand All @@ -407,8 +439,8 @@ private void filterPartitionedIdsFromRemoteFks(PersistentClass entityPersistentC

dependantValue
.getColumns()
.removeIf(t -> myQualifiedIdRemovedColumnNames.contains(
propertyValueBag.getCollectionTable().getName() + "#" + t.getName()));
.removeIf(t -> myQualifiedIdRemovedColumnNames.contains(new TableAndColumnName(
propertyValueBag.getCollectionTable().getName(), t.getName())));
}
}
}
Expand All @@ -429,8 +461,9 @@ private Set<String> determineFilteredColumnNamesInForeignKey(
if (isBlank(targetColumnName)) {
targetColumnName = sourceColumnName;
}
if (myQualifiedIdRemovedColumnNames.contains(theTargetTableName + "#" + targetColumnName)) {
columnNamesToRemoveFromFks.add(sourceColumnName);
if (myQualifiedIdRemovedColumnNames.contains(
new TableAndColumnName(theTargetTableName, targetColumnName))) {
columnNamesToRemoveFromFks.add(sourceColumnName.toUpperCase(Locale.ROOT));
}
}
}
Expand All @@ -450,13 +483,15 @@ private static void removeColumnsFromIndexes(
for (PartitionedIndex partitionedIndex : partitionedIndexes.value()) {
String indexName = partitionedIndex.name();
Set<String> columnNames = Set.of(partitionedIndex.columns());
assert columnNames.stream().allMatch(t -> t.equals(t.toUpperCase(Locale.ROOT)));

Index index = table.getIndex(indexName);
if (index != null) {

List<Selectable> selectables = getFieldValue(index, "selectables");
for (Iterator<Selectable> iter = selectables.iterator(); iter.hasNext(); ) {
Column next = (Column) iter.next();
if (!columnNames.contains(next.getName())) {
if (!columnNames.contains(next.getName().toUpperCase(Locale.ROOT))) {
iter.remove();
}
}
Expand Down Expand Up @@ -552,4 +587,33 @@ private static Class<?> getType(ClassLoaderService theClassLoaderService, String
Validate.notNull(entityType, "Could not load type: %s", theEntityTypeName);
return entityType;
}

private static class TableAndColumnName {
private final String myTableName;
private final String myColumnName;
private final int myHashCode;

private TableAndColumnName(String theTableName, String theColumnName) {
myTableName = theTableName.toUpperCase(Locale.ROOT);
myColumnName = theColumnName.toUpperCase(Locale.ROOT);
myHashCode = Objects.hash(myTableName, myColumnName);
}

@Override
public boolean equals(Object theO) {
if (!(theO instanceof TableAndColumnName)) return false;
TableAndColumnName that = (TableAndColumnName) theO;
return Objects.equals(myTableName, that.myTableName) && Objects.equals(myColumnName, that.myColumnName);
}

@Override
public int hashCode() {
return myHashCode;
}

@Override
public String toString() {
return "[" + myTableName + "#" + myColumnName + "]";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* #%L
* HAPI FHIR JPA Hibernate Services
* %%
* Copyright (C) 2014 - 2025 Smile CDR, Inc.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package ca.uhn.hapi.fhir.sql.hibernatesvc;

import org.hibernate.service.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* #%L
* HAPI FHIR JPA Hibernate Services
* %%
* Copyright (C) 2014 - 2025 Smile CDR, Inc.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package ca.uhn.hapi.fhir.sql.hibernatesvc;

import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* #%L
* HAPI FHIR JPA Hibernate Services
* %%
* Copyright (C) 2014 - 2025 Smile CDR, Inc.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package ca.uhn.hapi.fhir.sql.hibernatesvc;

import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* #%L
* HAPI FHIR JPA Hibernate Services
* %%
* Copyright (C) 2014 - 2025 Smile CDR, Inc.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package ca.uhn.hapi.fhir.sql.hibernatesvc;

import java.lang.annotation.ElementType;
Expand Down
1 change: 0 additions & 1 deletion hapi-fhir-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-deployable-pom</artifactId>
<version>7.7.18-SNAPSHOT</version>

<relativePath>../hapi-deployable-pom/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import jakarta.persistence.OneToMany;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

Expand Down Expand Up @@ -65,20 +66,23 @@ public class TermConceptMapGroup extends BasePartitionable implements Serializab
value = {
@JoinColumn(
name = "CONCEPT_MAP_PID",
insertable = true,
insertable = false,
updatable = false,
nullable = false,
referencedColumnName = "PID"),
@JoinColumn(
name = "PARTITION_ID",
referencedColumnName = "PARTITION_ID",
insertable = true,
insertable = false,
updatable = false,
nullable = false)
},
foreignKey = @ForeignKey(name = "FK_TCMGROUP_CONCEPTMAP"))
private TermConceptMap myConceptMap;

@Column(name = "CONCEPT_MAP_PID", nullable = false)
private Long myConceptMapPid;

@Column(name = "SOURCE_URL", nullable = false, length = TermCodeSystem.MAX_URL_LENGTH)
private String mySource;

Expand Down Expand Up @@ -109,6 +113,8 @@ public TermConceptMap getConceptMap() {

public TermConceptMapGroup setConceptMap(TermConceptMap theTermConceptMap) {
myConceptMap = theTermConceptMap;
myConceptMapPid = theTermConceptMap.getId();
Validate.notNull(myConceptMapPid, "Concept map pid must not be null");
setPartitionId(theTermConceptMap.getPartitionId());
return this;
}
Expand Down
Loading
Loading