I am saving entries in table only if they already exist:
public static Email getOrCreate(Session sess, String address) {
if (address==null) return null;
List l = sess.createQuery("SELECT email FROM Email email WHERE email.address=:address").setString("address", address).list();
if (l.size() > 0)
return (Email) l.get(0);
Email email = new Email(address);
sess.save(email);
return email;
}
I am not satisfied with the speed of this procedure. It takes me about 5 mins to save 5000 complicated records and 50% of time is occupied by this function. My hibernate cache setting is:
cfg.setProperty(“hibernate.cache.provider_class”, “org.hibernate.cache.EhCacheProvider”);
Of course I could create static map and sync it with database sometimes, but it looks a bit ugly. Can hibernate do something like this? Thanx.
Do it in batch:
Make sure, though, that your database allows 5000 elements in an
INclause. Oracle, for example, limits them to 1000. But you could still only execute 5 request instead of 5000.That said, 5 minutes to do that looks like a whole lot to me. Do you have a database index defined on email.address?