There are many entities in my project that utilize the same custom IdentifierGenerator for @Id generation using SpringDataJPA/Hibernate.
An example entity;
@Entity
@Table(name = "CHAIRS")
public class ChairEntity {
@Id
@GeneratedValue(generator = "myGenerator")
@GenericGenerator(strategy = "a.b.jpa.generator.MyGenerator", name = "myGenerator")
protected String id;
// rest of the entity
}
But I do not want to repeat @GenericGenerator decleration for every id field of every entity. Is there no way to hide this annotation that contains an ugly hardcoded full class name inside?
post scriptum
I was previously utilizing @MappedSuperClass with a BaseEntity class containing id field, but due to some technical reasons I had to abandon utilizing such a superclass. Obviously that is the easiest solution to this question.
Solution I came up with was a Java trick, all my entities are under
a.b.jpa.entity, and my generator ina.b.jpa.generator, and since@GenericGeneratorcan be used on class directly, I created apackage-info.javafora.b.jpa.entitypackage with the following contents;That annotates all classes under
a.b.jpa.entitywith the@GenericGenerator, which is perfect solution for my question. Essentially making it available for all entities.code of the generator;
and the entities in the following state;
Which did the trick, now the
@GenericGeneratoris defined in a single location, supplying theMyGeneratordetails to all my entities, if any of them wishes to use it, they just utilize@GeneratedValue.