Java JPA 实体没有主键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21397393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:27:47  来源:igfitidea点击:

JPA entity has no primary key?

javadatabasejpaentityejb-3.0

提问by Siddharth Trikha

I have an Entity class:

我有一个实体类:

@Entity
@Table(name="CMC_MAP_SERVER_INFO")
@NamedQuery(name="CmcMapServerInfo.getMapServer", query="SELECT c FROM CmcMapServerInfo c")
public class CmcMapServerInfo implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name="APPLICATION_NAME")
    private String applicationName;

    private String remarks;

    @Column(name="SERVER_IP")
    private String serverIp;

    @Column(name="SERVER_NAME")
    private String serverName;

    @Column(name="SERVER_PORT")
    private short serverPort;

    public CmcMapServerInfo() {
    }

I get the following error:

我收到以下错误:

Entity class [class cdot.oss.cmsat.conf.ejb.entity.CmcMapServerInfo] has no primary key specified.

I read online and found out that entities must have a primary key defined. But my table here is a ONE row table only. It just used to save system configuration.

我在网上阅读并发现实体必须定义一个主键。但我这里的表只是一个单行表。它只是用来保存系统配置。

So only queries I will like to do will be to check if the row exists then get that row and update it.

所以我只想做的查询是检查该行是否存在,然后获取该行并更新它。

My columns are serverIp, port, name of the server.

我的列是serverIp, port, name of the server

How should I proceed to remove this error?

我应该如何继续消除此错误?

回答by 6ton

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.

您可以将 applicationName 或 ip 地址标记为主键(尽管不是自动生成的)。即使这些列都没有被声明为数据库中的主键,也没关系。

回答by Zaw Than oo

JPA 2.0 Specification

JPA 2.0 Specification

  • Entity class must be annotated with the Entityannotation.
  • Entity class must have a no-arg constructor.
  • Entity class must not be final
  • Entity class must implement the Serializableinterfaces.
  • Entity class must have a unique, immutable ID
  • 实体类必须使用注解进行Entity注解。
  • 实体类必须具有无参数构造函数。
  • 实体类不能是最终的
  • 实体类必须实现Serializable接口。
  • 实体类必须具有唯一的、不可变的 ID

Otherwise, you cannot.

否则,你不能。

回答by Rafael Quintino

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]:

也许你会遇到 [Select By Id] 的问题:

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

Take care and be happy!!!

保重和快乐!!!

回答by Jose Manuel Gomez Alvarez

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

我在没有 PK 的消息中也遇到了这个问题。

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

在 Oracle 中,您可以使用任何表始终存在的 ROWID 列。

Like this:

像这样:

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

Hope it helps...

希望能帮助到你...

回答by Ducane

Another way would be specify that the class itself can be used as unique identifier with @IdClass. So in this case just annotate the class with @IdClass(CmcMapServerInfo.class) and it should be independent of underlying database.

另一种方法是指定类本身可以用作@IdClass 的唯一标识符。因此,在这种情况下,只需使用 @IdClass(CmcMapServerInfo.class) 注释该类,它应该独立于底层数据库。