0

I'm new to Hibernate. I needed the below scenario for my project.

For example am inserting 20 records data of class TestHBDemo.

hbsession = HibernateUtil.getCurrentSession();
tx = hbsession.beginTransaction();
for(int index = 0; index < 20; ++index){
    TestHBDemo obj = new TestHBDemo();
    //setting data

    hbsession.save(obj);
}
tx.commit();

How to insert only 1 - 10 and 15 - 20 records by omitting in-between records

hbsession = HibernateUtil.getCurrentSession();
tx = hbsession.beginTransaction();
for(int index = 0; index < 20; ++index){
    TestHBDemo obj = new TestHBDemo();
    //setting data

    hbsession.save(obj);

    if(somecondition)
    // setting save points
}
//omitting unnecessary insertions
tx.commit();

Please, provide me the solution preferably in hibernate rather than using javax.sql.

Thanks.....

NamingException
  • 2,388
  • 1
  • 19
  • 41

2 Answers2

-1

I acheived it by using below code:

hbsession = HibernateUtil.getCurrentSession();
    tx = hbsession.beginTransaction();
    for(int index = 0; index < 20; ++index){

    TestHBDemo obj = new TestHBDemo();
    //setting data

        hbsession.save(d5campaingObj);
        if(index == 9){
            hbsession.flush();
            hbsession.clear();
        }
        if(index == 14){
            hbsession.clear();               
        }
    }
tx.commit();

But am waiting for better approach from experts. Thanks....

NamingException
  • 2,388
  • 1
  • 19
  • 41
  • I don't understand what this has to do with hibernate. It's just a conditional statement inside your loop? What kind of feature would you like to know about in hibernate that would replace that? – Affe Dec 06 '12 at 07:15
  • i added only some part of the answer. From above, am able to persist upto 10 records by flush(), and omitted next 5 records. I did it by using fush(). But, am not able to save records as savepoints in JDBC. Because, after flushing, am not able to delete those particular flushed records and to insert new records before committing... If am not clear please ask for clarity.... – NamingException Dec 06 '12 at 08:07
-1
hbsession = HibernateUtil.getCurrentSession();
tx = hbsession.beginTransaction();
for(int index = 0; index < 20; ++index){
    TestHBDemo obj = new TestHBDemo();
    //setting data
    **if(index>10 && inxex<15)
        continue**;
    hbsession.save(obj);
    // setting save points
}
tx.commit();
NamingException
  • 2,388
  • 1
  • 19
  • 41
zeus
  • 1