create a insert with multiple rows

398 views
Skip to first unread message

Avinash Kundaliya

unread,
Jun 23, 2016, 4:16:10 AM6/23/16
to Ebean ORM
Hi,
I want to insert multiple models and would like to have a single statement to insert. The best i have right now is to use transactions, is there a way to have multiple inserts ?

Current alternative:
    Transaction txn = Ebean.beginTransaction();
    try {
      txn.setBatchMode(true);
      txn.setBatchSize(30);
      txn.setBatchGetGeneratedKeys(false);

      posNames.stream().forEach(posName -> {
        Model model = new Model(UUID.randomUUID());
        Ebean.save(model);
      });
      txn.commit();
    } catch(Exception e) {
      //exception handler
    } finally {
      txn.end();
    }

yuqi...@gmail.com

unread,
Jun 23, 2016, 5:20:19 AM6/23/16
to Ebean ORM
Hi,
Ebean can save a collection.you can save like this:
List<Model> modelList = new LinkedList<>();
Ebean.save(modelList);

在 2016年6月23日星期四 UTC+8下午4:16:10,Avinash Kundaliya写道:

Avinash Kundaliya

unread,
Jun 23, 2016, 7:44:40 AM6/23/16
to Ebean ORM
unfortuntely, Ebean.save doesn't take a collection
and Ebean.saveAll exists which takes a collection but it still makes single query for each insert (which is very weird and not the expected behavior IMO)
any other pointers ?
I tried Ebean.insertAll as well which makes a single query for each insert. Weird or ?

Regards,
Avinash

Daryl Stultz

unread,
Jun 23, 2016, 8:13:45 AM6/23/16
to eb...@googlegroups.com
On Thu, Jun 23, 2016 at 7:44 AM, Avinash Kundaliya <avi...@avinash.com.np> wrote:
unfortuntely, Ebean.save doesn't take a collection
and Ebean.saveAll exists which takes a collection but it still makes single query for each insert (which is very weird and not the expected behavior IMO)

Can you describe what you would expect? Write an example insert query that you would prefer Ebean to generate?

/Daryl 

Rob Bygrave

unread,
Jun 23, 2016, 1:40:39 PM6/23/16
to ebean@googlegroups
I tried Ebean.insertAll as well which makes a single query for each insert. Weird or ?

You should post up the log.

Ebean for logging ... posts the statement for each row in a JDBC batch so yes it might look like there is a statement for each insert but in fact that is just the logging and in fact there was only 1 statement prepared (and then batch add for each).

So post up your log and we can check (there is some differences when using JDBC batch).


Cheers, Rob.

--

---
You received this message because you are subscribed to the Google Groups "Ebean ORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages