Skip to content

@PersistenceModule#861

Draft
gavinking wants to merge 5 commits intojakartaee:mainfrom
gavinking:persistence-module
Draft

@PersistenceModule#861
gavinking wants to merge 5 commits intojakartaee:mainfrom
gavinking:persistence-module

Conversation

@gavinking
Copy link
Copy Markdown
Member

@gavinking gavinking commented Dec 22, 2025

This change introduces:

  • a way to declare a Java module as defining a persistence unit (or "persistence archive" in language we were using early on), that is, a package of entities and related code, along with XML mapping files, and
  • an annotation for specifying "static" configuration information which does not vary with the deployment, as an alternative to persistence.xml, and
  • a way to segregate deployment-specific information into a separate .properties file.

See #701 (comment).

NOTE: This proposal is on ice until I can get feedback from someone else who is prepared to spend time thinking carefully about what a "persistence unit" or "persistence module" is going to mean in the future. Since I have not been able to gather such feedback yet, this feature is not currently planned for JPA 4.

@gavinking gavinking added the candidate-for-4 Good candidate for JPA 4 label Dec 22, 2025
Comment on lines +191 to +194
/**
* The schema management action to be performed by generated DDL scripts.
*/
SchemaManagementAction schemaManagementScriptsAction() default NONE;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it really makes sense to have this here.

* schemaManagementDatabaseAction = VALIDATE,
* properties = {
* @PersistenceProperty(name="jakarta.persistence.jdbc.batchSize", value="20"),
* @PersistenceProperty(name="jakarta.persistence.jdbc.fetchSize", value="1000")
Copy link
Copy Markdown
Contributor

@hantsy hantsy Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the properties in other @XXXXDefinition, use a list of string that contain the property field/value is ok.

properties = {"abc=xyz", ...}

Optionally, add a propertiesMap as alternative.

propertiesMap = mapOf(...)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hantsy Well, there's various ways I could answer that question.

First: if we're inventing a minilanguage, why not go all the way and just say properties is a text block and you write:

properties = """
             jakarta.persistence.jdbc.batchSize = 20
             jakarta.persistence.jdbc.fetchSize = 1000
             """;

That's even better, no?

Second: to me this question simply proves what I've been trying to say, which is that annotations are simply a pretty uncomfortable place to stick configuration properties! Even you, who were the one who wanted this feature, don't really feel comfortable with @PersistenceProperty (which has been there since JPA 1.0, from back when we didn't really know how to write Java).

Third: well, there's an actual good answer to your question and it's my fault for posting a bad code example. The answer is that you can write:

@PersistenceProperty(name=PersistenceConfiguration.JDBC_FETCH_SIZE, value="20")

I will update the code sample to reflect that usage.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, please keep in mind that the real essence of this proposal is that your config properties are more likely to be specified in the .properties file, not in the Java code. The idea here is to decouple:

  1. persistence unit declaration, from
  2. config properties.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and:

propertiesMap = mapOf(...)

You can't have a Map as an annotation member in Java.

* }
* )
* module org.example.library { ... }
* }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new PersistenceModule depends on JPMS system?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this proposal, it depends on having a module descriptor, yes.

@gavinking gavinking removed the candidate-for-4 Good candidate for JPA 4 label Dec 24, 2025
Copy link
Copy Markdown

@OndroMih OndroMih left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a few questions to specific lines, @gavinking

persistence classes included in the persistence unit using the `class`
element of the `persistence.xml` file. See <<a12305>>.

==== Persistence Properties Files
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A question: Where do these properties file reside? For example, I have a WAR with a JAR, which is a JPA module. Do I put the properties file into the JAR, into the WAR or keep it outside and specify it at deployment time?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to what is specified here, it goes in the META-INF directory of the JAR (i.e. of the persistence unit package).

Obviously I'm anticipating a future where these properties could also be specified in a location external to the persistence unit archive. But that's not in this proposal.

persistence classes included in the persistence unit using the `class`
element of the `persistence.xml` file. See <<a12305>>.

==== Persistence Properties Files
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another question: To use the properties files, do I need to use the new PersistenceModule annotation? Or I can define a properties file also for PUs defined in persistence.xml?

Since the properties file name is tied with PU name, it should also make sense to use it with any PU, not only a PU in a persistence module, right? If yes, I think it makes sense to split this proposal about properties files into a separate PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use the properties files, do I need to use the new PersistenceModule annotation?

No, it says "a persistence unit", that is, any persistence unit.

Since the properties file name is tied with PU name, it should also make sense to use it with any PU, not only a PU in a persistence module, right?

Yes.

I think it makes sense to split this proposal about properties files into a separate PR.

As it is, it's not really especially useful without the annotation. If you're going to put properties in the META-INF directory, and you already have a persistence.xml there, you might as well just put them in persistence.xml. (Obviously it becomes more useful if they can also be sourced from somewhere else.)

@gavinking gavinking marked this pull request as draft January 1, 2026 00:45
@gavinking
Copy link
Copy Markdown
Member Author

Note that if we ever did this, the annotation should also allow specification of the info that is currently allowed in <persistence-unit-defaults>. See #237 (comment).

* mapping format, be uniquely named, and be resource-loadable
* from the application classpath.
*/
String[] mappingFileNames() default {};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following #237 (comment) - how we could add those defaults here? Would we follow with similar annotation for mapping files? Then, would make sense to separate those defaults somehow and use them directly here? Not sure about consequences ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants