History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: APF-718
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Matt Raible
Reporter: Bas Cancrinus
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
AppFuse

Unique constraint violated when running test-all

Created: 04/Apr/07 06:45 AM   Updated: 15/Sep/07 01:07 PM
Component/s: Build, Test, or Deploy Process
Affects Version/s: 1.9.4
Fix Version/s: 2.0 Final

File Attachments: 1. Text File APF-718.patch (3 kb)
2. XML File sample-data.xml (3 kb)
3. Java Source File UserActionTest.java (2 kb)
4. Java Source File UserDaoTest.java (4 kb)
5. Java Source File UserExistsExceptionTest.java (2 kb)

Environment: Oracle 9
Issue Links:
Duplicate
This issue duplicates:
APF-689 Roles not loaded into DB with Oracle Major Resolved
 


 Description  « Hide
When RoleDaoTest.testAddAndRemoveRole() runs on Oracle 9 and the default sample-data.xml is loaded then the following exception is thrown: "ORA-00001: unique constraint (...) violated". The pk values that the sequence generates collide with the values in sample-data.xml. As a workaround replace the values in sample-data.xml with high values.

A better approach would be to reserve low values for the test data and create the sequence with the "minvalue ... start ..." clause. However, hbm2ddl only seems to pick up the "sequence" (name) parameter. Therefore I suggest to include the former approach into v1.9.5. I suggest to include release note as well that warns the user against possible collisions of high pk values.

See the following thread for previous discussions about this issue:
http://www.archivesat.com/AppFuse/thread556393.htm



 All   Comments   Change History   FishEye      Sort Order:
Matt Raible - 04/Apr/07 09:55 AM
Someone recently suggested we use negative numbers for the sample-data.xml primary keys. It sounded like a good idea. There was a discussion about it on the mailing list. Unfortunately, I can't seem to find the thread.

Bas Cancrinus - 04/Apr/07 04:03 PM
Using negative pk's for test records is a good idea. I think you referred to the following thread: http://www.mail-archive.com/users@appfuse.dev.java.net/msg03053.html I'll test it tomorrow and post my results back to this issue.

Bas Cancrinus - 05/Apr/07 07:46 AM
Changed user ids in unit tests to a negative value.

Bas Cancrinus - 05/Apr/07 07:47 AM
Changed user and role ids in the sample data to a negative value.

Bas Cancrinus - 05/Apr/07 07:49 AM
Unittests ran successfully with negative pk values (ant clean setup test-all). Modified src and test data are attached to this issue.

Matt Raible - 10/Apr/07 12:02 AM
I believe this is related to APF-689.

While specifying negative values for the sample data works, it seems kinda ugly to me - especially since the "user" and "admin" roles will likely be used in the production version of the application. It seems wrong that "seed data" has to be so hackish.

Bas Cancrinus - 14/Apr/07 04:45 AM
I agree that negative id's don't look very pretty. My usual workaround for this issue is to create a shadow schema which is dedicated to DBUnit tests. I dislike this approach now, because you are not testing against the prod schema and updates to the config files and the schema have to be carried out twice.

My favorite solution would be to annotate a pojo class like

/**
  * @hibernate.database-object="CREATE SEQUENCE HIBERNATE_SEQUENCE MINVALUE X START X"
  */

in conjunction with a pojo method annotation like

/**
  * @hibernate.id column="id" unsaved-value="null"
  * @hibernate.generator class="sequence"
  */

so that all id's < X are reserved for DBUnit. However, there are 2 obstacles:
1) I'm not sure whether java2hbm currently supports a tag like "@hibernate.database-object"
2) hbm2ddl would generate the create sequence statement twice. The method annotation DDL must be suppressed.

See the following post for an interesting workaround:
http://forum.hibernate.org/viewtopic.php?p=2320168#2320168

For more information on <database-object> see:
http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-database-object


Matt Raible - 28/Aug/07 02:59 PM
Moving to future release. I suggesting using the following workaround in the meantime:

Set your hibernate_sequence to a high enough value so that future insertions do not produce unique key constraint violations. For instance, if your highest key value in any of your tables is 182974, then the following statement would work for you -

SQL> alter sequence hibernate_sequence increment by 182975 minvalue 0;

Rob Hills - 09/Sep/07 01:54 PM
This issue also affects at least one other database: PostgreSQL.

As discussed in the AppFuse User list:

http://www.nabble.com/Error-executing-database-operation%3A-CLEAN_INSERT-tf3433993.html

it is proposed that the best solution is to have separate test and production ("default") data files with the test data having negative keys.

I will try and submit a patch for this ASAP.

Rob Hills - 13/Sep/07 09:05 AM
This patch changes the appfuse-maven-plugin to produce test data containing negative integers for primary keys and also changes the generated struts action-test java code to expect negative IDs in existing test data.

Although the attached patch fixes "showstopper" problems for my own project, I believe several more things need to be done to properly close this issue:

1. Rename "sample-data.xml" to "test-data.xml" and modify all elements of appfuse that use this file;

2. Modify appfuse to support preload a DB with data during a production deployment. Such data could be defined in a "default-data.xml" file possibly located in src/main/resources.

Matt Raible - 15/Sep/07 12:49 PM
Changed test data to use negative keys to avoid unique constraint violations on Oracle. Also added "prod" profile to allow loading default data and to serve as a placeholder for other production-related settings. To build with "prod" profile, use "mvn package -P prod". Currently, this profile only contains a different data file (with positive primary keys) for DbUnit.

NOTE: I chose to keep the name "sample-data.xml" instead of changing it to test-data.xml. Changing the filename was too risky of a change this late in the game of releasing 2.0.

Matt Raible - 15/Sep/07 01:07 PM
NOTE: I did *not* update the tutorials or documentation to use negative numbers for primary keys in sample-data.xml. Hopefully this won't be an issue. If it is, please enter a new issues and we'll update the tutorials.