Skip to content

Conversation

@Asanio06
Copy link

@Asanio06 Asanio06 commented Oct 25, 2025

The goal of this commit is to enable the EntityManagerFactoryBuilder to automatically detect and apply PersistenceUnitPostProcessor beans.
Currently, it is possible to use an EntityManagerFactoryBuilderCustomizer, but since there is no getter providing access to the already registered PersistenceUnitPostProcessor instances, using setPersistenceUnitPostProcessors may unintentionally remove existing ones.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 25, 2025
@Asanio06 Asanio06 force-pushed the feat/use-PersistenceUnitPostProcessor-bean branch 2 times, most recently from 0a533b5 to 702c81a Compare October 25, 2025 16:53
Copy link
Member

@snicoll snicoll left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. I've left a couple comments for your consideration.

ObjectProvider<PersistenceUnitManager> persistenceUnitManager,
ObjectProvider<EntityManagerFactoryBuilderCustomizer> customizers) {
ObjectProvider<EntityManagerFactoryBuilderCustomizer> customizers,
PersistenceUnitPostProcessor[] persistenceUnitPostProcessors) {
Copy link
Member

Choose a reason for hiding this comment

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

This should be an ObjectProvider as well.

Copy link
Author

Choose a reason for hiding this comment

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

Oh yes, The change has been made.

EntityManagerFactoryBuilderCustomizer entityManagerFactoryBuilderCustomizer() {
return (builder) -> builder.setPersistenceUnitPostProcessors(
(pui) -> pui.addManagedClassName("customized.attribute.converter.class.name"));
PersistenceUnitPostProcessor entityManagerFactoryBuilderCustomizer() {
Copy link
Member

Choose a reason for hiding this comment

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

This replaces an existing test. I think we should leave it as it is and create a new one that demonstrates the code you've changed.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, absolutely, I have created a new test.

@snicoll snicoll changed the title feat: make PersistenceUnitPostProcessor beans use automatically Auto-configure EntityManagerFactoryBuilder to apply PersistenceUnitPostProcessor beans Oct 25, 2025
@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Oct 25, 2025
@Asanio06 Asanio06 force-pushed the feat/use-PersistenceUnitPostProcessor-bean branch from 702c81a to 22a47be Compare October 25, 2025 19:17
@Asanio06 Asanio06 requested a review from snicoll October 25, 2025 19:20
@snicoll
Copy link
Member

snicoll commented Oct 26, 2025

We'll have to figure out what we want to do here. I am not 100% sure that registering them from beans is the right call as PersistenceUnitPostProcessor is from Spring Framework and has already a well-defined scope:

Implementations can be registered with a DefaultPersistenceUnitManager or via a org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.

Perhaps a more narrowed improvement would be to change EntityManagerFactoryBuilder so that it can add a PersistenceUnitPostProcessor rather than always replacing the existing list. Once this is in place, then you can use EntityManagerFactoryBuilderCustomizer and set/add a PersistenceUnitPostProcessor as you see fit (including taking the ones from the context if that works for you).

I've flagged for team attention so that we can get more feedback before any further change is made. Thanks again.

@snicoll snicoll added the for: team-attention An issue we'd like other members of the team to review label Oct 26, 2025
@Asanio06
Copy link
Author

@snicoll Indeed, adding an "add" would be very interesting. Maybe both approaches are worth adding.
Thanks for the comments, I’m waiting for your feedback on the next steps 🙂

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Oct 26, 2025
@nosan
Copy link
Contributor

nosan commented Oct 27, 2025

Perhaps a more narrowed improvement would be to change EntityManagerFactoryBuilder so that it can add a PersistenceUnitPostProcessor rather than always replacing the existing list. Once this is in place, then you can use EntityManagerFactoryBuilderCustomizer and set/add a PersistenceUnitPostProcessor as you see fit (including taking the ones from the context if that works for you).

I think adding an addPersistenceUnitPostProcessors(...) or maybe additionalPersistenceUnitPostProcessors(...) method to EntityManagerFactoryBuilder looks much better to me than registering PersistenceUnitPostProcessor instances as @Bean and then setting them on the EntityManagerFactoryBuilder, with a chance that someone could later update them using customizers.

With the customizer approach, there's only one way to apply them; with beans, there would be two - and honestly, I don't see any advantage in that.

@Asanio06 Asanio06 force-pushed the feat/use-PersistenceUnitPostProcessor-bean branch from 22a47be to 5cc0d9e Compare October 27, 2025 06:52
@Asanio06
Copy link
Author

Thanks @nosan @snicoll for feedbacks. I make the change

@wilkinsona
Copy link
Member

I'm increasingly not a fan of defining things as beans purely so that they can be registered with something else. With that in mind, I'd vote for methods on the builders and the use of a customizer.

@snicoll snicoll added type: enhancement A general enhancement and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Oct 27, 2025
@snicoll snicoll added this to the 4.x milestone Oct 27, 2025
@snicoll snicoll changed the title Auto-configure EntityManagerFactoryBuilder to apply PersistenceUnitPostProcessor beans Allow EntityManagerFactoryBuilder to also add PersistenceUnitPostProcessor instances Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants