Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » native id generator(an id generator that can use alternatively sequence or identity )
native id generator [message #539060] Wed, 09 June 2010 14:08 Go to next message
Luca  is currently offline Luca
Messages: 8
Registered: April 2010
Junior Member
Hi all,
I'm going to switch from Hibernate to EclipseLink in an exiting project. The old Hibernate mapping could be used with Oracle, MySQL, PostgreSQL and H2. And it used native id generator to automatically switch between databases with autoincrement support and databases with sequence support. Is there such a generator type in EclipseLink?

Thanks in advance,
Luca
Re: native id generator [message #539232 is a reply to message #539060] Thu, 10 June 2010 06:14 Go to previous messageGo to next message
Doug Clarke is currently offline Doug Clarke
Messages: 155
Registered: July 2009
Senior Member
Luca,

EclipseLink supports table and native sequencing on most databases. Can you show how you currently configure your sequencing? Is it JPA based?

Doug
Re: native id generator [message #539958 is a reply to message #539232] Mon, 14 June 2010 12:31 Go to previous messageGo to next message
Luca  is currently offline Luca
Messages: 8
Registered: April 2010
Junior Member
Hi Doug,
with Hibernate I had this situation

the MySQL DDL with auto_increment:

Create table attribute (
id_attribute Bigint NOT NULL AUTO_INCREMENT,
name Varchar(50) NOT NULL,
type Int NOT NULL,
UNIQUE (name),
Primary Key (id_attribute)) ENGINE = InnoDB;

the Oracle DDL with sequence:

CREATE SEQUENCE seq_attribute;

Create table attribute (
id_attribute Integer NOT NULL ,
name Varchar2 (50) NOT NULL UNIQUE ,
type Integer NOT NULL ,
primary key (id_attribute)
);

and the mapping of "native" type using auto_increment and cascading
to the configured sequence on database missing autoincrement feature

<hibernate-mapping>
<class name="org.spagic.metadb.model.Attribute" table="attribute">
<comment></comment>
<id name="idAttribute" type="java.lang.Long">
<column name="id_attribute" />
<generator class="native">
<param name="sequence">seq_attribute</param>
</generator>
</id>

...

</class>
</hibernate-mapping>

I'd like to do something simlar with EclipseLink

@Id
@NativeGenerator(name="ATTRIBUTE_GENERATOR", sequenceName="SEQ_ATTRIBUTE", allocationSize = 1)
@GeneratedValue(strategy=GenerationType.NATIVE, generator="ATTRIBUTE_GENERATOR")
@Column(name="ID_ATTRIBUTE")
private Long idAttribute;

and having it work with autoincrement or sequence of given name.
NATIVE is a name from Hibernate, this feature may already exist but I cannot
figure out how to annotate id's to get this behaviour.

The actual project is based on JPA standard, the previous, on Hibernate, wasn't.

Thanks,
Luca

[Updated on: Mon, 14 June 2010 13:06]

Report message to a moderator

Re: native id generator [message #540002 is a reply to message #539060] Mon, 14 June 2010 14:36 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

If you just put @GeneratedValue, then it will work on any database and use whatever mechanism is appropriate for the database.

If you give it the IDENTITY or SEQUENCE generator type, then I believe it will use this type, unless it is not supported, then default to another type.

You could also use TABLE sequencing, which is database independent and support sequence preallocation, so will have better performance than IDENTITY.

If you need a finer level of control, you can use a SessionCustomizer and in code register a Sequence object with the EclipseLink Session's DatabaseLogin based on the DatabasePlatform. Or you could have a different persistence.xml per database.




James : Wiki : Book : Blog : Twitter
Re: native id generator [message #541161 is a reply to message #540002] Fri, 18 June 2010 13:43 Go to previous messageGo to next message
Andrea Zoppello is currently offline Andrea Zoppello
Messages: 48
Registered: July 2009
Member
Hi James,

I'll made a try wiithout specifyng the generator type.

BTW i've not clear wich criteria are used to choose if using,
TABLE, SEQUENCE, OR IDENTITY.

If when i specify "GeneratorType.AUTO" Eclipse link will choose
to use the TABLE approach this won't work for me.

I would use the identity column or the sequence and not the table approach.

What we're figuring here is if eclipse link could provide a simple way *like the one provided by hibernate* where you simply specify a generator-class = native, and you give a sequence name.

When you use such approach hibernte is able to use identity columns or the sequence you've provided a a paremeter to the generator type.

Is this possible with EclipseLink???

Andrea

Re: native id generator [message #541819 is a reply to message #539060] Tue, 22 June 2010 15:50 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

I believe AUTO will use IDENTITY on database platforms that support IDENTITY, and SEQUENCE or TABLE on those that do not.

You can configure a NativeSequence in code using a SessionCustomizer.


James : Wiki : Book : Blog : Twitter
Previous Topic:<transformation> tag documentation?
Next Topic:NullPointerException from Hashtable via IndirectMap
Goto Forum:
  


Current Time: Sat Sep 20 16:09:30 GMT 2014

Powered by FUDForum. Page generated in 0.02176 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software