Skip to content

Conversation

evie-lau
Copy link
Contributor

What's changed?

New recipe for OpenJPA -> EclipseLink migration
If class is tagged with @Entity or @MappedSuperclass, add default no-arg constructor if missing.

What's your motivation?

https://wiki.eclipse.org/EclipseLink/Examples/JPA/Migration/OpenJPA/Mappings#No-arg_Constructor

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 and others added 2 commits February 14, 2024 13:07
…nstructorToEntityClassTest.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…structorToEntityClass.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek timtebeek changed the title New recipe to add default constructor to Entity class Add default constructor to Entity class Feb 14, 2024
@evie-lau evie-lau marked this pull request as ready for review February 15, 2024 07:25
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.

Great work once again! Good to see you worked out the issue with context sensitivity when injecting a constructor. Indeed that'll be down to how we generate and extract the code snippet from JavaTemplate. If you're ever curious what gets generated you can add this little snippet to the code to get the generated template.
image

class __P__ {
    static native <T> T p();
    static native <T> T[] arrp();
    static native boolean booleanp();
    static native byte bytep();
    static native char charp();
    static native double doublep();
    static native int intp();
    static native long longp();
    static native short shortp();
    static native float floatp();
}

class __M__ {
    static native Object any(Object o);

    static native Object any(java.util.function.Predicate<Boolean> o);

    static native <T> Object anyT();
}

public class MissingNoArgConstructorEntity {
    private int id;

    public MissingNoArgConstructorEntity(int id) {
    }

    /*__TEMPLATE__*/
    public MissingNoArgConstructorEntity() {
    }/*__TEMPLATE_STOP__*/
}

For constructors specifically we need that context sensitivity, as otherwise the generated template does not compile.
image

Hope that helps you on your next one!

@timtebeek timtebeek requested a review from cjobinabo February 15, 2024 10:27
@timtebeek timtebeek added the recipe Recipe requested label Feb 15, 2024
@timtebeek
Copy link
Member

Once again we'll wait for @cjobinabo to merge when convenient there! :)

github-actions[bot]

This comment was marked as resolved.

@evie-lau
Copy link
Contributor Author

Reverted recent change because @Entity annotation can never be used on an inner class

@cjobinabo cjobinabo merged commit c71b02c into openrewrite:main Apr 16, 2024
@evie-lau evie-lau deleted the entityDefaultConstructor 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