I want to use HSQLDB as an embedded database but am having trouble getting it to auto-increment.
As far as I understand, [CALL] IDENTITY()
can be used to get the last primary key value. However, experiments through both iBatis and HSQLDB's DatabaseManagerSwing
continually return a 0 value.
How can I get auto-incrementation to work with HSQLDB?
Edit:
I didn't mention that I'm using DDLUtils to autogenerate tables. The following does not suit HSQLDB:
<?xml version="1.0"?>
<!DOCTYPE database SYSTEM "http://db.apache.org/torque/dtd/database.dtd">
<database name="testdb">
<table name="users">
<!-- using autoincrement attribute below causes
"primary key already exists" exception -->
<column name="id" type="INTEGER" primaryKey="true" />
<column name="username" type="VARCHAR" size="30" />
<column name="password" type="VARCHAR" size="100" />
</table>
</database>
Also, here is the iBatis SQL map used for the domain class:
<insert id="insertUser" parameterClass="user">
<selectKey keyProperty="id" resultClass="int">
CALL IDENTITY()
</selectKey>
INSERT INTO USERS
( USERNAME, PASSWORD )
VALUES
( #username#, #password#)
</insert>
If you use an ORM, they will perform the identity column work for you. sormula makes it easy with an annotation. See org.sormula.tests.identity package within the project for examples.
Row class defined:
From org.sormula.identity.tests.InsertTest:
HSQLDB is included in the tests.
Here's an example that prints out
on my machine: