JPA Error : The entity has no primary key attribute defined
Asked Answered
H

8

17

I am using JPA in my application. In one of the table, I have not used primary key (I know its a bad design).

Now the generated entity is as mentioned below :

@Entity
@Table(name="INTI_SCHEME_TOKEN")
public class IntiSchemeToken implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name="CREATED_BY")
    private String createdBy;

    @Temporal( TemporalType.DATE)
    @Column(name="CREATED_ON")
    private Date createdOn;

    @Column(name="SCH_ID")
    private BigDecimal schId;

    @Column(name="TOKEN_ID")
    private BigDecimal tokenId;

    public IntiSchemeToken() {
    }

    public String getCreatedBy() {
        return this.createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreatedOn() {
        return this.createdOn;
    }

    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    public BigDecimal getSchId() {
        return this.schId;
    }

    public void setSchId(BigDecimal schId) {
        this.schId = schId;
    }

    public BigDecimal getTokenId() {
        return this.tokenId;
    }

    public void setTokenId(BigDecimal tokenId) {
        this.tokenId = tokenId;
    }



} 

Here In my project, eclipse IDE shows ERROR mark(RED colored cross) on this class and the error is "The entity has no primary key attribute defined".

Can anyone tell me, How to create an entity without primary key ?

Thanks.

Hemistich answered 2/11, 2012 at 7:20 Comment(1)
#1519578Lowdown
A
23

You can't. An entity MUST have a unique, immutable ID. It doesn't have to be defined as a primary key in the database, but the field or set of fields must uniquely identify the row, and its value may not change.

So, if one field in your entity, or one set of fields in your entity, satisfies these criteria, make it (or them) the ID of the entity. For example, if there is no way that a user can create two instances in the same day, you could make [createdOn, createdBy] the ID of the entity.

Of course this is a bad solution, and you should really change your schema and add an autogenerated, single-column ID in the entity.

Aftermost answered 2/11, 2012 at 7:44 Comment(4)
+1. If you don't need it from the business logic point of view, just add an artificial ID column to make ORM happy and your life easier.Disharoon
@Thanks for the reply. I have declared createdOn as primary key in the entity not in the DB. It works fine now.Hemistich
"It doesn't have to be defined as a primary key in the database". I object to that. If this is a must due to existence of another primary key definition, it must be used. Otherwise, if the implicit suggestion is to define a secondary index, then OK, but going without an index at all, you'll kill your database.Peyter
I'm just saying that Hibernate doesn't require that its Id field is defined as a primary key in the table. Of course, I wouldn't ever use a table without a PK, but that's a different problem.Aftermost
B
8

If your Primary Key(PK) is a managed super class which is inherited in an entity class then you will have to include the mapped super class name in the persistence.xml file.

Look at the bug report: https://bugs.eclipse.org/bugs/show_bug.cgi?id=361042

Backlog answered 3/5, 2015 at 9:8 Comment(0)
N
6

If you need to define a class without primary key, then you should mark that class as an Embeddable class. Otherwise you should give the primary key for all entities you are defining.

Noella answered 3/11, 2012 at 3:28 Comment(0)
P
4

You can turn off (change) validation that was added.

Go to workspace preferences 'Java Persistence->JPA->Errors/Warnings' next 'Type' and change 'Entity has no primary key' to 'Warnning'.

Packton answered 5/5, 2015 at 6:54 Comment(1)
Seems to be the ideal solution in this case ^Grozny
L
1

In addition to http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#No_Primary_Key you can use some build-in columns like ROWID in Oracle:

but with care:

Entity frameworks doesn't work for all kind of data (like statistical data which was used for analysis not for querying).

Lowdown answered 6/12, 2013 at 14:8 Comment(0)
A
0

Another solution without Hibernate

If - you don't have PK on the table - there is a logical combination of columns that could be PK (not necessary if you can use some kind of rowid) -- but some of the columns are NULLable so you really can't create PK because of DB limitation - and you can't modify the table structure (would break insert/select statements with no explicitly listed columns at legacy code)

then you can try the following trick - create view at database with virtual column that has value of concatenated logical key columns ('A='||a||'B='||'C='c..) or rowid - create your JPA entity class by this view - mark the virtual column with @Id annotation

That's it. Update/delete data operations are also possible (not insert) but I wouldn't use them if the virtual key column is not made of rowid (to avoid full scan searches by the DB table)

P.S. The same idea is partly described at the linked question.

Antidepressant answered 24/10, 2014 at 12:24 Comment(0)
D
0

You need to create primary key ,If not found any eligible field then create auto increment Id.

CREATE TABLE fin_home_loan (
  ID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (ID));
Diamagnet answered 2/12, 2015 at 19:32 Comment(0)
K
0

Just add fake id field. In Postgres:

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

In Oracle:

@Id
@Column(name="ROWID")
String rowid;
Kauffman answered 18/10, 2021 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.