Hi,
I have a strange problem with Hibernate when it generate the SQL. I am trying to use the Spring framework to set the Connection Factory, the session etc. But it doesn't seems to be a Spring problem, the only thing it could be would be a conflict between "commons" librairies. The Spring framework has correctly instantiated all the beans for Hibernate, but when Hibernate tries to make a very simple SQL (from a very simple HQL) it put 2 understores in front of the table name!
I have generated the job.hbm.xml using the Artifact Generator :
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<!-- JOB TABLE HERE --------------------------------- -->
<class
name="com.etl.persistence.model.Job"
table="job"
catalog=""
>
<id
name="Id"
type="java.lang.Integer"
>
<column name="id" length="10" not-null="true" sql-type="int unsigned" />
<generator class="assigned" />
</id>
<property
name="Date"
type="java.util.Date"
>
<column name="date" length="19" not-null="true" sql-type="datetime" />
</property>
<property
name="Type"
type="java.lang.String"
>
<column name="type" not-null="true" sql-type="varchar" />
</property>
<set
name="SetOfLoad"
>
<key>
<column name="jobId" length="10" not-null="false" unique="true" />
</key>
<one-to-many
class="com.etl.persistence.model.Load"
/>
</set>
</class>
</hibernate-mapping>
So, as you can see, my classe
com.etl.persistence.model.Job is linked to the table
job.
This is the code for the Dao, I am using the AOP Interceptor as described in chapter 11.2.4 of the Spring Reference Documentation. The only thing I changed from the exemple is passing a true parameter in getSession instead of false parameter (create session if session==null).
Code:
package com.etl.persistence.dao;
import java.util.List;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class JobDaoImpl extends HibernateDaoSupport implements IJobDao{
public List listAllJob(){
Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
List result = session.createQuery("from Job as job").list();
return result;
}
}
And here's my test:
Code:
package com.desjardins.vmd.etl.persistence.dao;
import java.util.List;
import junit.framework.TestCase;
import com.desjardins.vmd.util.SpringUtil;
public class JobDaoTest extends TestCase {
/**
* Constructor for ConfigurationReaderTest.
* @param arg0
*/
public JobDaoTest(String arg0) {
super(arg0);
}
public void testListAllJob() {
JobDaoImpl job = SpringUtil.getJobDao();
List l = job.listAllJob();
assertNotNull(l);
assertEquals(4, l.size());
}
}
The problem is linked to the SQL generation :
Code:
INFO: SQLErrorCodes loaded: [DB2, HSQL, MS-SQL, MySQL, Oracle, Informix, PostgreSQL, Sybase]
12-Apr-2005 7:36:55 PM org.springframework.beans.factory.support.AbstractBeanFactory getBean
INFO: Creating shared instance of singleton bean 'jobDaoTarget'
12-Apr-2005 7:36:55 PM org.springframework.beans.factory.support.AbstractBeanFactory getBean
INFO: Creating shared instance of singleton bean 'myJobDao'
12-Apr-2005 7:36:55 PM org.springframework.aop.framework.DefaultAopProxyFactory <clinit>
INFO: CGLIB2 available: proxyTargetClass feature enabled
12-Apr-2005 7:36:55 PM org.springframework.aop.framework.ProxyFactoryBean createAdvisorChain
INFO: Bean with name 'jobDaoTarget' concluding interceptor chain is not an advisor class: treating it as a target or TargetSource
12-Apr-2005 7:36:55 PM org.springframework.beans.factory.support.AbstractBeanFactory getBean
INFO: Creating shared instance of singleton bean 'jndiTemplate'
<!-- WRONG SQL HERE! --------------------------------- -->
Hibernate: select job0_.id as id, job0_.date as date1_, job0_.type as type1_ from __job job0_
<!-- WRONG SQL HERE! --------------------------------- -->
12-Apr-2005 7:36:56 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1146, SQLState: 42S02
12-Apr-2005 7:36:56 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Table 'etl.__job' doesn't exist
The table name should be job instead of __job!
Any idea? I will continu working on that... maybe Spring is interfering with Hibernate?
Thanks for any clue.
Etienne
Montreal