4

Assuming that (using JPA) I have an entity with an id:

...
@Id
@TableGenerator(name = "EVENT_GEN",
                table = "SEQUENCES",
                pkColumnName = "SEQ_NAME",
                valueColumnName = "SEQ_NUMBER",
                pkColumnValue = "ID_SEQUENCE",
                allocationSize=1)
private Long id;
...

how can I declare a custom annotation so the above id mapping will be :

@CustomIdAnnotation
private Long id

May be something like this SO answer.

O.Badr
  • 2,853
  • 2
  • 27
  • 36
  • 2
    JPA 2.2 will support "meta-annotations", and DataNucleus JPA already provides that http://www.datanucleus.org/products/accessplatform_5_1/jpa/annotations.html#meta_annotations No idea if Hibernate supports that ... – Neil Stockton Jul 21 '17 at 14:37

2 Answers2

2

As Neil Stockton mention, the meta annotation will probably be a part of next JPA version 2.2.

And for now, with JPA 2.1, I can use @Embeddable class for both id (@EmbeddedId) and non-id field (@Embedded)

Just to note that for @Embeddable, I can use a generic class, so it will be useful for any type + I can easily override my column attributes:

@Embeddable
@Getter @Setter @NoArgsConstructor // Lombok library
public class EmbeddableGeneric<T> {
    @Column 
    // other annotations
    T myField;
    
    ...
}

and in my entity class:

@Entity
@Getter @Setter @NoArgsConstructor // You know now what's this!
public class Person {

    @Id
    @GeneratedValue
    private Long id;

    @Embedded
    @AttributeOverride(name = "myField", column = @Column(name = "STRING_FIELD"))
    private EmbeddableGeneric<String> myString;

...
}

Let's wait for JPA 2.2 to overcome this verbosity.

Dario Seidl
  • 4,140
  • 1
  • 39
  • 55
O.Badr
  • 2,853
  • 2
  • 27
  • 36
  • The link to the GitHub issue link here is outdated. Java EE has been deprecated, and the Eclipse Foundation has taken on the task of furthering its projects and specifications as part of [EE4J](https://projects.eclipse.org/projects/ee4j). Here's the new location to track progress on this issue: https://github.com/eclipse-ee4j/jpa-api/issues/43 – Ethan Radford Apr 08 '19 at 18:39
0

Theoretically it should be something like this:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.persistence.Id;
import javax.persistence.TableGenerator;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CustomIdAnnotation {

    TableGenerator generator() default @TableGenerator(name = "EVENT_GEN", 
            table = "SEQUENCES", 
            pkColumnName = "SEQ_NAME", 
            valueColumnName = "SEQ_NUMBER", 
            pkColumnValue = "ID_SEQUENCE", 
            allocationSize = 1);

    Id id();
}

However, I think that this will not work because the persistence provider (Hibernate, EclipseLink,etc..) processes annotations of package javax.persistence.* + provider specific annotations directly in your entity class. So this may work if you plan to write your own persistence provider. (implementing JSR-000338 JPA 2.1 specification)

Veli Örnek
  • 79
  • 1
  • 4
  • I don't think it's a good idea to put your id inside your custom annotation, the idea is to group my verbose and duplicated JPA annotations in a signle one. – O.Badr Aug 10 '17 at 11:07