Struts Spring Hibernate 框架整合时候出现的问题
1.Tomcat和项目的lib目录下均要加入mysql数据库驱动mysql-connector-java-5.0.8-bin.jar。在bean中配置数据源的时候,请注意严格按照下面方式配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/drdatabase"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
上面的黑色部分我曾经出错了,导致Cannot Load JDBC 异常。
2. Struts action 配置与Spring配置时候出现的问题:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="doctors" extends="struts-default">
<action name ="savePatient" class="savePatientAction">
<result name="redirect">ListPatient.action</result>
<result name="input">/AddPatients.jsp</result>
</action>
</package>
</struts>
name的名称与显示层的JSP的Form表单提交名称一致,class的名称必须与在Spring中配置的bean的id号一致:
<bean id="savePatientAction" class="com.chenx.action.SavePatientAction">
<property name="patientService">
<ref bean="patientService"/>
</property>
</bean>
3.
form 标签 没有namespace属性
解决方法:将“一对多”关系中的“一”方,not-null设置为false
(参考资料:http://www.thearcmind.com/confluence/pages/viewpage.action?pageId=212)
异常2:org.hibernate.TransientObjectException: object references an unsaved transient instance
解决方法:cascade="save-update,persist"
(参考资料:http://www.laliluna.de/254.html)
异常3:org.hibernate.QueryException: could not resolve property
解决方法:"from Category category where category.userID = :userID"修改为"from Category category where userID = :userID"或者"from Category category where category.user.id = :userID"
(参考资料:http://www.laliluna.de/277.html)
异常4:could not initialize proxy - the owning Session was closed
解决方法:设置lazy为false
(参考资料:http://forum.springframework.org/showthread.php?t=27993)
<property name="userService">
<ref bean="userService"/>
</property>
</bean>