You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by thiago ananias <th...@hotmail.com> on 2008/10/07 13:21:40 UTC

How can i make my classes work in all databases?

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.


Re: How can i make my classes work in all databases?

Posted by Pinaki Poddar <pp...@apache.org>.
Thanks Milosz. You are right. 
Annotating a class with @Replicated enables Slice to store its instances in
multiple databases. This is useful for quasi-static master data such as
Country/Currency Code which all other entities may refer. Earlier because of
'collocation constraint', Slice was storing the entire closure of a graph in
the same slice -- but with @Replicated and ReplicationStrategy interface --
this restriction has been removed. Few preliminary test cases have been
committed to demonstrate its usage. 
The aggregate queries are also aware of this replicated instances and
returns the correct aggregate values.



Miłosz Tylenda wrote:
> 
> Thiago,
> 
> A few weeks ago there was a commit of @Replicated annotation. It might be
> what you want. It requires a nightly build of OpenJPA though. Have a look
> at
> 
> http://openjpa.apache.org/builds/latest/docs/javadoc/org/apache/openjpa/persistence/Replicated.html
> 
> Regards,
> Milosz
> 
> 
>> I'm sorry...i told you without make the test! It's true, the
>> GeneratedValue
>> works correctly in "Slices", this is Great!
>> 
>> I have one more question but it's specifically about Slices:
>> 
>> I have a class that makes the Distribution Rules, and the method "public
>> String distribute(Object object, List slices, Object context)"
>> always returns a String of one Slice! But if i want to persist a object
>> in
>> all databases or two, what can i do?
>> 
>> Thanks!
>> 
>> 
>> 
>> Michael Dick wrote:
>> > 
>> > 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 :
>> > 
>> > 
>> > 
>> >> 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.
>> >> 
>> > 
>> > 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
>> > 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
>> >> > 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.
>> >>
>> >>
>> > 
>> > 
>> 
>> -- 
>> View this message in context:
>> http://n2.nabble.com/How-can-i-make-my-classes-work-in-all-databases--tp1303283p1306265.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--tp1303283p1331788.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: How can i make my classes work in all databases?

Posted by Miłosz Tylenda <mt...@o2.pl>.
Thiago,

A few weeks ago there was a commit of @Replicated annotation. It might be what you want. It requires a nightly build of OpenJPA though. Have a look at

http://openjpa.apache.org/builds/latest/docs/javadoc/org/apache/openjpa/persistence/Replicated.html

Regards,
Milosz


> I'm sorry...i told you without make the test! It's true, the GeneratedValue
> works correctly in "Slices", this is Great!
> 
> I have one more question but it's specifically about Slices:
> 
> I have a class that makes the Distribution Rules, and the method "public
> String distribute(Object object, List slices, Object context)"
> always returns a String of one Slice! But if i want to persist a object in
> all databases or two, what can i do?
> 
> Thanks!
> 
> 
> 
> Michael Dick wrote:
> > 
> > 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 :
> > 
> > 
> > 
> >> 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.
> >> 
> > 
> > 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
> > 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
> >> > 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.
> >>
> >>
> > 
> > 
> 
> -- 
> View this message in context: http://n2.nabble.com/How-can-i-make-my-classes-work-in-all-databases--tp1303283p1306265.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> 
> 

Re: How can i make my classes work in all databases?

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  I am bit irregular on attending this list as I have been on vacation and
traveling. Thanks Mike for his illuminating answers and Mike's statement on
lack of clarity on 'master' slice and auto-generated identity is painfully
noted :)
 
  > But if i want to persist a object in all databases or two, what can i
do?
  I have recently added support in Slice to relax 'collocation constraint'
mainly to support quasi-static master data. In effect, it has made possible
for an instance to be stored in multiple databases as identical copies. The
changes are committed to trunk but without documentation.
   I will try to commit the document as soon as possible. 


thiago ananias wrote:
> 
> 
> I'm sorry...i told you without make the test! It's true, the
> GeneratedValue works correctly in "Slices", this is Great!
> 
> I have one more question but it's specifically about Slices:
> 
> I have a class that makes the Distribution Rules, and the method "public
> String distribute(Object object, List<String> slices, Object context)"
> always returns a String of one Slice! But if i want to persist a object in
> all databases or two, what can i do?
> 
> Thanks!
> 
> 
> 
> Michael Dick wrote:
>> 
>> 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 :
>> 
>> 
>> 
>>> 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.
>>> 
>> 
>> 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
>> <th...@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
>>> > <th...@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.
>>>
>>>
>> 
>> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/How-can-i-make-my-classes-work-in-all-databases--tp1303283p1331783.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: How can i make my classes work in all databases?

Posted by thiago ananias <th...@hotmail.com>.

I'm sorry...i told you without make the test! It's true, the GeneratedValue
works correctly in "Slices", this is Great!

I have one more question but it's specifically about Slices:

I have a class that makes the Distribution Rules, and the method "public
String distribute(Object object, List<String> slices, Object context)"
always returns a String of one Slice! But if i want to persist a object in
all databases or two, what can i do?

Thanks!



Michael Dick wrote:
> 
> 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 :
> 
> 
> 
>> 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.
>> 
> 
> 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
> <th...@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
>> > <th...@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.
>>
>>
> 
> 

-- 
View this message in context: http://n2.nabble.com/How-can-i-make-my-classes-work-in-all-databases--tp1303283p1306265.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: How can i make my classes work in all databases?

Posted by Michael Dick <mi...@gmail.com>.
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 <th...@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
> > <th...@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.
>
>

Re: How can i make my classes work in all databases?

Posted by thiago ananias <th...@hotmail.com>.

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
> <th...@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.


Re: How can i make my classes work in all databases?

Posted by Michael Dick <mi...@gmail.com>.
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 <th...@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.
>
>