0

I have multiple classes annotated with @Entity. Half of the classes have these 3 additional fields in them: specialFieldA, specialFieldB, and specialFieldC; the other half do not.

Is there a JPA way to ensure that future classes have these 3 fields in them? I.e. if a teammate wants to make another class with specialFieldA, specialFieldB, and specialFieldC in them, is there a way to enforce having these 3 fields?

I thought of using an interface, but it didn't seem like the right strategy since you can only define methods.

I can't use an abstract class because these classes already extend a parent class.

Grant Foster
  • 722
  • 2
  • 11
  • 21

2 Answers2

0

I ended up creating a separate class with just those special fields in it and annotated the class with @Embeddable. Then in my entity I included that new class as a field and put an @Embedded annotation on it.

Grant Foster
  • 722
  • 2
  • 11
  • 21
-1

Make a abstract class like Special fields have the annotation @MappedSuperClass

@MappedSuperClass
public abstract class SpecialFields {
  @Column(name = "specialFieldA")
  private String specialFieldA;

  @Column(name = "specialFieldB")
  private String specialFieldB;

  @Column(name = "specialFieldC")
  private String specialFieldC;
}

All the classes furthue you want to have these fields in it extends from this class Like

@Entity
@Table(name ="sample")
public class Sample extends SpecialFields  implements Serializable{
     //All three fields are part of this class and automatically maps to date base columns with same name..
}

Now all the classes which have these three fields must extends from SpecialFields class. and other are not.

Khalid Shah
  • 3,132
  • 3
  • 20
  • 39