在action中调用services中的方法(services种方法调用dao中方法),比如在调用getObjectById(String id)方法的时候 如何在数据库没有此条记录的情况下返回null ?有什么好的办法处理这些异常?Dao层
public class DataDictionaryDaoImpl extends HibernateDaoSupport implements DataDictionaryDao{
public DataDictionary getDataDictionary(String id){
DataDictionary result;
try{
result= (DataDictionary)this.getHibernateTemplate().load(DataDictionary.class, id);
}catch(RuntimeException e ){
e.printStackTrace();
throw e;
}
return result;
}services层:
public DataDictionary getDataDictionary(String id){
DataDictionary result =null;
try{
result= dataDictionaryDao.getDataDictionary(id);
}catch(RuntimeException e){
e.printStackTrace();
throw e;
}
return result;
}
Action中 :
DataDictionary rusult=null;
try{
rusult = dataDictionaryService.getDataDictionary("dddd");
System.out.println(rusult.getValue() );
}
catch(RuntimeException e){
System.out.println("" );
}
if(rusult !=null)用这种方法处理导致的结果是 action中可以catch到,但 执行到if(rusult !=null) 立即出错:
这时如果数据库中没有数据 最好rusult 能变成null,我是菜鸟 也不知道这种异常处理方法行不行,大家有什么好的建议不?我在javaweb模块 也发了一个相似的帖子 大家也可以去回答?同样给分
public class DataDictionaryDaoImpl extends HibernateDaoSupport implements DataDictionaryDao{
public DataDictionary getDataDictionary(String id){
DataDictionary result;
try{
result= (DataDictionary)this.getHibernateTemplate().load(DataDictionary.class, id);
}catch(RuntimeException e ){
e.printStackTrace();
throw e;
}
return result;
}services层:
public DataDictionary getDataDictionary(String id){
DataDictionary result =null;
try{
result= dataDictionaryDao.getDataDictionary(id);
}catch(RuntimeException e){
e.printStackTrace();
throw e;
}
return result;
}
Action中 :
DataDictionary rusult=null;
try{
rusult = dataDictionaryService.getDataDictionary("dddd");
System.out.println(rusult.getValue() );
}
catch(RuntimeException e){
System.out.println("" );
}
if(rusult !=null)用这种方法处理导致的结果是 action中可以catch到,但 执行到if(rusult !=null) 立即出错:
这时如果数据库中没有数据 最好rusult 能变成null,我是菜鸟 也不知道这种异常处理方法行不行,大家有什么好的建议不?我在javaweb模块 也发了一个相似的帖子 大家也可以去回答?同样给分
如果执行到if( result != null ),证明下面这句也执行正确了。
rusult = dataDictionaryService.getDataDictionary("dddd");具体的错误,lz要给出来,其他人才可能帮你。
Actiontry{
DataDictionary rusult = dataDictionaryService.getDataDictionary("dddd");
if(rusult!=null){
//todosomething
}
}
catch(RuntimeException e){
//这里做异常的catch和处理servers和dao有异常会在这里来catch。
}
serverspublic DataDictionary getDataDictionary(String id) throws Exception{
DataDictionary result = null;
try {
result = dataDictionaryDao.getDataDictionary(id);
return result;
} catch (RuntimeException e) {
throw e;
}
}
daopublic DataDictionary getDataDictionary(String id)throws Exception {
DataDictionary result;
try {
result = (DataDictionary) this.getHibernateTemplate().load(DataDictionary.class, id);
return result;
} catch (RuntimeException e) {
throw e;
}
}第二种方式:
Action:
DataDictionary rusult = dataDictionaryService.getDataDictionary("dddd");
if (rusult != null) {
// todosomething
}
servers:public DataDictionary getDataDictionary(String id) {
DataDictionary result = null;
try {
result = dataDictionaryDao.getDataDictionary(id);
return result;
} catch (RuntimeException e) {
return null;
}
}dao:
public DataDictionary getDataDictionary(String id) {
DataDictionary result;
try {
result = (DataDictionary) this.getHibernateTemplate().load(
DataDictionary.class, id);
return result;
} catch (RuntimeException e) {
return null;
}
}
这句话是什么意思?如果catch到异常的话是不会走到if(rusult !=null)的。
debug时候 执行完result = dataDictionaryDao.getDataDictionary(id);后
result值为:com.sun.jdi.InvocationException occurred invoking method.
出错 直接到了直接到了action中。
报错是:
18:22:12,850 ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:172)
at cn.gov.pbc.gwjd.common.pojo.DataDictionary$$EnhancerByCGLIB$$1a309c4f.getValue(<generated>)
at cn.gov.pbc.gwjd.actions.TestServices.main(TestServices.java:57)
try{
DataDictionary rusult = dataDictionaryService.getDataDictionary("dddd");
if(rusult!=null){
//todosomething
}
}
catch(RuntimeException e){
//这里做异常的catch和处理servers和dao有异常会在这里来catch。
}执行完后 直接到catch里面了 没有到 if(rusult!=null)
顺便说明一下 :ssh框架采用了事务控制,用spring来管理hibernate的session,从而导致了很多问题,配置文件如下
web.xml
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>applicationcontext.xml<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean><!-- 配置事务的传播特性 其他开头的方法* true 只读 脏数据不判断 速度快点-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice><!-- 那些类的哪些方法参与事务 -->
<aop:config>
<aop:pointcut id="allServicesMethod" expression="(execution(* cn.gov.pbc.gwjd.common.services.*.*(..)))
or (execution(* cn.gov.pbc.gwjd.supervise.services.*.*(..)))
or (execution(* cn.gov.pbc.gwjd.organization.services.*.*(..)))
"/>
<aop:advisor pointcut-ref="allServicesMethod" advice-ref="txAdvice"/>
</aop:config>
在配置文件里面制定你的exception
<tx:method name="update*" propagation="REQUIRED" rollback-for ="Exception"/>
很悲剧啊 ,学校里面学了ssh现在都不知道怎么用了 ,。
工作没用ssh。我采用第一种方法 并将 hibernate配置文件中的lazy="false" 加上 没错误了 但是
try{
DataDictionary rusult = dataDictionaryService.getDataDictionary("dddd");
if(rusult!=null){
//todosomething
}
}
catch(RuntimeException e){
//这里做异常的catch和处理servers和dao有异常会在这里来catch。
}执行完后 直接到catch里面了 没有到 if(rusult!=null)
肯定是dao那里抛出了异常~,所以Action这里直接到catch块了。Action try下面修改下DataDictionary rusult = new DataDictionary();
result= dataDictionaryService.getDataDictionary("dddd");
if.....