Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Entity not recognizing the @Id in a @MappedSuperclass

I'm attempting to create a base class for a set of entities to reduce coding effort and duplication. My thought is that the base class has the common meta-data fields, and the child classes deal with their unique attributes.

My base class:

@MappedSuperclass
public abstract class FinanceEntityBean {
    protected Long id;

    @Version
    private long version;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(final Long id) {
        this.id = id;
    }
}

The first entity:

@Entity
@Table(name = "tag")
public class Tag extends FinanceEntityBean {
}

I've written tests using this code to do CRUD functions on the Tag entity, and they are all working fine.

My question is - why does Eclipse (Indigo) insist that Tag has an error:

The entity has no primary key attribute defined

I've changed that to a warning for now so my code will compile, but I'm curious why Eclipse isn't happy, and if I've misunderstood something.

Is this valid JPA 2.0 code? Hibernate 4.1.5 is my JPA provider.

like image 491
Gordon Avatar asked Aug 27 '12 01:08

Gordon


1 Answers

When using mixed access you have to specify the access type. See Eclipse Dali bug 323527 for giving a better validation error when both fields and properties are annotated.

Option 1 : Annotate the getVersion() method instead, only properties are annotated.
Option 2 : Specify mixed access type as follows:

@MappedSuperclass
@Access(AccessType.PROPERTY)
public abstract class FinanceEntityBean {
    protected Long id;

    @Version
    @Access(AccessType.FIELD)
    private long version;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(final Long id) {
        this.id = id;
    }
}
like image 182
Karen Butzke Avatar answered Sep 18 '22 17:09

Karen Butzke