Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 765
Siddharth Trikha Avatar asked Jan 28 '14 05:01

Siddharth Trikha


People also ask

Does JPA require primary key?

Every JPA entity must have a primary key. You can specify a primary key as a single primitive, or JDK object type entity field (see "Configuring a JPA Entity Simple Primary Key Field").

How can we declare entity without primary key in JPA?

Sometimes your object or table has no primary key. The best solution in this case is normally to add a generated id to the object and table. If you do not have this option, sometimes there is a column or set of columns in the table that make up a unique value. You can use this unique set of columns as your id in JPA.

How do we mention primary key in JPA entity?

In JPA, we have two options to define the composite keys: the @IdClass and @EmbeddedId annotations. In order to define the composite primary keys, we should follow some rules: The composite primary key class must be public. It must have a no-arg constructor.

What if entity has no primary key?

If you create a default entity object from a table with no primary key, a RowID attribute is automatically created as the primary key.


1 Answers

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

like image 104
Jose Manuel Gomez Alvarez Avatar answered Sep 21 '22 13:09

Jose Manuel Gomez Alvarez