1

I am getting exception : org.hibernate.AnnotationException: No identifier specified for entity

I know that hibernate must have @Id in an entity class but in my table there are no primary keys just two foreign keys. How do i map the hibernate entity class without errors?

For example the class.

@Entity
@Table(name="building_block")
public class BuildingBlock implements Serializable {

    private Integer archId;
    private Integer mirrorId; //foreign key to Mirror table
    private Integer makeId; //foreign key to Maker table
    private Integer sweepId;
    private String search;
    private Integer length;
    private Date createDate;

    @Column(name="arch_id")
    public Integer getArchId() {
        return ArchId;
    }
    public void setArchId(Integer ArchId) {
        this.ArchId = ArchId;
    }
    @Column(name="mirror_id")
    public Integer getMirrorId() {
        return mirrorId;
    }
    public void setMirrorId(Integer mirrorId) {
        this.mirrorId = mirrorId;
    }
    @Column(name="make_id")
    public Integer getMakeId() {
        return makeId;
    }
    public void setMakeId(Integer makeId) {
        this.makeId = makeId;
    }

    ...

    @Column(name="create_date")
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}
logger
  • 1,983
  • 5
  • 31
  • 57
  • What does the corresponding SQL table look like? That is, which columns make up the table's primary key (either declared or implied)? – Brian Vosburgh Dec 11 '15 at 21:51
  • Although it is totally possible to add composite PKs in Hibernate (see http://stackoverflow.com/questions/13936340/how-to-create-hibernate-composite-key-using-annotations), I would discourage it, unless you're working on a legacy DB on which you can't perform any schema change. – Stefano Cazzola Dec 13 '15 at 09:03

3 Answers3

0

You must add primary key column and declare a primary key in the entity.

Ref : http://docs.jboss.org/hibernate/core/3.5/reference/en/html/mapping.html#mapping-declaration-id

Mapped classes must declare the primary key column of the database table

Rahman
  • 3,755
  • 3
  • 26
  • 43
0

You could try to make those foreign keys the primary keys of your entity by making a composite id.

To see how to make composite keys follow this link (How to map a composite key with Hibernate?)

Community
  • 1
  • 1
0

If you know that a set of fields can always identify a specific record, you can group them together and tell Hibernate to use that as the primary key.

See Using Hibernate Get with Multi-Column Primary Key for how to do this.

Community
  • 1
  • 1
Tim DG
  • 48
  • 4