Generating Primary Keys wit Optimalj

OptimalJ currently doesn’t generate code for database sequences which you can use to create unique values for primary keys in your tables.

So I did some research to see how other people solve this problem. One person suggested counting the number of records in a table, increase that number by one, and use that as your primary key. Brilliant! Should perform really well, and no concurrency problems to be expected.

I found one real solution on the optimalj forum, which was to create a Domain class (this translates into a database table) that holds ‘sequence’ records. One record per sequence, with a name and a value column. Then on the Domain Class you can add a DomainOperation to query the next sequence value, e.g., nextVal. The example on the forum described the situation when using EJBs, but I’m trying to generate DAOs, because i don’t have the memory in my laptop to run an ejb container.

After generating the application model and the code model i have to implement the code for the nextVal operation (get current value from table, add one, etc), but there’s a problem. Where is it? I can’t find the nextval stub anywhere, i must have checked 20 java files…

blog comments powered by Disqus