how to implement uuid in jpa model?

1,241 views
Skip to first unread message

macrohunt

unread,
May 13, 2009, 11:31:19 AM5/13/09
to play-framework
we always use @ID @GenerateValue in jpa model. but when we move the
data from one db to another, it has many problems.
How to use uuid instead with @GenerateValue? Is there any easy way?

Guillaume Bort

unread,
May 14, 2009, 8:49:22 AM5/14/09
to play-fr...@googlegroups.com
Yes you can but you must not extend JPAModel as it provides a default
Long, generated id.

You can either use the hibernate UUID generator (outside of the JPA spec) :

package models;

import javax.persistence.*;
import org.hibernate.annotations.*;
import play.db.jpa.*;

@javax.persistence.Entity
public class User extends JPASupport {

@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
public String id;

}

or let your application manage the identifiers without using any
@GeneratedValue annotation...
You can use play.libs.Codec.UUID() to generate unique identifiers.

macrohunt

unread,
May 16, 2009, 3:20:11 AM5/16/09
to play-framework
hi, Guillaume

Thank you for your help. i have create a app use uuid and success
when create a record.
But when i view or edit the record USE CRUD module, it's wrong,
this's the message:

15:14:53,661 WARN ~ SQL Error: -28, SQLState: S0022
15:14:53,662 ERROR ~ Column not found:
FF808081214845A201214845EBBC0001 in statement [select configurat0_.id
as id0_, configurat0_.confCode as confCode0_, configurat0_.confContent
as confCont3_0_, configurat0_.confDescription as confDesc4_0_,
configurat0_.confName as confName0_ from Configuration configurat0_
where configurat0_.id=ff808081214845a201214845ebbc0001]

the problem is the findById method always use : "id = ?", not "id =
'?' ".
Any suggestions to correct it?

macrohunt

unread,
May 16, 2009, 3:27:29 AM5/16/09
to play-framework
i have just change the str “ where id = " ” + id to “ where id = '" +
id + "'" ”. it works right.

but, if in one application, some models use uuid, but some models
still extends JPAModel, How to do?

Guillaume Bort

unread,
May 16, 2009, 8:46:10 AM5/16/09
to play-fr...@googlegroups.com
This because, this code is not well written.
It should use :

Query q = JPA.getEntityManager().createQuery("from " +
entityClass.getName() + " where id = ?");
q.setParameter(1, id);
q.getSingleResult();

Can you test it and if it works send me a patch ?

Thank you.

macrohunt

unread,
May 16, 2009, 3:21:01 PM5/16/09
to play-framework
Hi, Guillaume.

Thank you for your help.

i have test it and it works fine. but i have heavily modified
CRUD.java. so i just post the whole code of the method here:::

public JPASupport findById(Object id) {
Query q = JPA.getEntityManager().createQuery("from " +
entityClass.getName() + " where id = ?");
q.setParameter(1, id);
return (JPASupport) q.getSingleResult();
}
Reply all
Reply to author
Forward
0 new messages