Hi,
I am getting probelm while I am using hibernate APIs for native query.I am getting stale data.
Probelm statement is:
1. Let say I have two table : Table1 and Table2
2. In jsp I have two buttons: "Insert" and "Show List"
3. For example let say both table have 2 rows and if I click on "Show List" it is fetching data using hibernate native query API's so at this point it is showing two records thats fine for me.
4. Now if I click on "Insert" button it is inserting 1 record in both the table (Table1 and Table2).
5. Now I check into the database both the tables are updated with new inserted record, and at this point if I click on "Show List" button then it is fetching only 2 records not the third one which is inserted recently.
6. To fetching data from Table1 and Table2 I am using session.createSQLQuery("QueryString").
here I am using simple join in SQL Query.
7. This problem I am getting if I use joins in sql query for single table it is working fine.I am not able to resolve this problem.
Code details:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/junit_generator_v1</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<mapping resource="com/jUnitFramework/classes/egs/data/MethodTestcaseDetails.hbm.xml" />
<mapping resource="com/jUnitFramework/classes/egs/data/TestSuitExeReport.hbm.xml" />
<mapping resource="com/jUnitFramework/classes/egs/data/MethodArgument.hbm.xml" />
<mapping resource="com/jUnitFramework/classes/egs/data/ArgumentData.hbm.xml" />
<mapping resource="com/jUnitFramework/classes/egs/data/TestSuitDetails.hbm.xml" />
<mapping resource="com/jUnitFramework/classes/egs/data/PackageDetails.hbm.xml" />
<mapping resource="com/jUnitFramework/classes/egs/data/TestReport.hbm.xml" />
<mapping resource="com/jUnitFramework/classes/egs/data/ClassDetails.hbm.xml" />
<mapping resource="com/jUnitFramework/classes/egs/data/MethodDetails.hbm.xml" />
<mapping resource="com/jUnitFramework/classes/egs/data/TestExecution.hbm.xml" />
</session-factory>
</hibernate-configuration>
Code:
/*
* Created on Oct 7, 2009
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.jUnitFramework.classes.egs.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* @author ramranjan.shukla
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class HibernateSession {
private static SessionFactory sessionFactory;
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession()
throws HibernateException {
Session s = (Session) session.get();
if (s == null) {
// Don't get from JNDI, use a static SessionFactory
if (sessionFactory == null) {
// Use default hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
}
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession()
throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null) s.close();
}
}
Code:
Here is my DAO Code:
/**
* This method will fetch all the existing test suite id and name to be displayed for update or viewing purposes
*
* @return List of JUnitTestSuiteDTO objects containing only test suite id and test suite name
*/
public static List fetchAllTestSuitesForTestReports(){
List testSuiteList = new ArrayList();
Session session = null;
try{
session = HibernateSession.currentSession();
session.beginTransaction();
session.flush();
session.clear();
JUnitTestSuiteDTO jTestSuiteDto = new JUnitTestSuiteDTO();
//session.setCacheMode(CacheMode.IGNORE);
session.setFlushMode(FlushMode.AUTO);
session.setCacheMode(CacheMode.REFRESH);
if((session instanceof SessionImpl)){
((SessionImpl)session).setAutoClear(true);
}
Query query = session.createSQLQuery(JUnitSQLQuery.VIEW_ALL_TESTSUITE).setCacheable(false);
query.setCacheMode(CacheMode.REFRESH);
query.setFlushMode(FlushMode.AUTO);
List evnt = query.list();
for (Iterator iterator = evnt.iterator(); iterator.hasNext();) {
Object[] object = (Object[]) iterator.next();
jTestSuiteDto = new JUnitTestSuiteDTO();
//jTestSuiteDto.setTestSuiteID(Long.valueOf( ((Integer)object[0]).toString() ));
jTestSuiteDto.setTestExecutionId(Long.valueOf( ((Integer)object[1]).toString() ));
jTestSuiteDto.setTestSuiteName((String)object[2]);
jTestSuiteDto.setTestSuitAndExeID((String)object[3]);
jTestSuiteDto.setCreatedOn((Date)object[4]);
testSuiteList.add(jTestSuiteDto);
}
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}finally{
//session.close();
//sessionFactory.close();
session.disconnect();
try {
session.connection().close();
} catch (HibernateException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
HibernateSession.closeSession();
}
return testSuiteList;
}
Query String:
public static String VIEW_ALL_TESTSUITE = " select tsd.test_suit_id, t.test_exe_id," +
" CONCAT(tsd.test_suit_name, ' --> ', t.exe_start_time ) as suite_name," +
" CONCAT(tsd.test_suit_id, ':', t.test_exe_id ) as suite_id, tsd.DATE_CREATED" +
" from" +
" test_suit_details tsd, test_suit_execution_status t" +
" where" +
" tsd.deleted_flag IS NULL and tsd.test_suit_id = t.test_suite_id";
Please help me out to resolve this problem.