I have a large number of Java classes using JPA property access instead of field access, and converting them to use field access (moving the annotations from getters to fields) is time consuming. Re-generating them from database tables is also not an option.
So I was looking at using IDEA's structural search and replace to try move the annotations automatically. I'm wanting to change a class that looks like this:
@Entity
@Table
public class Make {
private Long id;
private String makeCode;
...other properties, with more involved JPA annotations
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(length = 10, unique = true)
public String getMakeCode() {
return makeCode;
}
public void setMakeCode(String makeCode) {
this.makeCode = makeCode;
}
...
}
to one that looks like this:
@Entity
@Table
public class Make {
@Id
@GeneratedValue
private Long id;
@Column(length = 10, unique = true)
private String makeCode;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMakeCode() {
return makeCode;
}
public void setMakeCode(String makeCode) {
this.makeCode = makeCode;
}
...
}
From there I'll be able to do additional refactoring, but I'm finding that moving the annotations manually is very time-consuming.
I've played around with structural search & replace, but its quite dense and lacking documentation, so I'd appreciate any tips on how to configure the different templates.
I imagine it would be something like:
find all methods starting with "get" that are annotated with at least one
javax.persistence.*
annotation, along with a matching private field, and replace with the same field and method, but move the annotations from method to field.
I'm just struggling to put that into the structural search expressions.