Comments (25)
This is a known limitation of batch execution. Furthermore, because generated
keys need to be stored in the parameter object, there's no place to store
multiple keys. I'm sure we could think of something, but this would be a
feature request and probably would never see the light of day given the
awkwardness it would add to the API (even if a list of keys was returned, how
would they be mapped to the resulting objects?). I'm open to recommendations
though.
Original comment by [email protected]
on 11 Jun 2010 at 7:54
- Added labels: Type-Feature, Version-Release3.x, Component-SqlMaps
- Removed labels: Type-Defect
from mybatis.
Hello. I want to make multiple row insert, but I don't know how can I retrieve
all generated keys. Insert statement:
<insert id="copy" useGeneratedKeys="true" keyProperty="clone.clonedId"
parameterType="map">
INSERT INTO objects (parameters, owner_id) SELECT parameters, #{newOwnerId} FROM objects WHERE id IN
<foreach collection="clones" item="clone" open="(" separator="," close=")">
#{clone.sourceId}
</foreach>
</insert>
The clones structure is list of CloneParameter class:
public class CloneParameter {
@SuppressWarnings({"UnusedDeclaration"})
private Integer clonedId;
private final Integer sourceId;
public CloneParameter(Integer sourceId) {
Assert.notNull(sourceId);
this.sourceId = sourceId;
}
public Integer getClonedId() {
return clonedId;
}
public Integer getSourceId() {
return sourceId;
}
}
Original comment by [email protected]
on 12 Aug 2011 at 10:00
from mybatis.
Found mysql-specific answer:
http://stackoverflow.com/questions/5054240/mysql-mulitple-row-insert-select-stat
ement-with-last-insert-id
Original comment by [email protected]
on 12 Aug 2011 at 10:50
from mybatis.
Issue 341 has been merged into this issue.
Original comment by eduardo.macarron
on 27 Jan 2012 at 5:37
from mybatis.
Can any one test the snapshot? I did a change in r4613 to fix this issue but
unfortunately neither hsqldb nor derby like generating keys with batches.
Thanks in advance!
Original comment by eduardo.macarron
on 28 Jan 2012 at 10:52
- Changed state: Accepted
Attachments:
from mybatis.
[deleted comment]
from mybatis.
[deleted comment]
from mybatis.
[deleted comment]
from mybatis.
[deleted comment]
from mybatis.
Workaround we've used is broken after update to 3.1.0. Statement and class
we've used is attached. In previous versions setClonedId was called 1 time per
inserted row. Now it's called only one time per batch statement.
Original comment by [email protected]
on 13 Mar 2012 at 11:12
Attachments:
from mybatis.
Dirty fix for this issue. Basically it's the same functionality as it was
before.
I liked previous non-working version :)
Original comment by [email protected]
on 13 Mar 2012 at 11:39
Attachments:
from mybatis.
Alex. Thanks for the report. Can you please open a new issue for this?
Original comment by eduardo.macarron
on 14 Mar 2012 at 2:36
from mybatis.
After having a deeper look I am wondering if you need a workaround. Have you
tried removing the cloneid stuff and accessing directly to creatorId or
whatever the generated parameter is?
Original comment by eduardo.macarron
on 14 Mar 2012 at 2:42
from mybatis.
Actually generated parameter is clonedId. The problem is that it's set only 1
time, instead of N, where N is number of inserts
Original comment by [email protected]
on 14 Mar 2012 at 8:26
from mybatis.
Do we still need to create another issue?
Original comment by [email protected]
on 14 Mar 2012 at 2:25
from mybatis.
Yes please. I think issue #33 is fixed. In your case you are requesting to
JDBC3 support this sentence:
INSERT INTO TABLE values ('Value1'),('Value2'),('Value3')
For the time being, it should work batching the inserts "in the classic way". I
mean
INSERT INTO TABLE values ('Value1')
INSERT INTO TABLE values ('Value2')
INSERT INTO TABLE values ('Value3')
And I suppose the performance will be similar, given that statements are
batched.
Could you try that?
Original comment by eduardo.macarron
on 14 Mar 2012 at 2:41
from mybatis.
Created issue 543 to move conversation there
Original comment by [email protected]
on 14 Mar 2012 at 6:10
from mybatis.
In your case you are requesting to JDBC3 support this sentence:
INSERT INTO TABLE values ('Value1'),('Value2'),('Value3')
For the time being, it should work batching the inserts "in the classic way". I mean
INSERT INTO TABLE values ('Value1')
INSERT INTO TABLE values ('Value2')
INSERT INTO TABLE values ('Value3')
And I suppose the performance will be similar, given that statements are batched.
Could you try that?
I can try, but I don't want to enable batching for all mappers. Can I do for 1
statement only?
Original comment by [email protected]
on 14 Mar 2012 at 6:14
from mybatis.
I suppose that. Is is a session parameter so you can create a session, execute
that statement and close it.
Original comment by eduardo.macarron
on 14 Mar 2012 at 6:26
from mybatis.
[deleted comment]
from mybatis.
[deleted comment]
from mybatis.
For the record I stumbled across a similar issue that is not due to MyBatis,
but due to the driver.
H2 JDBC driver doesn't return all the generated keys, only the last one. See :
https://groups.google.com/forum/?fromgroups=#!topic/h2-database/hoM_bQPMvq0
That means you cannot batch insert entities that have relations in a batch mode
using H2 ! MyBatis or not. And even worse, depending on your mapping choices
and java orchestration, you can end up with entities that are associated to the
wrong records, as only the last generated key is returned mybatis will use this
value on the first item in the list.
However looking at the MySQL Jdbc driver this 'feature' is supported since
years. Other JDBC drivers might as well.
Original comment by [email protected]
on 21 Dec 2012 at 10:45
from mybatis.
Hi Team,
Can you please let me know in which version of mybatis this is fixed.
I want a similar functionality where in I want to do batch execution and set
the generated keys in to object.
Thanks,
Sravanthi
Original comment by [email protected]
on 21 Mar 2014 at 9:27
from mybatis.
Hi. This one was marked as fixed in 3.1.0. So you can use anyone from it to the
latest 3.2.5.
Original comment by eduardo.macarron
on 21 Mar 2014 at 10:06
from mybatis.
hi
so now i can do it like [#10 [email protected]] recommend ?
Original comment by [email protected]
on 21 Mar 2014 at 5:40
from mybatis.
Related Issues (20)
- [mybatis-spring] Mapper scanning should not rely on proprietary @Mapper annotation HOT 1
- Migrations archive unzips to "ibatis-migrations" instead of "mybatis-migrations" HOT 2
- OSGI Support HOT 4
- Remove mapper order restriction when referencing SQL fragment in another file. HOT 40
- Non-existent object id is incorrectly resolved when there is an entry with the same short name. HOT 13
- Allow applying @Transactional to types and interfaces and add a @NoTransaction attribute HOT 4
- Please prioritize issues HOT 1
- Adjust issue's status HOT 1
- Unable to get auto-generated ID with mysql using annotations HOT 1
- Mybatis-spring doesn't propagate exceptions up the stack when using batch mode HOT 17
- how to free temp tablespace allocated for a select stmt return parameter HOT 1
- rollback on BatchExecutor executes batched statements HOT 8
- resultMap doesn't support mapping of unnamed columns by index HOT 2
- mybatis3.0.3 bug in WebService ?? HOT 2
- Annotation-based and AOP-based sessions HOT 3
- Global parameter typeHandler not invoked in case of null values HOT 2
- Exception: Result Maps collection does not contain value for test.User-result HOT 2
- Handling of update counts before result sets in select HOT 8
- configure behavior when all columns values are null for some rows HOT 5
- queryForMap does not use a column as map HOT 14
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mybatis.