<!-- Product has reference to ProductGroup, many products may reference same group --> <field name="group" type="myapp.ProductGroup"> <sql name="group_id" /> </field>
<!-- Product has reference to ProductDetail many details per product --> <field name="details" type="myapp.ProductDetail" required="true" collection="vector"> <sql many-key="prod_id"/> </field>
<!-- Product has reference to Category with many-many relationship --> <field name="categories" type="myapp.Category" required="true" collection="vector"> <sql name="category_id" many-table="category_prod" many-key="prod_id" /> </field> </class>
短事務 vs. 長事務 Castor 中正常的事務被稱為短事務。Castor 還提供了長事務這一概念,長事務由兩個短事務組成。我們可以使用一個典型的 Web 應用程序來理解兩種類型的事務之間的差異。對需要從數據庫讀取數據、將數據顯示給用戶然后把數據提交給數據庫的應用程序來說,我們查看有可能冗長的事務時間(這對于 Web 應用程序是很典型的)。但不能在數據庫中無限地保持寫鎖。為了解決這一問題,Castor 利用了兩個短事務。例如,第一個短事務使用只讀查詢來具體化對象,這讓您可以在不打開事務就顯示對象。假如用戶做了更改,則啟動第二短個事務,update() 方法將更改過的對象帶入事務的上下文。清單 5 是一個長事務的示例。