JPersist Persistence Tools Core API Reference
Persistence Tools Core API & SPI
The Persistence Tools API module (io.github.jpersist.tools.api) provides
vendor-agnostic abstractions and Service Provider Interfaces (SPI) for build-time validation,
entity model linting, query parsing, schema verification, offline DDL export, and bytecode enhancement
of JPA persistence artifacts.
By decoupling application tooling and build plugins from specific JPA provider implementations, this module enables consistent build-time analysis and schema management across different persistence specifications (e.g. Jakarta Persistence JPA 3.x and legacy Java Persistence JPA 2.x).
Architecture & Key Contracts
The API is divided into two primary package layers:
1. Public API Contracts (persistence.tools)
Defines the consumer-facing interfaces and data models for persistence tooling:
-
Enhancer— Transforms compiled entity class bytecode at build time to incorporate runtime optimizations such as lazy-loading proxies, inline dirty tracking, and bidirectional association management. -
EntityModelLinter— Statically inspects the JPA entity metamodel at build time to detect mapping anti-patterns, structural errors, and performance pitfalls. -
LintDiagnostic— Immutable diagnostic record encapsulating a single linting finding (severity, rule ID, class name, attribute name, and descriptive message). -
LintSeverity— Enumeration classifying diagnostic severity levels (ERROR,WARNING, orINFO). -
NamedObjectParser— Validates named JPQL queries and stored procedure declarations against the entity metamodel, returning rich metadata descriptors. -
NamedObjectDescriptor— Immutable descriptor encapsulating the validation results, target type, query/procedure name, and extracted parameter definitions. -
NamedObjectType— Enumeration classifying named persistence objects (QUERYorSTORED_PROCEDURE). -
SchemaExporter— Exports offline DDL scripts (CREATE,DROP, orBOTH) from entity metadata without requiring a live database connection. -
SchemaExporter.Action— Enumeration defining the DDL generation scope. -
SchemaValidator— Validates the entity metamodel against a live database schema via JDBC connections. -
PersistenceLinter— Common marker interface for all persistence linting components.
2. Service Provider Interfaces (persistence.tools.spi)
Defines the factory interfaces and runtime discovery mechanisms implemented by provider modules:
-
PersistenceServiceProvider— Common root SPI for version-to-namespace mapping andServiceLoader-based factory resolution. -
EnhancerProvider— Factory SPI for discovering and instantiating namespace-specific bytecode enhancers with default or customized enhancement settings. -
EntityModelLinterFactory— Factory SPI for discovering and instantiating entity model linters with default or customized configuration settings. -
NamedObjectParserFactory— Factory SPI for creating named object query and procedure parsers with default or custom configuration settings. -
SchemaValidatorFactory— Factory SPI for creating database schema validators with default or custom configuration settings. -
SchemaExporterFactory— Factory SPI for creating offline DDL schema exporters with default or custom dialect and formatting settings. -
PersistenceLinterFactory— Generic base factory SPI for discovering linter factory subtypes.
Service Discovery and Usage
Custom Configuration Settings
Persistence service provider factories (EnhancerProvider,
EntityModelLinterFactory, NamedObjectParserFactory,
SchemaValidatorFactory, and SchemaExporterFactory)
overload their factory methods with custom settings parameters. This feature enables consumers to override
default service implementation settings when instantiating tools, while also supporting convenient overloads
that use default settings when custom configuration is omitted:
-
EnhancerProvider.getEnhancer(java.util.Collection, java.util.Map)andEnhancerProvider.getEnhancer(java.util.Collection) -
EntityModelLinterFactory.newEntityModelLinter(java.util.List, java.util.Map)andEntityModelLinterFactory.newEntityModelLinter(java.util.List) -
NamedObjectParserFactory.newNamedObjectParser(java.util.List, java.util.Map)andNamedObjectParserFactory.newNamedObjectParser(java.util.List) -
newSchemaValidator(java.util.List, java.util.Map)andnewSchemaValidator(java.util.List) -
SchemaExporterFactory.newSchemaExporter(java.util.List, java.util.Map)andSchemaExporterFactory.newSchemaExporter(java.util.List)
Bytecode Enhancement
Obtain an enhancer for a specific JPA specification version (e.g. "3.1") via
EnhancerProvider.getEnhancerProvider(String):
EnhancerProvider provider = EnhancerProvider.getEnhancerProvider("3.1");
Enhancer enhancer = provider.getEnhancer(
classpathFiles,
Map.of("enhancement.option.lazy-initialization", true,
"enhancement.option.dirty-tracking", true));
byte[] enhancedBytes = enhancer.enhance("com.example.Order", originalBytecode);
Entity Model Linting
Obtain an entity model linter factory via
EntityModelLinterFactory.newEntityModelLinterFactory(String).
Linters statically inspect compiled entity classes and report diagnostics across mapping and performance rules:
EntityModelLinterFactory factory = EntityModelLinterFactory.newEntityModelLinterFactory("3.1");
EntityModelLinter linter = factory.newEntityModelLinter(entityClassNames, Map.of("linter.strict", "true"));
List<LintDiagnostic> findings = linter.lint();
for (LintDiagnostic diagnostic : findings) {
if (diagnostic.severity() == LintSeverity.ERROR) {
System.err.println(diagnostic.ruleId() + " [" + diagnostic.className() + "]: " + diagnostic.message());
}
}
Named Query and Stored Procedure Validation
Obtain a parser factory via
NamedObjectParserFactory.newNamedObjectParserFactory(String).
Parsers can be created with default settings or with custom configuration settings:
NamedObjectParserFactory factory = NamedObjectParserFactory.newNamedObjectParserFactory("3.1");
try (NamedObjectParser parser = factory.newNamedObjectParser(entityClassNames, settings)) {
NamedObjectDescriptor queryDesc = parser.parseQuery(
"Order.findByStatus", "SELECT o FROM Order o WHERE o.status = :status");
NamedObjectDescriptor procDesc = parser.parseStoredProcedure(
"Order.archive", "sp_archive_orders", Set.of("cutoffDate"));
}
Database Schema Validation
Obtain a schema validator factory via
newSchemaValidatorFactory(String).
Validators can be created with default settings or configured with a settings map:
SchemaValidatorFactory factory = SchemaValidatorFactory.newSchemaValidatorFactory("3.1");
SchemaValidator validator = factory.newSchemaValidator(entityClassNames, settings);
validator.validateSchema(Map.of(
"driver", "org.h2.Driver",
"url", "jdbc:h2:mem:testdb",
"username", "sa",
"password", ""));
Offline DDL Schema Export
Obtain a schema exporter factory via
SchemaExporterFactory.newSchemaExporterFactory(String).
Exporters generate DDL statements without a live database connection and write the resulting scripts
to a File, OutputStream, or Writer:
SchemaExporterFactory factory = SchemaExporterFactory.newSchemaExporterFactory("3.1");
SchemaExporter exporter = factory.newSchemaExporter(
entityClassNames,
Map.of("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect",
"hibernate.format_sql", "true")
);
// Export CREATE DDL statements to a destination file
exporter.export(SchemaExporter.Action.CREATE, new File("build/schema/create.sql"));
Java Platform Module System (JPMS)
The io.github.jpersist.tools.api module descriptor exports its API packages and declares
ServiceLoader dependencies:
exports persistence.tools;exports persistence.tools.spi;uses persistence.tools.spi.PersistenceServiceProvider;uses persistence.tools.spi.PersistenceLinterFactory;uses persistence.tools.spi.EntityModelLinterFactory;uses persistence.tools.spi.NamedObjectParserFactory;uses persistence.tools.spi.SchemaValidatorFactory;uses persistence.tools.spi.SchemaExporterFactory;uses persistence.tools.spi.EnhancerProvider;
Package Summary
persistence.tools- Public contracts for persistence bytecode enhancement, entity model linting, query parsing, schema validation, and offline DDL schema export.
persistence.tools.spi- Service Provider Interfaces (SPI) and dynamic discovery mechanisms for persistence tooling providers.