-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[FEATURE #3437] @JpaAssociationSync annotation for bidirectional entity associations #3692
base: master
Are you sure you want to change the base?
Conversation
We're going to look at this a bit later; one issue is that both roel and I thoroughly dislike JPA (not in the sense 'it's a bad tool', more in the sense: "The community (vastly) overuse it, and it tends to be 'missold' as a way to do SQL in java. It is not. It is a way to do object persistence in a crappy way in java, quite nice if you have no plans to ever actually treat the resulting persisted data as a database, let alone optimize the SQL behind it, because if thats on the horizon, just use JDBI, JOOQ, or some other abstraction that doesn't effectively yell out "SQL scares me!"'. Hey, we know we're opinionated and these opinions can be a bit out there. JPA is used by a lot of people, and features that help using it have a place in Project Lombok. The point is more: We might need some extra help (and certainly some more time) to fully evaluate this PR. Good job on including examples. We've had a quick look and we don't get it. Which, no doubt, is due to our lack of intricate understanding of the vagaries of JPA. We're going to try to find some time to evaluate this PR properly. Thanks for the contribution, and apologies that we need some time to evaluate it :) |
okay, no problem |
it would be really great if @vladmihalcea could help us :) |
@rukins any idea vlad is gonna chime in? I'm guessing no :( |
I'd like to add that a tooling for this problem already exists in Hibernate world https://docs.jboss.org/hibernate/orm/5.6/topical/html_single/bytecode/BytecodeEnhancement.html I'd be interested how this feature behaves when used together with the plugin linked above or if any effort should be made to prevent usage of both if there are any nasty side effects. |
@ZIRAKrezovic thank you for the note. I will test it and be back with the result |
That would be an awesome feature! I understand that not everyone feels comfortable using JPA for SQL in Java. But this is the widespread reality in the Java EE/Spring software industry, and it has been a hassle to consistently handle one-to-one, one-to-many, and many-to-many relationships in a large project. Such annotations would add great value in making JPA at least a little better. It would prevent silly bugs and speed up development. And it would be really nice to have a Hibernate (non JPA) variant. |
@ZIRAKrezovic They seem to work fine with each other. But actually I have never used Bytecode Enhancement from Hibernate before, so I can't tell how it works in real world projects and how it affect performance and so on. The only disadvantage I noticed is that Hibernate BE is embedded in setters and getters, and therefore bidirectional entity synchronization only works when creating new lists and setting it, not adding or removing elements. Also there are the answer on Stackoverflow and the article from Vlad, where he said
(But it was 3 years ago) It would be great if someone who has worked with Hibernate BE could help us with this issue. |
Overview
This pull request introduces a new Lombok annotation,
@JpaAssociationSync
, designed to facilitate the synchronization of JPA associations in the context of bidirectional relationships. The annotation can be applied at both the type and field level, generating helper methods to ensure that both sides of a bidirectional relationship remain consistent.The methods is generated according to Vlad Mihalcea article
The idea is taken from #3437 and #2364.
Key Features
@JpaAssociationSync
Annotation: Provides generation of association sync methods.@JpaAssociationSync.Extra
Annotation: Provides additional configuration options.Parameters
@JpaAssociationSync
@JpaAssociationSync.Extra
@OneToOne
or@ManyToMany
associations.Example
Source
Post.java
PostComment.java
PostDetails.java
Tag.java
Delomboked
Post.java
PostComment.java, PostDetails.java and Tag.java remain as it was.
Notes
@OneToMany
and@OneToOne
and getters for@ManyToMany
are necessary on the other side of association. Perhaps later it would be nice to implement an option with field access.@OneToMany
,@OneToOne
and@ManyToMany
annotations can be used on methods, so it will be implemented later.@JpaAssociationSync
handler can't find themappedBy
parameter in the association annotation or theinverseSideFieldName
parameter in the@JpaAssociationSync.Extra
annotation on field, nothing will be generated.ECJ
, you should add thejakarta.persistence-api
dependency to yourmaven-compiler-plugin
plugin (in case of Maven) as described here.