tags:

views:

54

answers:

2

I have a MySQL table without primary key, and I have to map it into a JPA entity. I cannot modify the table in any way.

Because entities must have a primary key, I have to specify one. If I'm certain that the field I use as a primary key in the entity (or the fields, should I opt for using composite primary key) will always be unique (and not null) in table, can the fact that the table doesn't have a primary key specified in CREATE TABLE cause any issues?

+2  A: 

That's correct. JPA has no way of knowing if the column(s) it is using as a PK is actually a real PK in the database. If those column(s) are, in practice, a PK, then it should be fine.

You may potentially get some performance problems if the pseudo-PK columns are not correctly indexed, though - JPA will execute queries against the PK on the assumption that it will perform well.

skaffman
+1  A: 

JPA itself doesn't analyze your database. Just don't use common methods using primary key (find/merge/...) instead use named queries, for example using jpql update syntax.

@Entity
@Table(name = "login")
@NamedQueries({
        @NamedQuery(name = "Login.updateLastOnline", 
        query = "UPDATE Login l SET l.lastOnline = :newDate WHERE l.loginId = :loginId")
        })
public class Login implements Serializable
{

It doesn't matter if loginId is primary key

Dewfy
Why could find and merge cause problems?
tputkonen
@tputkonen - may be I'm not explicitly write about, but I've tried to say, that you CAN avoid usage find/merge by using JPQL.
Dewfy
I agree with that, it's just that we had experienced some strange behavior when using merge with such tables, and it would be good to know the reason
tputkonen
This update isn't safe either from a concurrency point of view, which is a big problem.
Pascal Thivent