Question: MyBatis 3 - Spring

I want to insert list of items and for every item id has to generate from "TVA\_UPSELLADMIN\_CHANNEL\_SEQ.nextVal" but i am getting .xml validation error that you can't sub-child "selectKey" inside "foreach".

```
<insert id="insertServiceMappings" parameterType="java.util.List">

INSERT  
<foreach collection="list" item="channel" index="index" >

<selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT TVA_UPSELLADMIN_CHANNEL_SEQ.nextVal from dual
        </selectKey>

into tva_upselladmin_channel (id,source_id, service_id, name) values (#{id}, #{channel.sourceId}, #{channel.serviceId}, #{channel.name})
</foreach>
</insert>

```

Answer: 
Little late, but accepted answer is not 100% accurate. (Would just make this a comment, but no rep.) I would do this like so:

Assuming you have

```
class User < ActiveRecord::Base
  has_many :profiles  
  ...
  def latest_profile
    profiles.order(...) # query to get whatever record it is you want
  end
end

```

your simple\_fields\_for can just be

```
<%= f.simple_fields_for :profiles, f.object.latest_profile do |p| %>

```

Note the pluralization of :profiles, and that we don't need to clutter the controller with an additional instance variable because you can access the object in the form. (Using the singular will work I believe, but you will not get params with a key in the form of :profiles\_attributes, meaning even more code to account for unique params.)
The edit action: 

```
   class user > ApplicationController
      ... 
      def edit
        @profile = @user.profile.order("saved DESC").first
      end
      ...
    end

```

The working form:

```
<%= simple_form_for(@user) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :lang %>
    <%= f.input :firstname %>
    <%= f.input :lastname %>
    <%= f.input :email %>
    <%= f.input :born %>
    <%= f.input :gender %>
    <%= f.simple_fields_for :profile, @profile do |p| %> # the magic needed here
        <%= p.input :postal_code %>
        <%= p.input :core %>
        <%= p.input :daytime %>  
        <%= p.input :style %>
    <% end %>
    <% if @is_new %>
      <%= f.simple_fields_for :status do |s| %>
        <%= s.input :entered, as: :hidden, input_html: { value: Time.current } %>
      <% end %>
    <% end %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

```