openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael Dick" <>
Subject Re: How can i make my classes work in all databases?
Date Tue, 07 Oct 2008 14:37:39 GMT
Hi Thiago,

The class you included will work with all databases. Id will be mapped to an
INTEGER column, nome will be mapped to a VARCHAR column, and dtInsert will
be DATE or TIMESTAMP. The actual mappings are determined by OpenJPAs
DBDictionary classes - there's one for each database you listed. The given
example should work fine with any RDB we support.

It sounds like you're interested in using auto generation for the ID value
instead of using System.currentTimeMillis(). You're in luck there as well.
OpenJPA supports several methods of auto generation which are discussed in
the manual [1].

The most simple way to use an auto generated id field would be this :
import javax.persistence.GeneratedValue;

public class Produto implements Serializable {

   private int id;
   . . .

The GeneratedValue annotation causes OpenJPA to create an additional table
in the database which stores the last used ID value. Each new entity you
persist gets a new ID value which should be unique. This approach is generic
and works on any of the supported databases.

If you can't create a new table in the database then you'll probably want to
use @GeneratedValue(strategy=GenerationType.IDENTITY) which tells the
Database to generate the ID value. It should work on most databases but I've
only tried with DB2, MySQL, and Oracle.

Hope this helps,



On Tue, Oct 7, 2008 at 6:21 AM, thiago ananias <>wrote:

> Hello again!
> I'm need to make a classe that works in all databases, in the example of
> "Slice" the id is generated by "System.currentTimeMillis()"
> But i don't know if exists other way for map this class for oracle,
> sqlserver and mysql because there are differents kinds of ids(sequence,
> identity, auto). If don't exists a "universal mapping", i was thinking how
> can i generate a "UUDI" to be the ID of the tables, but how can i store
> this
> value in database? because if i store this value as string the database
> will
> be slow!!
> How can i make this class "universal" for the databases?
> @Entity
> @Table(name="produto")
> public class Produto implements Serializable {
>    @Id
>    @Column(name="id_produto")
>    private int id;
>    @Column(name="vc_nome", nullable=false)
>    private String nome;
>    @Column(name="dt_insert", nullable=false)
>    @Temporal(javax.persistence.TemporalType.DATE)
>    private Date dtInsert;
> }
> Thanks!
> --
> View this message in context:
> Sent from the OpenJPA Users mailing list archive at

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message