views:

35

answers:

2

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?

+1  A: 

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

Pascal Thivent
A: 

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();
    }
Mike Baranczak
No, this doesn't work.
Pascal Thivent