Return-Path: Delivered-To: apmail-openjpa-users-archive@locus.apache.org Received: (qmail 44315 invoked from network); 7 Oct 2008 14:38:19 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 7 Oct 2008 14:38:19 -0000 Received: (qmail 62874 invoked by uid 500); 7 Oct 2008 14:38:17 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 62856 invoked by uid 500); 7 Oct 2008 14:38:17 -0000 Mailing-List: contact users-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openjpa.apache.org Delivered-To: mailing list users@openjpa.apache.org Received: (qmail 62844 invoked by uid 99); 7 Oct 2008 14:38:17 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 07 Oct 2008 07:38:17 -0700 X-ASF-Spam-Status: No, hits=2.3 required=10.0 tests=DNS_FROM_SECURITYSAGE,HTML_MESSAGE,SPF_PASS,WHOIS_MYPRIVREG X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of michael.d.dick@gmail.com designates 209.85.200.168 as permitted sender) Received: from [209.85.200.168] (HELO wf-out-1314.google.com) (209.85.200.168) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 07 Oct 2008 14:37:13 +0000 Received: by wf-out-1314.google.com with SMTP id 28so3362118wfc.24 for ; Tue, 07 Oct 2008 07:37:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:in-reply-to:mime-version:content-type:references; bh=5cauFgRt36UaPO3NfBIbSX9xwpmNDHbYaZFWJLi8O/Q=; b=QHYKhEK5Ou2LsOqWEor7CT7lD7DTpw/uPYX062P6PZqNKFBMAr2y95w4JWFu56gOu9 s3r1ASNNdqYVY8EwJz9FrbfzcJuNk0wlXH3Ud6MCdfXkbwTR3XYtL4bvilDXamf11q6E oFCfO261l+1dl0afPdhoxV+NPGqxS5fsPYy+I= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version :content-type:references; b=qTX3hJASI7Qyws5rw/0GyQJdvEj6/8+UCarprMLvzn8suTPmn4j6U1v+q5AHJfPS11 gMJDGtl6SsHUC06mvS0MJ4Rk+oKnHpV9hZYdSUzuMSPD03LG9f+evH0CNVvtJeSwvDgT 5BGoo7xiRKISGNYgwBZ0nT2K9EDvUdf6rTyUI= Received: by 10.141.70.18 with SMTP id x18mr3904326rvk.284.1223390259124; Tue, 07 Oct 2008 07:37:39 -0700 (PDT) Received: by 10.141.40.12 with HTTP; Tue, 7 Oct 2008 07:37:39 -0700 (PDT) Message-ID: <72c1350f0810070737j12694e89r8fb29e6f190885ce@mail.gmail.com> Date: Tue, 7 Oct 2008 09:37:39 -0500 From: "Michael Dick" To: users@openjpa.apache.org Subject: Re: How can i make my classes work in all databases? In-Reply-To: <1223378500483-1303283.post@n2.nabble.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_46186_7393972.1223390259089" References: <1223378500483-1303283.post@n2.nabble.com> X-Virus-Checked: Checked by ClamAV on apache.org ------=_Part_46186_7393972.1223390259089 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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; @Entity @Table(name="produto") public class Produto implements Serializable { @Id @Column(name="id_produto") @GeneratedValue 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, -mike [1] http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_meta_gen 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: > http://n2.nabble.com/How-can-i-make-my-classes-work-in-all-databases--tp1303283p1303283.html > Sent from the OpenJPA Users mailing list archive at Nabble.com. > > ------=_Part_46186_7393972.1223390259089--