Siddharth Trikha
Siddharth Trikha

Reputation: 2506

JPA entity has no primary key?

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.

How should I proceed to remove this error?

Upvotes: 20

Views: 72785

Answers (6)

Svetopolk
Svetopolk

Reputation: 367

In postgres just add fake id field

@Id
@Column(name="ctid")
String id;

Upvotes: 3

Ducane
Ducane

Reputation: 129

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.

Upvotes: 0

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...

Upvotes: 38

Rafael Quintino
Rafael Quintino

Reputation: 69

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!!!

Upvotes: 6

Zaw Than oo
Zaw Than oo

Reputation: 9935

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.

Upvotes: 26

6ton
6ton

Reputation: 4214

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.

Upvotes: -1

Related Questions