Meaningless Drivel is fun!
The moose likes JDBC and Relational Databases and the fly likes derby with sequences and currentvalue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "derby with sequences and currentvalue" Watch "derby with sequences and currentvalue" New topic

derby with sequences and currentvalue

Yvan Sand

Joined: Mar 14, 2011
Posts: 3
hello, actually we use oracle and would like to use derby as the embedded db for our junit testing.

now we have a problem with sequences and I haven't found a solution yet, maybe we use it the wrong way?! I hope somebody can help...

we have the following sql script which creates two sequences and two tables. after that we insert some data using the sequences, but there is something wrong with the currentvalue...

the problem is that when calling "next value for seq_type" the sequence is ok and is incremented by 1, but the currentvalue is wrong and contains the value 6? when the sequence reaches 6, the currentvalue changes to 11.

can someone telling me what's going on and how must the script looks like to work correctly with sequences?

we're using derby for this tests.

thanks for helping!
Martin Vajsar

Joined: Aug 22, 2010
Posts: 3733

Though I'm not acquianted with derby, it seems to me that you're fetching the "current value" from some dictionary table. You should use proper SQL construct for that. If next value FOR seq_text is valid on derby to fetch next value from a sequence, I'd say that current value FOR seq_text should provide you with the current value for your transaction.

Edit: I was probably assuming too much. From a quick peek into Derby documentation it seems there is no support for the "current value" of the sequence. The construct you're using is mentioned on forums, but the value is obtained from a dictionary view and is not mentioned in docs. I think this works for someone by accident, Derby sequences apparently were not designed to provide current values. You'll probably need to store the "next value" into a variable and use it with both parent and child tables.
I agree. Here's the link:
subject: derby with sequences and currentvalue
jQuery in Action, 3rd edition