4

I've a class Foo, which have an @Entity and @Table annotation, and an other class, Bar, which extends Foo. Are the annotations on the Bar class? If not, can I force that?

Thanks a lot! Sam.

Samuel ROZE
  • 753
  • 1
  • 6
  • 12

3 Answers3

2

Annotations are not inherited.

You can read more here: http://fusionsoft-online.com/articles-java-annotations.php

Some other references: https://stackoverflow.com/a/1644135/1001027 and https://stackoverflow.com/a/4745820/1001027

Exceptions are annotations which its declaration is annotated with @Inherited.

Community
  • 1
  • 1
Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
  • By _default_, annotations are not inherited. However, annotations _are_ inherited (from superclasses) if they are meta-annotated with the `@Inherited` annotation. – matts Jun 12 '12 at 22:59
  • @matts: you are right, annotations are inherited in case its declaration (annotation itself) is annotated with `@Inherited`. – Francisco Spaeth Jun 13 '12 at 06:33
1

The @Entity and @Table annotations are not inherited by default, but you can use the @Inheritance annotation on the superclass to cause them to be inherited to subclasses, though.

See here for more information: http://en.wikibooks.org/wiki/Java_Persistence/Inheritance

matts
  • 6,738
  • 1
  • 33
  • 50
0

Check @MappedSuperclass annotation.

vbezhenar
  • 11,148
  • 9
  • 49
  • 63