Hibernate实体没有PK但有FK

时间:2022-09-15 17:50:17

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

我收到异常:org.hibernate.AnnotationException:没有为实体指定标识符

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?

我知道hibernate在实体类中必须有@Id但在我的表中没有主键只有两个外键。如何映射hibernate实体类没有错误?

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

3 个解决方案

#1


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

参考: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

映射类必须声明数据库表的主键列

#2


0  

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

您可以尝试通过创建复合ID来使这些外键成为您实体的主键。

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

要了解如何制作复合键,请点击此链接(如何使用Hibernate映射复合键?)

#3


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.

如果您知道一组字段始终可以标识特定记录,则可以将它们组合在一起并告诉Hibernate将其用作主键。

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

有关如何执行此操作,请参阅使用Hibernate获取多列主键。

#1


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

参考: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

映射类必须声明数据库表的主键列

#2


0  

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

您可以尝试通过创建复合ID来使这些外键成为您实体的主键。

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

要了解如何制作复合键,请点击此链接(如何使用Hibernate映射复合键?)

#3


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.

如果您知道一组字段始终可以标识特定记录,则可以将它们组合在一起并告诉Hibernate将其用作主键。

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

有关如何执行此操作,请参阅使用Hibernate获取多列主键。