Skip to content

Commit

Permalink
adding fetch configuration option to hibernate-orm module
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrzywanski committed Sep 14, 2020
1 parent 1f609b0 commit 68a1dd2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
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 All @@ -194,6 +208,7 @@ public static class HibernateOrmConfigPersistenceUnitDialect {
* JavaDoc].
*
* [NOTE]
*
* ====
* Not all the dialects are supported in GraalVM native executables: we currently provide driver extensions for
* PostgreSQL,
Expand Down Expand Up @@ -381,4 +396,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 @@ -741,14 +742,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 @@ -878,6 +882,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

0 comments on commit 68a1dd2

Please sign in to comment.