allenskd
allenskd

Reputation: 1805

Best way to insert a good amount of records in hibernate

I'm using hibernate + play! framework at work, is there a "best practice" on inserting a good amount of records using hibernate? They are around 6,000 to 10,000 per text file so I don't know if Hibernate is going to choke on the job or throw an exception.

Any suggestion let me know, let me know if I have to explain more

Upvotes: 12

Views: 25802

Answers (5)

Kartoch
Kartoch

Reputation: 7779

From *Java Persistence and Hibernate" (Manning) and following a comment from Pangea, use a stateless session (which doesn't have a persistence context cache) :

StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
    Item item = new Item(...);
    session.insert(item);
}
tx.commit();
session.close();

Upvotes: 24

Qianyue
Qianyue

Reputation: 1777

Just some corrections of the codes in the answer of Kartoch.

According to Batch Procession, "The insert(), update() and delete() operations defined by the StatelessSession interface are considered to be direct database row-level operations. They result in the immediate execution of a SQL INSERT, UPDATE or DELETE respectively. They have different semantics to the save(), saveOrUpdate() and delete() operations defined by the Session interface."

No more save(), flush(), clear() for StatelessSession. The code should be like this :

StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();

for ( int i=0; i<100000; i++ ) {
  Item item = new Item(.....);
  session.insert(item );
}    

tx.commit();
session.close();

Finally, Here is a discussion of the difference between the normal batch inserting and the StatelessSession insert : Using StatelessSession for Batch processing.

Upvotes: 3

HosseinSafy
HosseinSafy

Reputation: 139

Best is to use StatelessSessions. Consider the following example from (http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/batch.html):

StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();

ScrollableResults customers = session.getNamedQuery("GetCustomers")
    .scroll(ScrollMode.FORWARD_ONLY);
while ( customers.next() ) {
    Customer customer = (Customer) customers.get(0);
    customer.updateStuff(...);
    session.update(customer);
}

tx.commit();
session.close();

Upvotes: 2

chad
chad

Reputation: 2562

You can always get a Connection object directly in case you want to do the inserts outside of hibernate.

Connection connection = DB.getConnection();

Upvotes: -5

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

Just open your session and transaction.

Add all elements in the save of the session.

Then commit the transaction.

//Remember to effective handler errors
public void saveAll(List<Object> list) throws Exception{
Session s = HibernateUtil.openSession();
Transaction tx = s.beginTransaction();
for(Object obj : list)
 s.save(obj);
tx.commit();
s.flush();
s.close();
}

Upvotes: 1

Related Questions