Question

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.....

Was it helpful?

Solution

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....

OTHER TIPS

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top