openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael Dick" <michael.d.d...@gmail.com>
Subject Re: How can i make my classes work in all databases?
Date Tue, 07 Oct 2008 16:21:31 GMT
Sorry about that, I didn't notice the part about slice.

As I understand it slice uses the "master" slice to get the next value for
an autogenerated field. By default the master slice is the first slice
listed. The wording in the manual is a little confusing though :

<quote>
4.3. openjpa.slice.Master
This plug-in property can be used to identify the name of the master slice.
Master slice is used when a primary key is to be generated from a database
sequence.

By default the master slice is the first slice in the list of configured
slice names.
Warning
Currently, there is no provision to use sequence from multiple database
slices.
</quote>

I think the warning refers to
@GeneratedValue(strategy=GenerationType.SEQUENCE) not generated values in
general.

I've run some fairly simple unit tests and it looks like the generated
values are working. Have you run into errors when using Slice and
GeneratedValues?

-mike

On Tue, Oct 7, 2008 at 9:47 AM, thiago ananias <thiagoananias@hotmail.com>wrote:

>
>
> I understand you! but...
>
> I'm using "Slice"(A Distributed Database plugin for OpenJPA) and all
> database will have his own table "GeneratedValue" and i need a unique
> identifier for all databases at the same time!
>
> Thanks!
>
>
> Michael Dick wrote:
> >
> > 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
> > <thiagoananias@hotmail.com>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.
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://n2.nabble.com/How-can-i-make-my-classes-work-in-all-databases--tp1303283p1303755.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>

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