| native id generator [message #539060] |
Wed, 09 June 2010 10:08  |
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 #539958 is a reply to message #539232] |
Mon, 14 June 2010 08:31   |
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 09:06] Report message to a moderator
|
|
|
| Re: native id generator [message #540002 is a reply to message #539060] |
Mon, 14 June 2010 10:36   |
James Sutherland Messages: 1877 Registered: July 2009 |
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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.01680 seconds