Hibernate/JBoss & Bulk Insert?

Discussion in 'Java' started by Steve, Sep 15, 2008.

  1. Steve

    Steve Guest

    I am looking for a way to do a bulk insert of thousands of objects using JBoss and Hibernate. I currently have an injected
    EntityManager with a JTA PersistenceContext in a Stateless Session Bean and am doing something like

    for(int i = 0; i < num; i++){
    MyObject myObject = createMyObject(num, ...);
    entityManager.persist(myObject);
    }

    (in a try/catch block) which is slow.

    I have unsuccessfully tried to use entityManager.getTransaction() to begin, commit, etc. transactions. I have also unsuccessfully
    tied to convert the EntityManager to a HibernateEntityManager and use Hibernate transactions. I've also tried to use
    @TransactionAttribute(TransactionAttributeType.REQUIRED) but it made no significant difference (it's slightly faster).

    Is there a way to save multiple objects in a "single" database call, or at least a few?

    BTW, Having something like entityManager.persist(collection) would be nice.

    Thanks.

    Steve
    Steve, Sep 15, 2008
    #1
    1. Advertising

  2. On Sep 15, 3:43 pm, "Steve" <> wrote:
    > I am looking for a way to do a bulk insert of thousands of objects using JBoss and Hibernate. I currently have an injected
    > EntityManager with a JTA PersistenceContext in a Stateless Session Bean and am doing something like
    >
    >   for(int i = 0; i < num; i++){
    >     MyObject myObject = createMyObject(num, ...);
    >     entityManager.persist(myObject);
    >   }
    >
    > (in a try/catch block) which is slow.
    >
    > I have unsuccessfully tried to use entityManager.getTransaction() to begin, commit, etc. transactions. I have also unsuccessfully
    > tied to convert the EntityManager to a HibernateEntityManager and use Hibernate transactions. I've also tried to use
    > @TransactionAttribute(TransactionAttributeType.REQUIRED) but it made no significant difference (it's slightly faster).
    >
    > Is there a way to save multiple objects in a "single" database call, or at least a few?
    >
    > BTW, Having something like entityManager.persist(collection) would be nice.


    You'll get much better performance for bulk operations by realizing
    that your database *does not store Objects*, just rows, and treating
    data appropriately. In this case, iBATIS or straight JDBC will
    probably be much, much faster than JPA, as you can skip all of the
    steps involved in converting your data to objects and back to data.
    In fact, you may be better off skipping the app entirely and using
    your DB's equivalent of COPY <tablename> or LOAD <tablename> to bulk-
    load data from a flat file...

    -o
    Owen Jacobson, Sep 15, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean
    Replies:
    6
    Views:
    397
  2. =?Utf-8?B?U2hpanUgUG95aWxpbA==?=

    BULK INSERT WITH ADO.NET & ORACLE

    =?Utf-8?B?U2hpanUgUG95aWxpbA==?=, Apr 25, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    6,274
    Sathya.Pai
    Sep 9, 2009
  3. Niraj
    Replies:
    2
    Views:
    10,734
    Jim Kennedy
    Mar 3, 2004
  4. ABC
    Replies:
    1
    Views:
    734
    Galin iliev
    May 11, 2006
  5. Steve
    Replies:
    0
    Views:
    562
    Steve
    Sep 16, 2008
Loading...

Share This Page