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@GenericGenerator
can be used on class directly, I created apackage-info.java
fora.b.jpa.entity
package with the following contents;That annotates all classes under
a.b.jpa.entity
with 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
@GenericGenerator
is defined in a single location, supplying theMyGenerator
details to all my entities, if any of them wishes to use it, they just utilize@GeneratedValue
.