JIRA中,資料實體(entity)的字段、類型及和資料庫表的對應關系這些設定在 entityengine.xml、entitygroup.xml、entitymodel.xml中。
entitymodel.xml------------定義實體、屬性,以及和資料庫表、字段的對應關系。
entitygroup.xml------------定義資料實體所屬的分組資訊。
entityengine.xml-----------加載entity group, entity model, field type位置資訊、定義處理實體資料的delegator(委托);屬性、字段類型的對應資訊設定在fieldtype-***.xml檔案中。
使用 Ofbiz Entity Engine 來操作資料和資料庫,需要編輯以上配置檔案,操作略。本文讨論的是如何利用 active object 來處理資料。
1.實體entity的定義:
關系型資料庫中,實體就是由一系列屬性和該實體與其他實體間的關聯關系定義的一組資料。
2. AO簡介
ActiveObjects是一個純Java ORM。
AO可以與現有資料庫模式一起使用,也可以從使用者指定的實體接口自動生成資料庫模式。
ActiveObjects自然使用延遲加載以及複雜的緩存機制,比傳統的資料映射器ORM更具性能。 然而,它的主要設計目标不是性能,而是建立一個強大的ORM,用于內建到您的項目中。
JIRA産品是用Java語言寫的,在JIRA中操作資料和資料庫,也可以使用AO。
3.JIRA中使用AO的注意事項
3.1 在pom.xml中添加對activeobjects-plugin.jar的依賴。
3.2 AO通過接口(interface)來定義實體(entity),實體建立後,在資料庫中也會自動生成資料庫表(table)。
接口繼承了 Entity,它将主鍵直接定義為ID。
public interface DemoEntity extends Entity{
/**
定義 get,set方法以便存儲資料
*/
}
public interface Entity extends RawEntity<Integer>
{
@AutoIncrement
@NotNull
@PrimaryKey("ID")
public int getID(); //Entity接口已經為使用者定義好了ID主鍵
}
3.2.1 定義非空字段:
@NotNull
public String getAuthor();
public void setAuthor(String author);
3.3預設情況下,針對實體除ID以外的字段/屬性,AO使用懶加載(lazy loading);
使用 @Preload 注釋來讓AO立即加載。
3.4 為了能夠操作資料實體,需要在JIRA配置檔案(atlassian-plugin.xml)中引入AO元件(component import)。
(在SDK進階版本中,配置 component import,會報錯------atlassian-plugin.xml contains a definition of component-import.This is not allowed when Atlassian-Plugin-Key is set. 可以使用spring注解來解決,@ComponentImport)
3.5 每一個AO互動(interaction)的完成都需要在事務(transaction)中進行,可以在業務層接口上方加注解:
@Transaction
3.6 在定義實體(entity)名時,長度不要超過30個字元,否則會報如下錯誤:
net.java.ao.ActiveObjectsException: Invalid entity, generated table name (AO_769468_******) for 'com.******.****' is too long! It should be no longer than 30 chars.