Hi All
I'm new to Hibernate. It would be great if someone can guide me in the right way. My issue is:
I'm saving about 100 records (maximum is 1000) from the CSV to the DB. The CSV has 7 columns. One of the columns is "PracticeId". Before saving to the DB, I must retrieve 2 more values from a different table based on PracticeId, and then save to the DB. So right now I'm querying the DB about 100 times to retrieve the 2 columns. This takes about 16-17 seconds, which is rather slow. So can anyone help me in optimizing the fetching, so that I can reduce the time drastically. I have some parts of my code here:
pList is the ArrayList(Letter) that has the CSV values
Letter is the class that has to be saved
Practice is the class that has the other 2 values
Code:
for (Letter letter : pList) {
// Get Demo Id & Id by PracticeId
Practice practice = new Practice();
practice = importDao.getPracticeByPracticeId(practiceLetter.getPracticeId());
// Set Demo ID & Id to PracticeLetter
if (null != practice) {
letter.setDemoId(Long.toString(practice.getDemonstrationId()));
letter.setId(Long.toString(practice.getId()));
}
Then I save the pList to the DB.
This is the method that is called about 100 times (once each for each row in the CSV)
Code:
public Practice getPracticeByPracticeId(final String practiceId) throws DataAccessException {
return (Practice) this.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
{
Criteria criteria = session.createCriteria(Practice.class);
criteria.add(Restrictions.eq("practiceId", practiceId));
return criteria.uniqueResult();
}
});
}
Letter & Practice have no relation and pretty straightforward classes.
Hibernate Settings:
Code:
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
Hope I'm clear in my question. Please help me in this regard.
Thanks
Harry