开发者

Applying annotations to fields inherited from @MappedSuperclass

Has:

@MappedSuperclass
class Superclass {

    @Id
    @Column(name = "id")
    protected long id;

    @Column(name="field")
    private long field;

}

an开发者_高级运维d

@Entity
class Subclass extends Superclass {

}

How to annotate inherited id with @GeneratedValue and field with @Index within Subclass?


How to annotate inherited id with @GeneratedValue and field with @Index within Subclass?

AFAIK, you can't. What you can do is overriding attributes and associations (i.e. change the column or join column) using the AttributeOverride and AssociationOverride annotations. But you can't do exactly what you're asking.

For the GeneratedValue, consider using XML mapping to override the strategy if you don't want to declare it in the mapped superclass.

For the Index (which is not a standard annotation by the way), did you actually try to declare it at the table level using Hibernate's Table annotation instead (I'm assuming you're using Hibernate)?

@Table(appliesTo="tableName", indexes = { @Index(name="index1", columnNames=
    {"column1", "column2"} ) } ) 

creates the defined indexes on the columns of table tableName.

References

  • JPA 1.0 Specification
    • Section 2.1.9.2 "Mapped Superclasses"
    • Section 9.1.10 "AttributeOverride Annotation"
    • Section 9.1.11 "AttributeOverrides Annotation"
    • Section 9.1.12 "AssociationOverride Annotation"
    • Section 9.1.13 "AssociationOverrides Annotation"
  • Hibernate Annotations Reference Guide
    • 2.4. Hibernate Annotation Extensions
    • Chapter 3. Overriding metadata through XML


As for @GeneratedValue, it is possible to do like this:

@MappedSuperclass
class Superclass {

    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "id_generator")
    protected long id;

    @Column(name = "field")
    private long field;

}
@Entity
@SequenceGenerator(name = "id_generator", sequenceName = "id_seq")
class Subclass extends Superclass {

}


You might be able to do this if you apply the annotations to the accessor methods instead. (I haven't tried this, so I can't guarantee that it'll work.)

@MappedSuperclass
public class Superclass {

    @Id
    @Column(name = "id")
    public long getId() {
        return id;
    }

.

@Entity
public class Subclass extends Superclass {

    @GeneratedValue
    public long getId() {
        return super.getId();
    }


Just in case anyone else searches for this, I used the following code which adds in some overhead, but for processing Field annotations only shouldn't add that much:

    private List<Field> getAllFields() {
    List<Field> fieldList = new ArrayList<Field>();

    // Add all fields from the current class
    fieldList.addAll(Arrays.asList(mElement.getClass().getDeclaredFields()));

    // Use an index to iterate over mElement's parent types
    Class clazz = mElement.getClass();

    // Get any fields from the parent class(es)
    while (clazz.getSuperclass() != null) {
        fieldList.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
        // Set it to that parent class
        clazz = clazz.getSuperclass();
    }

    return fieldList;
}

The returned list would contain all fields for all parent and child classes with mElement being the object you are searching for annotations from. Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜