①判断一个拿到的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 后面跟的是类
下面片段是验证管理员是否存在
我们在后台能看到这样的语句:QueryTranslatorImpl:216 - HQL: from com.gslabs.model.Admin where adminname = 'admin'public boolean validate(Admin admin) { List<Admin> admins = hibernateTemplate.find("from Admin where adminname = 'admin');//单引号是必要的 if(admins.size()>0) return true; return false; }</span>
③用实体与表映射的时候@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,数据库用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>@Override
public String execute() throws Exception {
System.out.println("this is in the execute ");
fItemsManager.AddItems(fitems);
return SUCCESS;
}
@Override
public 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.html</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指定一下