0

I have got the type Word with the property Topics which is a List object. I have two tables in the database: Words and Topics. I want write a word in Words table and store each topic with its corresponding idWord in Topics. For the insert statement of the word I use generatedKey and keyProperty to obtain the idWord that mySQL assign to the word.

But I don't know how to do this, I've read iBATIS documentation but this is too brief with INSERT statements, I could get data from Words and Topics to Word type with but not instead.

Thanks a lot!!

P.S. Could you suggest more iBATIS documentation??

Jonás
  • 1,459
  • 4
  • 27
  • 43

1 Answers1

2

You can use selectKey tag inside your first table inert query; this will return identity column value that got generated. Use this id to populate the second table.

 <insert id="InsertLineItem" parameterClass="LineItem">
  INSERT INTO [LinesItem] 
  (Order_Id, LineItem_LineNum, Item_Id, LineItem_Quantity, LineItem_UnitPrice)
  VALUES
  (#Order.Id#, #LineNumber#, #Item.Id#, #Quantity#, #Item.ListPrice#)

  <selectKey resultClass="int" keyProperty="id" >
    SELECT @@IDENTITY AS ID
  </selectKey>
  </insert>
Flexo
  • 87,323
  • 22
  • 191
  • 272
Bala
  • 4,427
  • 6
  • 26
  • 29
  • According to https://stackoverflow.com/questions/1769688/howto-return-ids-on-inserts-with-ibatis-with-returning-keyword the selectKey is executed before the sql. See as well https://ibatis.apache.org/docs/dotnet/datamapper/ch03s03.html section 3.3.3, sorry saw your example right below the text. – Barny Apr 05 '19 at 12:03