您的位置:首页 > 运维架构

[ssh2]list判断空,HQL from类,@Entity,Write operations are not allowed in read-only mode

2015-01-05 09:08 501 查看
①判断一个拿到的list是否为空
如果判断其为nulll,则为  list==null
如果判断里面有没有元素,list.size()==0
如在验证数据库中是否存在已知邮箱:
@Override
public boolean ifExistsEmail(String eemail) {//重複返回真
List<User> users = (List<User>)hibernateTemplate.find("from User where email = ?",eemail)<span style="font-family: arial, 'courier new', courier, 宋体, monospace; white-space: pre-wrap;">//find(String queryString , Object value);</span>
if(users.size()==0){//用users==null你就完了,永远存在该邮箱
System.out.println("this is in userdaoimpl 不存在该邮箱!");
return false;}
else{
System.out.println("this is in userdaoimpl 存在该邮箱!");
for(int i=0;i<users.size();i++){
System.out.println(users.get(i).getEmail());
}
return true;
}
}</span>
②HQL语句from 后面跟的是类
下面片段是验证管理员是否存在
public boolean validate(Admin admin) {
List<Admin> admins = hibernateTemplate.find("from Admin where adminname = 'admin');//单引号是必要的
if(admins.size()>0) return true;
return false;
}</span>
我们在后台能看到这样的语句:QueryTranslatorImpl:216 - HQL: from com.gslabs.model.Admin where adminname = 'admin' 
③用实体与表映射的时候@Entity是用的“javax.persistence.Entity”包
Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.解决方法:在action 对应的函数sevice层配上@transactional后解决;错误原因:          OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flushmode 设为FlushMode.NEVER。然后把该sessionFactory绑定到TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该sessionFactory的绑定,最后closeSessionIfNecessary根据该session是否已和transaction绑定来决定是否关闭session。在这个过程中,若HibernateTemplate发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flushmodel的话,则doFilter的整个过程都是Flush.NEVER。所以受transaction(声明式的事务)保护的方法有写权限,没受保护的则没有。也就是说,在用OpenSessionInviewFilter来getSession,数据库用HibernateTemplate交互时,除了配置了@Transactional的事物有写权限,其他都没有。网络提供的解决方法,未实验:
解决方法:web.xml配置里添加<filter><filter-name>OpenSessionInViewFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class><init-param><param-name>sessionFactoryBeanName</param-name><param-value>sessionFactory</param-value></init-param><init-param><param-name>singleSession</param-name><param-value>true</param-value></init-param><init-param><param-name> flushMode </param-name><param-value>AUTO </param-value></init-param></filter>//   。。。。<filter-mapping><filter-name>OpenSessionInViewFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>如果在交给spring 管理的情况下,在beans.xml 里的配置<bean id="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><aop:config><aop:pointcut id="bussinessService"expression="execution(* com.fan.service.base.*.*(..))" /><aop:advisor pointcut-ref="bussinessService"advice-ref="txAdvice" /></aop:config><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="get*" read-only="false" propagation="NOT_SUPPORTED"/><tx:method name="find*" read-only="false" propagation="NOT_SUPPORTED"/><tx:method name="save*" propagation="REQUIRED"/> // 如果不把save update delete都配置上,<tx:method name="update*" propagation="REQUIRED"/> //这些操作会无效<tx:method name="delete*" propagation="REQUIRED"/></tx:attributes></tx:advice>
⑤我的action中有两个execute()方法:
<span style="white-space:pre">	</span>@Overridepublic String execute() throws Exception {System.out.println("this is in the execute ");fItemsManager.AddItems(fitems);return SUCCESS;}@Overridepublic String execute(FItems fitems)throws Exception{System.out.println("this is in the execute ");fItemsManager.AddItems(fitems);return SUCCESS;}
第一个是我用程序中右键,生成重写方法生成的;第二个是自个敲完添加的Override,Override会报错;程序不会执行自己写的execute,因为它带了参数,重写execute是不能带参数的,即使在action中指明了这样做对同一个Action类的不同方法的action
<action name="fItems" class="com.gslabs.action.FItemsAction" method="AddItems"><result name="success">/view/home.a8e8html</result><result name="error">/view/fail.jsp</result></action>
<pre name="code" class="java"><action name="fItems2" class="com.gslabs.action.FItemsAction" method="DelItems"><result name="success">/view/home.html</result><result name="error">/view/fail.jsp</result></action>
action名字变一下,method指定一下

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐