Skip to content

Conversation

evie-lau
Copy link
Contributor

What's changed?

Add new recipe and tests for OpenJPA -> EclipseLink migration

Remove @Id annotation if...

  • parent class is @Embeddable and
  • parent class referenced by @EmbeddedId

What's your motivation?

https://wiki.eclipse.org/EclipseLink/Examples/JPA/Migration/OpenJPA/Mappings#.40EmbeddedId_and_.40Id_in_the_Same_Entity

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@evie-lau evie-lau requested a review from timtebeek February 13, 2024 20:54
@evie-lau evie-lau added the recipe Recipe requested label Feb 13, 2024
Copy link
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Implementation looks perfect to me; great to see! I've done some very minor touch ups to the docs, code highlighting inside text blocks, and formatting. Good to merge if you ask me, but let's wait for @cjobinabo to return before we merge.


import static org.openrewrite.java.Assertions.java;

class RemoveEmbeddableIdTest implements RewriteTest {
Copy link
Contributor

@cjobinabo cjobinabo Mar 6, 2024

Choose a reason for hiding this comment

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

It would be good to include the following scenarios:

  • Class1 annotated with @Entity and defines a field of type Class2 annotated with @Embedded. Then Class2's definition is annotated with @Embeddable and defines a field of any type annotated with @Id
  • @Embeddable is used on a sub-class where this sub-class uses @Id

Copy link
Contributor Author

@evie-lau evie-lau Mar 15, 2024

Choose a reason for hiding this comment

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

Does this recipe also have to handle the @Embedded case? The rule only mentioned @EmbeddableId.
Otherwise if you meant @EmbeddableId, is the first case not the same as the first test, the one annotated with @DocumentExample?

Copy link
Contributor Author

@evie-lau evie-lau Mar 15, 2024

Choose a reason for hiding this comment

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

In the second case, is Class1 still defining Class2 with @EmbeddableId?
And should the recipe be expected to run on Subclass2 which extends Class2?

github-actions[bot]

This comment was marked as outdated.

import javax.persistence.Entity;
@Entity
public class MainEntity {
Copy link
Contributor

@cjobinabo cjobinabo Mar 19, 2024

Choose a reason for hiding this comment

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

I should have been clearer about describing the sub-class test case I was looking for. Something like this was what I had in mind:

              import javax.persistence.Embeddable;
              import javax.persistence.Id;
              public class SomeClass {
                  @Embeddable
                  public class EmbeddableObject {
                      @Id
                      private int field;
                  }
              }

Without calling the super class you prevent the code the from branching any deeper.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If SomeClass also had it's own @Id annotated field, should that be left unchanged by this recipe?

Copy link
Contributor Author

@evie-lau evie-lau Apr 1, 2024

Choose a reason for hiding this comment

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

Conversely, if the outer SomeClass is @Embeddable, has an @Id field, and has an inner class without @Embeddable and an inner field with @Id, should that inner field annotation be removed as well?

Although not sure if this is easily feasible. Current behavior is to remove @Id from current class and all inner classes from there.

Copy link
Contributor

@cjobinabo cjobinabo 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 making the updates

@cjobinabo cjobinabo merged commit 39b39bd into openrewrite:main Apr 10, 2024
@evie-lau evie-lau deleted the removeEmbeddableId branch May 30, 2025 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

recipe Recipe requested

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants