Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 283651 - Derby tables are generated with "ALWAYS AS IDENTITY"
Summary: Derby tables are generated with "ALWAYS AS IDENTITY"
Status: CLOSED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Mac OS X - Carbon (unsup.)
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Tom Ware CLA
QA Contact:
URL:
Whiteboard: submitted_patch
Keywords:
Depends on:
Blocks:
 
Reported: 2009-07-15 18:38 EDT by Allan Lykke Christensen CLA
Modified: 2022-06-09 10:10 EDT (History)
5 users (show)

See Also:


Attachments
Patch and Test Case (5.02 KB, patch)
2010-08-26 10:37 EDT, Felipe Duarte CLA
peter.krogh: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Allan Lykke Christensen CLA 2009-07-15 18:38:51 EDT
Build ID: 1.1.2

Steps To Reproduce:
1. Create an entity with an Identity column:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

2. Generate the DDL for the entities

From the produced DDL you'll see that the field is generated as "ALWAYS AS IDENTITY". With this construct it is impossible to use the generated database for unit testing as identity values cannot be overridden. Instead it should have been generated as "DEFAULT AS IDENTITY".


More information:
Example of generated table:

CREATE TABLE mytab (id BIGINT GENERATED ALWAYS AS IDENTITY NOT NULL, title VARCHAR(255), PRIMARY KEY (id))

Expected output:

CREATE TABLE mytab (id BIGINT GENERATED DEFAULT AS IDENTITY NOT NULL, title VARCHAR(255), PRIMARY KEY (id))
Comment 1 Tom Ware CLA 2009-07-22 13:13:08 EDT
Setting Target and priority.  For details of what that means, see: http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines
Comment 2 Damien Bonvillain CLA 2010-05-31 12:26:36 EDT
Showstopper for using EclipseLink with dbunit in an in-memory database scenario.
Comment 3 Felipe Duarte CLA 2010-08-25 17:10:06 EDT
@Allan Lykke Christensen

Same problem here (version 1.1.1).. to resolve this issue I have to create a method to reset the sequence in setUp() .. something like that:

public void resetSequence(Connection connection, String table, String IdField) throws SQLException {
		Statement stmt = connection.createStatement();
		stmt.executeUpdate(
				"ALTER TABLE " + table + " ALTER COLUMN " + IdField + " RESTART WITH 1");
		stmt.close();
}

and in the setUp():

public setUp() {
.
.
.

   resetSequence(connection, "TABLE_NAME", "TABLE_ID");
   FlatXmlDataSet dataset = new FlatXmlDataSet(new File(
				"dataset.xml"));
   DatabaseOperation.CLEAN_INSERT.execute(connection, dataset);
}

@Tom Ware

is the bug confirmed?
Comment 4 Tom Ware CLA 2010-08-26 08:03:48 EDT
It sounds like the bug is legitimate.

I'd like to encourage those that would like to see this bug fixed to vote for it as votes are one of our main criteria in determining which community bugs to fix.
Comment 5 Felipe Duarte CLA 2010-08-26 09:16:42 EDT
Tom,

looking at the source code I think I've found the solution and it's quite simple .. The class DerbyPlatform.java on line 215 has the following code:

    /**

     * INTERNAL:

     * Append the receiver's field 'identity' constraint clause to a writer.

     */

    public void printFieldIdentityClause(Writer writer) throws ValidationException {

        try {

            writer.write(" GENERATED ALWAYS AS IDENTITY");

        } catch (IOException ioException) {

            throw ValidationException.fileError(ioException);

        }

    }


to resolve the issue the code must change the string "ALWAYS" to "BY DEFAULT" and it will look like these:

    /**

     * INTERNAL:

     * Append the receiver's field 'identity' constraint clause to a writer.

     */
    @Override // I think the annotation is missing here

    public void printFieldIdentityClause(Writer writer) throws ValidationException {

        try {

            writer.write(" GENERATED BY DEFAULT AS IDENTITY");

        } catch (IOException ioException) {

            throw ValidationException.fileError(ioException);

        }

    }

I'm trying to compile to test it .. Then I send the results ..

[]s
Comment 6 Tom Ware CLA 2010-08-26 09:24:09 EDT
I am going to tag this as having a "submitted patch". 

Approximately every week, I go through the bugs with that tag and do some work on the ones that have new information.  (Essentially, submitting a good quality patch is a good way to get committer eyes on a bug more quickly)

Assuming your patch works, you will have a very good workaround.  You can subclass DerbyPlatform and override the method.  You can then point at your new class with persistence unit property: eclipselink.target-database=<fully qualified class name>
Comment 7 Felipe Duarte CLA 2010-08-26 10:37:30 EDT
Created attachment 177530 [details]
Patch and Test Case

The files contains a patch to correct the bug and a test case to verify the correctness
Comment 8 Tom Ware CLA 2010-09-14 11:22:07 EDT
Checked in submitted patch as submitted.

Tested manually with JPA IDENTITY tests

Reviewed by: Tom Ware - reviewed customer submitted fix
Comment 9 Peter Krogh CLA 2010-12-08 14:12:15 EST
iplog+ moved to patch.
Comment 10 Eclipse Webmaster CLA 2022-06-09 10:10:26 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink