persistence icon indicating copy to clipboard operation
persistence copied to clipboard

Allow type level annotations to be used as meta-annotations

Open lukasj opened this issue 12 years ago • 18 comments

I repeatedly find myself annotating my JPA entities with the very same set of annotations:

@Entity
@EntityListeners(AuditingEntityListener.class)
class Person {

}

If both @Entity and @EntityListener were allowed to be used as meta-annotations I could collapse them into:

@Target(TYPE)
@Retention(RUNTIME)
@Entity
@EntityListeners(AuditingEntityListener.class)
@interface @AuditedEntity {

}

Resulting in:

@AuditedEntity
class Person {

}

This is in line with the meta-annotation handling CDI exposes to introduce annotation with richer semantics in annotation code. The following changes would be required.

  • Add ElementType.ANNOTATION_TYPE to the relevant annotations
  • Specify that persistence providers have to evaluate the annotations from the meta-level as well using the first one found, so that locally defined annotations would be considered first.

lukasj avatar Jan 17 '13 20:01 lukasj

  • Issue Imported From: https://github.com/javaee/jpa-spec/issues/43
  • Original Issue Raised By:@glassfishrobot
  • Original Issue Assigned To: @lukasj

lukasj avatar Aug 31 '18 16:08 lukasj

@glassfishrobot Commented Reported by oliver.gierke

lukasj avatar Jan 17 '13 20:01 lukasj

@glassfishrobot Commented @ldemichiel said: Support for this functionality is on our roadmap for Java EE 8. We should revisit this issue at that time.

lukasj avatar Jan 18 '13 01:01 lukasj

@glassfishrobot Commented @lukasj said: Why do you think that ElementType.ANNOTATION_TYPE should be added to relevant annotations? Annotations having ElementType.TYPE are already allowed to be used on annotation types.

lukasj avatar Sep 23 '15 12:09 lukasj

@glassfishrobot Commented oliver.gierke said: O.o - you're completely right. I'm quite ashamed I wasn't aware of that . There's still tow aspects though:

  • it would be cool if all annotations were usable as meta-annotations (might need to change the ticket description). The ones currently to be used with methods or fields only can't be used this way right now.
  • I think it deserves a dedicated section/sentence on the meta-annotation usage in the spec to make sure all implementors actually support it

lukasj avatar Oct 31 '15 13:10 lukasj

@glassfishrobot Commented oliver.gierke said: I've just checked Hibernate 4.3.11 and when tying to create a composed annotation with @Entity as meta-annotation, it considers the annotation type (@AuditedEntity from my original example) as entity type and complains about a missing identifier. So it seems at least Hibernate doesn't consider meta-annotations the way the should right now.

lukasj avatar Oct 31 '15 13:10 lukasj

@glassfishrobot Commented @lukasj said: check RI / EclipseLink nightly from master (2.7), it should be supported there already

as for the section in the spec - right, it has to be added and it should define which types are allowed to be used on meta annotations I'll share the 'draft' for this section either within this ticket or through users mailing list.

lukasj avatar Oct 31 '15 20:10 lukasj

@glassfishrobot Commented oliver.gierke said: Thanks for that, Lucas. Are there any consumable nightly builds of EclipseLink 2.7 somewhere?

lukasj avatar Nov 01 '15 11:11 lukasj

@glassfishrobot Commented @lukasj said: sure, https://www.eclipse.org/eclipselink/downloads/nightly.php if you prefer maven then see https://wiki.eclipse.org/EclipseLink/Maven for the repo setup

lukasj avatar Nov 01 '15 18:11 lukasj

@glassfishrobot Commented @lukasj said: btw: #109 has been implemented in RI as well

lukasj avatar Nov 01 '15 18:11 lukasj

@glassfishrobot Commented Issue-Links: is duplicated by JPA_SPEC-98

lukasj avatar Jan 17 '13 20:01 lukasj

@glassfishrobot Commented This issue was imported from java.net JIRA JPA_SPEC-43

lukasj avatar May 05 '17 06:05 lukasj

hi, I also wanted to add that it would be great to have field-level meta-annotation support.

For instance, every time I create a new JPA entity, I need to declare the id as follows:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

It would be nice to be able to create a single annotation that embeds both annotations. I would use it that way:

@MyCompanyId
private Long id;

and declare the annotation as follows:

@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public @interface MyCompanyId {
}

For it to be possible, @Id and @GeneratedValue annotations for the jakarta.persistence package would need to be updated so they include ElementType.ANNOTATION_TYPE as follows:

@Target({METHOD, FIELD,ANNOTATION_TYPE})
@Retention(RUNTIME)
public @interface Id {}

Would it make sense?

michaelisvy avatar Jan 10 '20 01:01 michaelisvy

How is the status of this? I read that that meta-annotations are supported since JPA 2.2. Are all JPA annotations (class and field level) supported in meta-annotations now?

darioseidl avatar Jun 08 '21 09:06 darioseidl

This one is open, since it was planned for JAVA EE 8. Any plans, updates on this?

emivoz avatar Jun 13 '23 07:06 emivoz

Even though this is a ten-year old feature request, with votes, I don't think it's urgent to do it in 3.2, partly because we'll need to take time to carefully consider how far it should go:

  • @EntityListeners is a no-brainer IMO, and,
  • likewise, other "model"-level annotations like @Entity and @Id seem like good candidates, but
  • the "mapping"-level annotations like @Table and @Column seem like they should not be meta-annotations.

On the other hand, within the categories of "model" and "mapping", perhaps there are special exceptions. For example, @Converter is more of a "mapping" annotation, but it seems to me that it's really a good candidate for use as a meta-annotation.

But I think this is something we should definitely consider for inclusion in JPA 4.

gavinking avatar Aug 23 '23 10:08 gavinking

@EntityListeners is a no-brainer IMO

I guess another "no-brainer" is @Convert. It would be really nice to be able to apply a converter via a custom annotation.

gavinking avatar Aug 28 '23 08:08 gavinking

It's great to see that this thread is being revived!

I'm not familiar with the calendar for JPA 3.2, but if there is time, wouldn't it be possible to put into JPA 3.2 the most common ones? I was thinking of @Entity, @Id, @GeneratedValue, as they are the ones we use in each and every Entity class.

michaelisvy avatar Aug 28 '23 22:08 michaelisvy