|
|
|
[
Permlink
| « Hide
]
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.
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.
Changed user ids in unit tests to a negative value.
Changed user and role ids in the sample data to a negative value.
Unittests ran successfully with negative pk values (ant clean setup test-all). Modified src and test data are attached to this issue.
I believe this is related to
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. 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 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; 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. 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. 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. 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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||