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

Adding fetch configuration option to hibernate-orm module #11950

Merged
merged 1 commit into from
Sep 19, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,24 @@ public class HibernateOrmConfigPersistenceUnit {
* The size of the batches used when loading entities and collections.
*
* `-1` means batch loading is disabled. This is the default.
*
*
* @deprecated {@link #fetch} should be used to configure fetching properties.
* @asciidoclet
*/
@ConfigItem(defaultValue = "-1")
@Deprecated
public int batchFetchSize;

/**
* The maximum depth of outer join fetch tree for single-ended associations (one-to-one, many-to-one).
*
* A `0` disables default outer join fetching.
*
* @deprecated {@link #fetch} should be used to configure fetching properties.
* @asciidoclet
*/
@ConfigItem
@Deprecated
public OptionalInt maxFetchDepth;

/**
Expand Down Expand Up @@ -132,6 +138,13 @@ public class HibernateOrmConfigPersistenceUnit {
@ConfigDocSection
public HibernateOrmConfigPersistenceUnitLog log;

/**
* Fetching logic configuration.
*/
@ConfigItem
@ConfigDocSection
public HibernateOrmConfigPersistenceUnitFetch fetch;

/**
* Caching configuration
*/
Expand Down Expand Up @@ -182,7 +195,8 @@ public boolean isAnyPropertySet() {
!cache.isEmpty() ||
!secondLevelCachingEnabled ||
multitenant.isPresent() ||
multitenantSchemaDatasource.isPresent();
multitenantSchemaDatasource.isPresent() ||
fetch.isAnyPropertySet();
}

@ConfigGroup
Expand Down Expand Up @@ -404,4 +418,32 @@ public static class HibernateOrmConfigPersistenceUnitCacheMemory {
@ConfigItem
public OptionalLong objectCount;
}

@ConfigGroup
public static class HibernateOrmConfigPersistenceUnitFetch {
/**
* The size of the batches used when loading entities and collections.
*
* `-1` means batch loading is disabled. This is the default.
*
* @asciidoclet
*/
@ConfigItem(defaultValue = "-1")
public int batchSize;

/**
* The maximum depth of outer join fetch tree for single-ended associations (one-to-one, many-to-one).
*
* A `0` disables default outer join fetching.
*
* @asciidoclet
*/
@ConfigItem
public OptionalInt maxDepth;

public boolean isAnyPropertySet() {
return batchSize > 0 || maxDepth.isPresent();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -776,14 +777,17 @@ private static void producePersistenceUnitDescriptorFromConfig(
}

// Query
if (persistenceUnitConfig.batchFetchSize > 0) {
descriptor.getProperties().setProperty(AvailableSettings.DEFAULT_BATCH_FETCH_SIZE,
Integer.toString(persistenceUnitConfig.batchFetchSize));
descriptor.getProperties().setProperty(AvailableSettings.BATCH_FETCH_STYLE, BatchFetchStyle.PADDED.toString());
if (persistenceUnitConfig.fetch.batchSize > 0) {
setBatchFetchSize(descriptor, persistenceUnitConfig.fetch.batchSize);
} else if (persistenceUnitConfig.batchFetchSize > 0) {
setBatchFetchSize(descriptor, persistenceUnitConfig.batchFetchSize);
}

persistenceUnitConfig.maxFetchDepth.ifPresent(
depth -> descriptor.getProperties().setProperty(AvailableSettings.MAX_FETCH_DEPTH, String.valueOf(depth)));
if (persistenceUnitConfig.fetch.maxDepth.isPresent()) {
setMaxFetchDepth(descriptor, persistenceUnitConfig.fetch.maxDepth);
} else if (persistenceUnitConfig.maxFetchDepth.isPresent()) {
setMaxFetchDepth(descriptor, persistenceUnitConfig.maxFetchDepth);
}

persistenceUnitConfig.query.queryPlanCacheMaxSize.ifPresent(
maxSize -> descriptor.getProperties().setProperty(AvailableSettings.QUERY_PLAN_CACHE_MAX_SIZE, maxSize));
Expand Down Expand Up @@ -913,6 +917,16 @@ public static Optional<String> guessDialect(String resolvedDbKind) {
throw new ConfigurationError(error);
}

private static void setMaxFetchDepth(ParsedPersistenceXmlDescriptor descriptor, OptionalInt maxFetchDepth) {
descriptor.getProperties().setProperty(AvailableSettings.MAX_FETCH_DEPTH, String.valueOf(maxFetchDepth.getAsInt()));
}

private static void setBatchFetchSize(ParsedPersistenceXmlDescriptor descriptor, int batchFetchSize) {
descriptor.getProperties().setProperty(AvailableSettings.DEFAULT_BATCH_FETCH_SIZE,
Integer.toString(batchFetchSize));
descriptor.getProperties().setProperty(AvailableSettings.BATCH_FETCH_STYLE, BatchFetchStyle.PADDED.toString());
}

private void enhanceEntities(final JpaEntitiesBuildItem domainObjects,
BuildProducer<BytecodeTransformerBuildItem> transformers,
List<AdditionalJpaModelBuildItem> additionalJpaModelBuildItems,
Expand Down