解决方案 »
- Struts2中,使用全局转换器时,始终不能调用convertToString方法,而使用局部转换器则正常,不知为何?
- myeclipse 8.5 blue,m1,m2,GA.哪个版本好!
- 求一个PL/SQL的存储过程
- struts+hibernate的一个问题,一直解决不了,请高手帮忙
- POI写EXCEL公式出现#REF如何解决
- 如何将List里的数据换位置?
- hibernate 不能开始事务!什么问题啊?
- 小弟初来乍到,先给各位散分请安了!
- 请教: Jbuilder 开发 EJB 的问题, 急!急!
- 用java开发一个ftp程序????????
- 关于邮件群发的问题【请您放下脚步进来指教一二,谢谢!】
- 新手囧问题啊
public Order findByNumber(String orderNumber) {
try {
String hql = "from Order where orderNumber = '" + orderNumber + "'";
Query query = getSession().createQuery(hql);
log.info(hql);
List list = query.list();
if (list.size() > 0)
return (Order) list.get(0);
return null;
} catch (RuntimeException re) {
log.error("find order by orderNumber failed", re);
throw re;
}catch(Exception e){
e.printStackTrace();
}catch(Error er){
er.printStackTrace();
}
return null;
}
Dao有没有继承org.springframework.orm.hibernate3.support.HibernateDaoSupport呢?让spring帮你管hibernate事务,否则你的session不及时手动close掉,就会造成这种情况。
Session session = getSession();
try {
String hql = "from Order where orderNumber = '" + orderNumber + "'";
Query query = session.createQuery(hql);
log.info(hql);
List list = query.list();
if (list.size() > 0)
return (Order) list.get(0);
return null;
} catch (RuntimeException re) {
log.error("find order by orderNumber failed", re);
throw re;
}catch(Exception e){
e.printStackTrace();
}catch(Error er){
er.printStackTrace();
}
finally{
releaseSession(session);
}
scope="prototype">
<property name="sessionFactory" ref="sf"></property>
</bean> <!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/logistic?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置SessionFactory -->
<bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="sessionFactory">
<ref bean="sf"/>
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allMethod" expression="execution(* dao.imp.*.*(..))"/>
<aop:advisor pointcut-ref="allMethod" advice-ref="txAdvice"/>
</aop:config>
我按照6楼说的改了,也还是不行。
public void testFindByNumber() {
for (int i = 0; i < 9; i++) {
System.out.println("====begin=====" + i);
Order order = orderDAO.findByNumber("DD1302182287233");
System.out.println(order);
System.out.println("====stop=====" + i);
}
}
控制台输出:====begin=====0
Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
com.logistic.entity.Order@10c3a08
====stop=====0
====begin=====1
Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
com.logistic.entity.Order@102a0a5
====stop=====1
====begin=====2
Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
com.logistic.entity.Order@1d8d237
====stop=====2
====begin=====3
Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
com.logistic.entity.Order@538974
====stop=====3
====begin=====4
Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
com.logistic.entity.Order@1be8711
====stop=====4
====begin=====5
Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
com.logistic.entity.Order@1c8fb4b
====stop=====5
====begin=====6
Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
com.logistic.entity.Order@127f79d
====stop=====6
====begin=====7
Hibernate: select order0_.ord_id as ord1_1_, order0_.ord_number as ord2_1_, order0_.ord_sennm as ord3_1_, order0_.ord_senaddr as ord4_1_, order0_.ord_senpost as ord5_1_, order0_.ord_sencont as ord6_1_, order0_.ord_recnm as ord7_1_, order0_.ord_recaddr as ord8_1_, order0_.ord_recpost as ord9_1_, order0_.ord_reccont as ord10_1_, order0_.ord_source as ord11_1_, order0_.ord_destination as ord12_1_, order0_.ord_paytype as ord13_1_, order0_.ord_money as ord14_1_, order0_.ord_operator as ord15_1_, order0_.ord_date as ord16_1_, order0_.ord_state as ord17_1_, order0_.ord_describe as ord18_1_ from logistic.t_order order0_ where order0_.ord_number='DD1302182287233'
com.logistic.entity.Order@5db5ae
====stop=====7
====begin=====8