Timofey Gorshkov
Timofey Gorshkov

Reputation: 5125

Applying annotations to fields inherited from @MappedSuperclass

Has:

@MappedSuperclass
class Superclass {

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

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

}

and

@Entity
class Subclass extends Superclass {

}

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

Upvotes: 4

Views: 7292

Answers (4)

Timofey Gorshkov
Timofey Gorshkov

Reputation: 5125

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 {

}

Upvotes: 1

AllDayAmazing
AllDayAmazing

Reputation: 2383

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.

Upvotes: 0

Mike Baranczak
Mike Baranczak

Reputation: 8374

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();
    }

Upvotes: 0

Pascal Thivent
Pascal Thivent

Reputation: 570365

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

Upvotes: 5

Related Questions