Move @Id to the superclass
All entities in the system (about 150 classes) contain their own id
property with a list of required annotations.
@Id
@JsonProperty
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "crop_id")
private Long id;
The current code looks not very flexible. For example, if we need to change GenerationType
then we have to update each class instead of a single superclass. We had to use that approach because each entity has a unique name of id column in the DB (table_name_id
). To get rid of duplicate code we could use JPA's @AttributeOverride
annotation.
Used to override the mapping of a
Basic
(whether explicit or default) property or field orId
property or field.
May be applied to an entity that extends a mapped superclass or to an embedded field or property to override a basic mapping or id mapping defined by the mapped superclass
We can move the id
field to the @MappedSuperclass
and override the mapping of id using @AttributeOverride
@Entity
@Table(name = "crop")
@Cacheable
@AttributeOverride(name = "id", column = @Column(name = "crop_id"))
@JsonIdentityInfo(scope = Crop.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Crop extends CooperatorOwnedModel implements Copyable<Crop> {
...
}