1. 程式人生 > >Java專案開發中一些BUG的總結

Java專案開發中一些BUG的總結

1,JVM Bind


2, hbm主鍵生成策略錯誤:

Struts Problem Report

Struts has detected an unhandled exception:

Messages:

  1. ids for this class must be manually assigned before calling save(): cn.itcast.bos.domain.bc.Standard
  2. ids for this class must be manually assigned before calling save(): cn.itcast.bos.domain.bc.Standard; nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): cn.itcast.bos.domain.bc.Standard

File:

org/hibernate/id/Assigned.java

Line number:

53

Stacktraces

org.springframework.orm.hibernate3.HibernateSystemException:ids for this class must be manually assigned before calling save():cn.itcast.bos.domain.bc.Standard; nested exception isorg.hibernate.id.IdentifierGenerationException: ids for this class must bemanually assigned before calling save(): cn.itcast.bos.domain.bc.Standard

解決:

反向生成的PO和hbm,在hbm中的主鍵生成策略是assigned。如果主鍵是String型別的,則需要將這個修改成uuid,就可以修正錯誤。

3,延遲加載出錯,報沒有session

Struts Problem Report

Struts has detected an unhandled exception:

Messages:

  1. could not initialize proxy - no Session
  2. java.lang.reflect.InvocationTargetException
  3. org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
  4. org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException

File:

org/hibernate/proxy/AbstractLazyInitializer.java

Line number:

167

Stacktraces

org.apache.struts2.json.JSONException:

 org.apache.struts2.json.JSONException:

 org.apache.struts2.json.JSONException:java.lang.reflect.InvocationTargetException

注意表格中的紅色加粗字,初次看到這個資訊的時候,可以猜到是懶載入造成的session關閉,

解決方法:通過在web.xml中配置過濾器解決,

<!-- 解決延遲載入異常 -->

<filter>

   <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

 </filter>

 <filter-mapping>

   <filter-name>OpenSessionInViewFilter</filter-name>

   <url-pattern>/*</url-pattern>

  </filter-mapping>

4,由資料表外來鍵引發的異常:

Struts Problem Report

Struts has detected an unhandled exception:

Messages:

  1. Cannot add or update a child row: a foreign key constraint fails (`mvbos`.`bc_subarea`, CONSTRAINT `FK_area_region` FOREIGN KEY (`region_id`) REFERENCES `bc_region` (`id`))
  2. Could not execute JDBC batch update
  3. Could not execute JDBC batch update; SQL [update mvbos.bc_subarea set decidedzone_id=?, region_id=?, addresskey=?, startnum=?, endnum=?, single=?, position=? where id=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

File:

com/mysql/jdbc/PreparedStatement.java

Line number:

1,666

Cannot add or update a child row: a foreign keyconstraint fails (`mvbos`.`bc_subarea`, CONSTRAINT `FK_area_region` FOREIGN KEY(`region_id`) REFERENCES `bc_region` (`id`))

這句話的意思是:bc_subarea表的外來鍵指向了bc_region的id屬性。也就是說,subarea的存在依賴於region,沒有region就沒有subarea,所以必須要先存在region。才可以。

得出出現錯誤的原因:

在儲存subarea的時候,subarea中指向region的外來鍵的值,在region表中不存在。

解決方式:

檢視是否是subarea的外來鍵輸入錯誤,或者建立id等於subarea的外來鍵的region資料記錄。

5,struts.xml中不配result


Noresult defined for action cn.itcast.bos.web.action.subarea.SubareaAction andresult upload

出現原因:在Action中定義了返回結果upload,但是在struts.xml中沒有配置接收upload的result。

解決方案:在struts.xml中配置result。

6,不能例項化action

Struts Problem Report

Struts has detected an unhandled exception:

Messages:

  1. regionAction
  2. Unable to instantiate Action, regionAction, defined for 'regionAction_pageQuery' in namespace '/'regionAction

File:

org/apache/catalina/loader/WebappClassLoader.java

Line number:

1,645

Stacktraces

Unable to instantiate Action,regionAction, defined for 'regionAction_pageQuery' in namespace '/'regionAction

原因:spring管理action時,需要在applicationContext.xml中配置action的實現類,出現這個問題是因為在spring的核心配置檔案中,這個action的實現類的id與struts.xml中的class值不一致了。

解決:檢視struts.xml的這個action的class屬性和applicationContext.xml的這個action的id屬性是否一致,如果不一致,修改其一,使其一致即可。

7,主鍵生成策略設定不當產生的異常

Struts Problem Report

Struts has detected an unhandled exception:

Messages:

  1. Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
  2. Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

File:

org/hibernate/jdbc/Expectations.java

Line number:

85

Stacktraces

org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException:Batch update returned unexpected row count from update [0]; actual row count:0; expected: 1; nested exception is org.hibernate.StaleStateException: Batchupdate returned unexpected row count from update [0]; actual row count: 0;expected: 1

解決:檢視hbm檔案中的主鍵生成策略。

8,tomcat啟動時報session錯誤

嚴重: IOExceptionwhile loading persisted sessions: java.io.EOFException

嚴重: Exception loading sessions from persistent storage

解決:

說法一:根本原因是給tomcat設定的快取太小,解決的辦法就是在eclipse的tomcat裡配置一下所使用的記憶體大小

說法二:分析:EOFException表示輸入過程中意外地到達檔案尾或流尾的訊號,導致從session中獲取資料失敗。異常是tomcat本身的問題,由於tomcat上次非正常關閉時有一些活動session被持久化(表現為一些臨時檔案),在重啟時,tomcat嘗試去恢復這些session的持久化資料但又讀取失敗造成的。此異常不影響系統的使用。

解決辦法:tomcat6.0\work\Catalina\localhost\peam\SESSIONS.ser刪除。如果正常關閉服務端,該檔案是自動刪除的。我參照上面方法解決的問題,用的是MyEclipse6.0,tomcat6.0。通常情況下,會認為是tomcat的快取,會直接把整個localhost資料夾刪除。但是上面的方法也是可取的,在localhost資料夾下,找到部署的工程名,在該工程名下有SESSIONS.ser檔案,直接刪除。重啟tomcat,問題解決。

9,WebService釋出報錯

Exception in thread "main" com.sun.xml.internal.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class cn.itcast.ws.jaxws.SayHello is notfound. Have you run APT to generate them?

原因:很可能是JDK的版本不夠造成的,建議JDK1.6u17之後的版本,最好改成1.7。

解決:升級JDK到1.7即可。