JPA entity has no primary key?

主宰稳场 提交于 2019-11-27 23:44:38

JPA 2.0 Specification

  • Entity class must be annotated with the Entity annotation.
  • Entity class must have a no-arg constructor.
  • Entity class must not be final
  • Entity class must implement the Serializable interfaces.
  • Entity class must have a unique, immutable ID

Otherwise, you cannot.

I had this problem as well with a message without PK.

In Oracle, you can use the ROWID column which is always there for any table.

Like this:

@Id
@Column(name="ROWID")
String rowid;

Hope it helps...

Every entity object in the database is uniquely identified. An alternate way to represent a table without a primary key is to use a composite primary key using all its columns, or some of them representing a candidate key:

@Entity
public class TableWithoutId {
    @EmbeddedId EmbeddableTableWithoutId id;

    /* Getters an Setters... */

    //Here you can implement @Transient delegates to the Getters an Setters of "id".
}

@Embeddable
Class EmbeddableTableWithoutId {
    int columnA;
    long columnB;
    /* Getters an Setters... */
}

Maybe you will have problems with [Select By Id]:

entityManager.find(TableWithoutId.class, id);//it can throws NonUniqueResultException or anything like that...

Take care and be happy!!!

You can mark the applicationName or the ip address as the primary key (though not auto generated). It's ok even if neither of these columns are declared as primary keys in the database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!