VO插入數據時的初始化

<!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Cambria; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:-1610611985 1073741899 0 0 159 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:"/@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋體; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} h2 {mso-style-priority:9; mso-style-qformat:yes; mso-style-link:"標題 2 Char"; mso-style-next:正文; margin-top:13.0pt; margin-right:0cm; margin-bottom:13.0pt; margin-left:0cm; text-align:justify; text-justify:inter-ideograph; line-height:173%; mso-pagination:lines-together; page-break-after:avoid; mso-outline-level:2; font-size:16.0pt; font-family:"Cambria","serif"; mso-ascii-font-family:Cambria; mso-ascii-theme-font:major-latin; mso-fareast-font-family:宋體; mso-fareast-theme-font:major-fareast; mso-hansi-font-family:Cambria; mso-hansi-theme-font:major-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:major-bidi; mso-font-kerning:1.0pt;} h3 {mso-style-priority:9; mso-style-qformat:yes; mso-style-link:"標題 3 Char"; mso-style-next:正文; margin-top:13.0pt; margin-right:0cm; margin-bottom:13.0pt; margin-left:0cm; text-align:justify; text-justify:inter-ideograph; line-height:173%; mso-pagination:lines-together; page-break-after:avoid; mso-outline-level:3; font-size:16.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-font-kerning:1.0pt;} pre {mso-style-noshow:yes; mso-style-priority:99; mso-style-link:"HTML 預設格式 Char"; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Courier New"; mso-fareast-font-family:宋體; color:black;} p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph {mso-style-priority:34; mso-style-unhide:no; mso-style-qformat:yes; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; text-indent:21.0pt; mso-char-indent-count:2.0; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋體; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} span.2Char {mso-style-name:"標題 2 Char"; mso-style-priority:9; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"標題 2"; mso-ansi-font-size:16.0pt; mso-bidi-font-size:16.0pt; font-family:"Cambria","serif"; mso-ascii-font-family:Cambria; mso-ascii-theme-font:major-latin; mso-fareast-font-family:宋體; mso-fareast-theme-font:major-fareast; mso-hansi-font-family:Cambria; mso-hansi-theme-font:major-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:major-bidi; font-weight:bold;} span.3Char {mso-style-name:"標題 3 Char"; mso-style-priority:9; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"標題 3"; mso-ansi-font-size:16.0pt; mso-bidi-font-size:16.0pt; font-weight:bold;} span.code1 {mso-style-name:code1; mso-style-unhide:no; mso-ansi-font-size:11.0pt; mso-bidi-font-size:11.0pt; font-family:"Courier New"; mso-ascii-font-family:"Courier New"; mso-hansi-font-family:"Courier New"; mso-bidi-font-family:"Courier New"; color:#336699;} span.HTMLChar {mso-style-name:"HTML 預設格式 Char"; mso-style-noshow:yes; mso-style-priority:99; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"HTML 預設格式"; mso-ansi-font-size:11.0pt; font-family:"Courier New"; mso-ascii-font-family:"Courier New"; mso-fareast-font-family:宋體; mso-hansi-font-family:"Courier New"; mso-bidi-font-family:"Courier New"; color:black; mso-font-kerning:0pt;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:933973771; mso-list-type:hybrid; mso-list-template-ids:257097268 1825324852 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-tab-stop:none; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt;} @list l1 {mso-list-id:1969579472; mso-list-type:hybrid; mso-list-template-ids:34403246 695134388 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l1:level1 {mso-level-tab-stop:none; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

一: 插入時不進行查詢

1.       當你插入的 VO 沒有從數據庫中查詢數據的時候,你必須使用 setMaxFetchSize(0) 初始化他。當 setMaxFetchSize 置爲 0 的時候則 VO 會創建一個空的 Rowset 但是查詢不會執行。(注:每次進行這個初始化之前請確認 VO 不存在任何行)

if (vo.getFetchedRowCount() == 0)

{vo.setMaxFetchSize(0);}

Row row = vo.createRow();

vo.insertRow(row);

row.setNewRowState(Row.STATUS_INITIALIZED);

2.       如果你有一個 VO 包含,可變的數據。並且你可能會提交保存有這個 VO 數據的事務。(應用程序屬性 VO 是一個很好的例子 PVO 實現 PPR VO 保存程序狀態的)你需要添加以下代碼來保證數據不會被不注意的清除。

if (vo.getFetchedRowCount() == 0) 
{ 
  
vo.setMaxFetchSize(0); 
  
vo.executeQuery();


}

3.       作爲一個規定不要使用 WHERE 1=0 來強迫執行一個沒有意義的查詢。但是在 Master-Details 的關係中是個例外,需要使用 WHERE 1=0 並且執行 executeQuery() 來達到 DetailsVO 插入的初始化目的。而是用 setMaxFetchSize 0 則會造成不利的影響。

二:插入時 VO 執行查詢

1.       對於那些即用來插入,又用來查詢的 VO 必須在插入前執行 executeQuery() 。如果在查詢前就執行了 insertRow(row) ,插入的行可能不會被正確的保存,所以可能在激活後丟失

三:避免無條件的初始化

processRequest() 方法被重新進入,無限制的初始化會重置導致事務狀態的丟失。用戶對臨時 VO 屬性的修改會丟失。以下代碼可以可以避免這種情況

if (!vo.isPreparedForExecution())
{
  
vo.executeQuery();
}

  注:有些情況並不能依靠 isPreparedForExecution() 去檢查,比如當你在使用了 Transacton.rollback() or ViewObject.clearCache() 後,馬上插入數據如果不顯式或者隱式的查詢 VO 你就會丟失這些行。 Transacton.rollback() or ViewObject.clearCache() 會重新 Vo 的查詢狀態。所以爲了防止不必要的數據丟失使用 OAViewObject.isExecuted() 代替

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章