Skip to content

Commit f1d17f5

Browse files
evie-lautimtebeek
andauthored
Use JoinColumn annotation for relationship mappings (#416)
* Use JoinColumn annotation for relationship mappings * Add failing test where other Column annotations are modified when it shouldn't be * Apply changes only to current variable declaration * Fix test case imports to wildcard * Move test to javax package * Minor polish * Remove comment Co-authored-by: Tim te Beek <[email protected]> * Update wording and names * Add test cases, add Entity check, cleanup * Add test case and fix for inner classes * Show that type is only changed for affected field * Add trailing newline --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 360281f commit f1d17f5

File tree

3 files changed

+507
-0
lines changed

3 files changed

+507
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.javax;
17+
18+
import lombok.EqualsAndHashCode;
19+
import org.openrewrite.ExecutionContext;
20+
import org.openrewrite.Preconditions;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.TreeVisitor;
23+
import org.openrewrite.java.ChangeType;
24+
import org.openrewrite.java.JavaIsoVisitor;
25+
import org.openrewrite.java.RemoveAnnotationAttribute;
26+
import org.openrewrite.java.search.FindAnnotations;
27+
import org.openrewrite.java.search.UsesType;
28+
import org.openrewrite.java.tree.J;
29+
30+
@EqualsAndHashCode(callSuper = false)
31+
public class UseJoinColumnForMapping extends Recipe {
32+
private final String JOIN_COLUMN = "javax.persistence.JoinColumn";
33+
private final String COLUMN = "javax.persistence.Column";
34+
35+
@Override
36+
public String getDisplayName() {
37+
return "`@JoinColumn` annotations must be used with relationship mappings";
38+
}
39+
40+
@Override
41+
public String getDescription() {
42+
return "In OpenJPA, when a relationship attribute has either a `@OneToOne` or a `@ManyToOne` annotation with a " +
43+
"`@Column` annotation, the `@Column` annotation is treated as a `@JoinColumn` annotation. EclipseLink " +
44+
"throws an exception that indicates that the entity class must use `@JoinColumn` instead of `@Column` " +
45+
"to map a relationship attribute.";
46+
}
47+
48+
@Override
49+
public TreeVisitor<?, ExecutionContext> getVisitor() {
50+
return Preconditions.check(
51+
Preconditions.and(
52+
new UsesType<>("javax.persistence.Entity", true),
53+
new UsesType<>(COLUMN, true),
54+
Preconditions.or(
55+
new UsesType<>("javax.persistence.OneToOne", true),
56+
new UsesType<>("javax.persistence.ManyToOne", true)
57+
)
58+
),
59+
new JavaIsoVisitor<ExecutionContext>() {
60+
@Override
61+
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
62+
// Exit if not annotated with @Column and a relationship mapping annotation
63+
if (FindAnnotations.find(multiVariable, COLUMN).isEmpty()
64+
|| (FindAnnotations.find(multiVariable, "javax.persistence.OneToOne").isEmpty()
65+
&& FindAnnotations.find(multiVariable, "javax.persistence.ManyToOne").isEmpty())) {
66+
return multiVariable;
67+
}
68+
69+
// Change @Column to @JoinColumn
70+
// The javax.persistence.Column attributes length, precision, and scale are not kept.
71+
maybeRemoveImport(COLUMN);
72+
maybeAddImport(JOIN_COLUMN);
73+
J.VariableDeclarations joinColumn = (J.VariableDeclarations) new ChangeType(COLUMN, JOIN_COLUMN, false).getVisitor().visit(multiVariable, ctx);
74+
joinColumn = (J.VariableDeclarations) new RemoveAnnotationAttribute(JOIN_COLUMN, "length").getVisitor().visit(joinColumn, ctx);
75+
joinColumn = (J.VariableDeclarations) new RemoveAnnotationAttribute(JOIN_COLUMN, "precision").getVisitor().visit(joinColumn, ctx);
76+
joinColumn = (J.VariableDeclarations) new RemoveAnnotationAttribute(JOIN_COLUMN, "scale").getVisitor().visit(joinColumn, ctx);
77+
78+
return joinColumn;
79+
}
80+
}
81+
);
82+
}
83+
}

src/main/resources/META-INF/rewrite/openjpa-to-eclipselink.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ recipeList:
2626
- org.openrewrite.java.migrate.javax.AddColumnAnnotation
2727
- org.openrewrite.java.migrate.javax.AddDefaultConstructorToEntityClass
2828
- org.openrewrite.java.migrate.javax.RemoveEmbeddableId
29+
- org.openrewrite.java.migrate.javax.UseJoinColumnForMapping

0 commit comments

Comments
 (0)