急!!!Spring+hibernate连接ORACLE,有查询SQL,但是没有结果?
在TOMCAT5.5下,用STRUCT+SPRING+HIBERNATE开发,连接数据库发现问题,配置文件如下:
applicationContext.xml
<beans>
<bean id="ttDataSource" class="org.apache.commons.dbcp.BasicDataSource">
。。。。。
</bean>
<bean id="ttSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="ttDataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.max_fetch_depth">2</prop>
<prop key="hibernate.default_batch_fetch_size">8</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>Username.hbm.xml</value>
</list>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="ttTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="ttSessionFactory" />
</property>
</bean>
<bean id="UsernameDAO" class="UsernameDAO">
<property name="sessionFactory">
<ref bean="ttSessionFactory" />
</property>
</bean>
<bean id="userrightTarget" class="UserrightService">
<property name="usernameDAO">
<ref local="UsernameDAO"></ref>
</property>
</bean>
<bean id="userrightService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="ttTransactionManager"></ref>
</property>
<property name="target">
<ref local="userrightTarget"></ref>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
Username.hbm.xml:
....
<class name="Username" table="USERNAME" schema="tt" >
<id name="username" type="string">
<column name="USERNAME" length="8" />
<generator class="assigned"></generator>
</id>
<property name="longname" type="string">
<column name="LONGNAME" length="60" />
</property>
....
UsernameDAO.java:
....
public Username findById( java.lang.String id) {
log.debug("getting Username instance with id: " + id);
Username instance=new Username();
try {
HibernateTemplate template=this.getHibernateTemplate();
// HibernateTemplate template=this.getHibernateTemplate();
// Connection con=template.getSessionFactory().getCurrentSession().connection();
// PreparedStatement pst=con.prepareStatement("select * from username where username='123'");
// ResultSet rst=pst.executeQuery();
// while(rst.next()){
// System.out.println(rst.getString("username"));
// //System.out.println(rst.getString("functionname"));
// }
List l =(List)template.find("from Username user where user.username=?",id);
System.out.println(l.size());
instance.getDepartment();
System.out.println(instance.getUserid());
} catch (Exception re) {
log.error("get failed", re);
//throw re;
}
return instance;
}
....
跟踪测试:如果取消注释部分,可以得到查询结果,但如果注释了,执行template.find("from Username user where user.username=?",id);可以看到控制台显示了HQL-》SQL语句,但是没有查询结果,将该SQL直接到数据库查询,可以有结果。请问是什么原因?急!!!!