[Play 2.x] Ebean ORM and batch insert

1,203 views
Skip to first unread message

Antonello Pastena

unread,
Oct 5, 2013, 12:00:36 PM10/5/13
to play-fr...@googlegroups.com
Hi all, 
I use Play Framework with Ebean ORM for connecting with my database.

Through a post i send some data to an URL doing a POST request.

The data is in this format: dataA;dataB;dataC;

Now i split the string using the ; separator and i would like to insert them in the db, doing something like this in the controller

for ( )
      MyModel mymodel = new MyModel()
      mymodel.field=data[i]
      mymodel.save()
endfor

Now i noticed that in this case are performed 3 different insert in the database. I'd like to perform a batch insert.

I saw that is possible to do something like what i want


But it does work with CallableSQL but i wasn't able to make it work with model save() instruction.

aki...@gmail.com

unread,
Mar 20, 2015, 6:34:50 AM3/20/15
to play-fr...@googlegroups.com
Hello Blindstealer,

It's late, still if it helps - you may try like below fashion -

      Transaction txn = Ebean.beginTransaction();
      try {
                txn.setBatchMode(true);
                txn.setBatchSize(10);
                // ** turn off getGeneratedKeys if you don't need to use the
                // mymodel bean instances after you have inserted them **
                txn.setBatchGetGeneratedKeys(false);

                for ( )
                      MyModel mymodel = new MyModel()
                      mymodel.field=data[i]
                      mymodel.save()
                endfor

                txn.commit();
                result = true;

            } catch(Exception e) {
                //exception handler
            } finally {
                txn.end();
            }


Thanks
Khalid
Reply all
Reply to author
Forward
0 new messages