start a transaction
insert/update the user
loop over the notes and insert them
end the transaction
Nathan
> The
> problem is, the id is auto generated; an auto_increment column and I
> don't think there's a way MyBatis can return the generated value back
> to the calling method after insert.
MyBatis supports as well auto generated keys, please see page 24-25 of
the manual[1] for more detailed informations:
"useGeneratedKeys: (insert only) This tells MyBatis to use the JDBC
getGeneratedKeys method to retrieve keys generated internally by the
database (e.g. auto increment fields in RDBMS like MySQL or SQL
Server). Default: false"
Good luck!
Simo
[1] http://mybatis.googlecode.com/svn/trunk/doc/en/MyBatis-3-User-Guide.pdf
http://people.apache.org/~simonetripodi/
http://www.99soft.org/
That said, doing a 10,000 record batch without commits would strain
even a pretty beefy database server, but I wouldn't expect MyBatis to
be the breaking point there.
Larry
If you are doing a clean insert such as:
INSERT INTO categories
(category_name)
VALUES (#{categoryName})
where the table look like this:
CREATE TABLE IF NOT EXISTS categories (
category_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
category_name VARCHAR(128) NOT NULL,
UNIQUE INDEX ( category_name )
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
You can indeed use 'getGeneratedKeys' to grab the new ID, in this case category_id.
However if you need to handle duplicate inserts, you will need to use the 'selectKey' statement as follows:
/* dictionaries.Category.insertObject */
INSERT INTO categories
(category_name)
VALUES (#{categoryName})
ON DUPLICATE KEY UPDATE
category_id=LAST_INSERT_ID(category_id);
<selectKey resultType="Integer" keyProperty="categoryID" order="AFTER">
SELECT LAST_INSERT_ID();
</selectKey>
because 'getGeneratedKeys' will return the next ID in the sequence and not the ID of the duplicate. Obviously a bug somewhere in MySQL or JDBC.
Cheers
François
Would I be right in saying that putting 'jdbcType=...' in the mapper files for nullable columns in no longer a requirement in mybatis-3.0.1?
Cheers
François