Auto increment and unique id's
Martin Holst Swende <[email protected]>
| Newsgroups | gmane.comp.java.orm.simpleorm |
|---|---|
| Message-ID | <[email protected]> |
Hi,
The tables I was using were MySQL tables with auto-increment primary
keys. As I have gathered now from earlier discussions, that does not
work very well, so I started using the key-generation facilities instead.
* The generator did not notice that my tables had fields that were
AUTO_INCREMENT. So, the code 'out of the box' did not really work. When
I created fields (with id's that I set myself) the id's I specified were
silently ignored. Bug there - should at least give warning at some point.
* Using ses.createWithGeneratedKey(meta) does not work out-of-the box
either, despite documentation that it defaults to SSELECT_MAX -
nullpointers were thrown (both mode and params are null). To solve that,
I added the following to SFieldScalar:
/**
* If generator is not already configured,
* this method initalizes this field to use Default generator mode,
which is SSELECT_MAX
* @return the mode
*/
public SGeneratorMode getGeneratorModeOrDefault()
{
if(this.generatorMode == null && this.theGenerator == null)
this.setGeneratorMode(SGeneratorMode.SSELECT_MAX,
this.fieldName.concat("_seq_no").toLowerCase());
return getGeneratorMode();
}
And modified the SGenerator thus:
public static SGenerator theGenerator(SFieldScalar fld) {
SGenerator gen = (SGenerator)fld.getTheGenerator();
if (gen == null) {
+ switch (fld.getGeneratorModeOrDefault()) {
- switch (fld.getGeneratorMode()) {
case SSELECT_MAX:
gen = new SGeneratorSelectMax(fld,
(String)fld.getGeneratorParameter()[0]); break;
case SSEQUENCE:
gen = new SGeneratorSequence(fld,
(String)fld.getGeneratorParameter()[0]); break;
case SINSERT:
gen = new SGeneratorInsertIdentity(fld,
(String)fld.getGeneratorParameter()[0]); break;
}
fld.setTheGenerator(gen);
}
return gen;
}
Should I commit my fix ?
* Remaining question: my fields are still AUTO_INCREMENT. But it works
anyway, I guess since auto_increment sets exactly the value that
SSELECT_MAX expects - they happen to correlate, which I guess is a prime
candidate for future bugs. What is "The Best" way to handle this: Should
I remove the AUTO_INCREMENT - property from the fields in MySql and rely
only on the generator?
--
Martin Holst Swende ................. MSC Konsult AB
tel: +46(0)70 9519098 ............... Vasagatan 52
[email protected] .......... 111 20 Stockholm
------------------------------------
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/SimpleORM/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/SimpleORM/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:[email protected]
mailto:[email protected]
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/