在本節中,我們將說明如何執行 ClusterJ 應用程式的基本操作,包括下列項目
建立新執行個體、設定其屬性,並將其儲存至資料庫
執行主索引鍵查詢(讀取)
更新現有列並將變更儲存至資料庫
從資料庫刪除列
建構並執行查詢,以從資料庫擷取符合特定條件的一組列
建立新列。 若要將新列插入表格中,請先建立 Employee
的新執行個體。這可透過呼叫 Session
方法 newInstance()
來完成,如下所示
Employee newEmployee = session.newInstance(Employee.class);
設定與所需 employee
表格欄對應的 Employee
執行個體屬性。例如,下列程式碼會設定 id
、firstName
、lastName
和 started
屬性。
emp.setId(988);
newEmployee.setFirstName("John");
newEmployee.setLastName("Jones");
newEmployee.setStarted(new Date());
在您對變更感到滿意後,您可以持續保存 Employee
執行個體,導致將包含所需值的新列插入 employee
表格中,如下所示
session.persist(newEmployee);
如果已啟用自動認可,且資料庫中已存在與此 Employee
執行個體具有相同 id
的列,則 persist()
方法會失敗。如果已停用自動認可,且資料庫中已存在與此 Employee
執行個體具有相同 id
的列,則 persist()
方法會成功,但後續的 commit()
會失敗。
如果您希望即使列已存在,仍可儲存資料,請使用 savePersistent()
方法,而非 persist()
方法。savePersistent()
方法會視需要更新現有執行個體或建立新執行個體,而不會擲回例外狀況。
您尚未指定的值會與其 Java 預設值 (0
代表整數類型、0.0
代表數值類型,而 null
代表參考類型) 一起儲存。
主索引鍵查詢。 您可以使用 Session
的 find()
方法,在 NDB
表格中尋找現有列,如下所示
Employee theEmployee = session.find(Employee.class, 988);
這相當於主索引鍵查詢 SELECT * FROM employee WHERE id = 988
。
ClusterJ 也支援複合主索引鍵。find()
方法可以將物件陣列作為索引鍵,其中物件陣列的元件會依宣告順序用來表示主索引鍵欄。此外,查詢會經過最佳化,以偵測是否將主索引鍵的欄指定為查詢準則的一部分,如果是,則會執行主索引鍵查詢或掃描,做為實作查詢的策略。
ClusterJ 也支援多欄已排序的 btree 和唯一雜湊索引。與主索引鍵相同,如果查詢指定已排序或唯一索引欄位的值,ClusterJ 會將查詢最佳化,以使用索引掃描表格。
NDB Cluster 會自動將表格資料分散至多個資料節點。對於某些作業 (尋找、插入、刪除和更新),最好告知叢集資料實際所在的資料節點,並讓交易在該資料節點上執行。ClusterJ 會自動偵測分割索引鍵;如果可針對特定資料節點最佳化作業,ClusterJ 會自動在該節點上啟動交易。
更新並儲存列。 若要更新剛取得為 theEmployee
的列中指定欄的值,請使用名稱對應該欄名稱的 set*()
方法。例如,若要更新此 Employee
的 started
日期,請使用 Employee
的 setStarted()
方法,如下所示
theEmployee.setStarted(new Date(getMillisFor(2010, 01, 04)));
為了方便起見,在此範例中,我們使用方法 getMillisFor()
,該方法定義如下,位於 AbstractClusterJModelTest.java
檔案中 (在 NDB Cluster 原始碼樹狀結構的 storage/ndb/clusterj/clusterj-test/src/main/java/testsuite/clusterj
目錄中找到)
/** Convert year, month, day into milliseconds after the Epoch, UTC.
* Set hours, minutes, seconds, and milliseconds to zero.
* @param year the year
* @param month the month (0 for January)
* @param day the day of the month
* @return
*/
protected static long getMillisFor(int year, int month, int day) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, day);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long result = calendar.getTimeInMillis();
return result;
}
如需詳細資訊,請參閱所指示的檔案。
您可以呼叫其他 Employee
setter 方法,更新其他欄,如下所示
theEmployee.setDepartment(3);
若要將變更的列儲存回 NDB Cluster 資料庫,請使用 Session
的 updatePersistent()
方法,如下所示
session.updatePersistent(theEmployee);
刪除列。 您可以使用 Session
的 deletePersistent()
方法輕鬆刪除單列。在此範例中,我們會尋找 ID 為 13 的員工,然後從 employee
表格中刪除此列
Employee exEmployee = session.find(Employee.class, 13);
session.deletePersistent(exEmployee);'
System.out.println("Deleted employee named " + exEmployee.getFirst()
+ " " + exEmployee.getLast() + ".");
也有刪除多列的方法,其中提供兩個選項
從表格中刪除所有列。
刪除任意的列集合。
兩種多列刪除都可以使用 deletePersistentAll()
方法來執行。此方法的第一個變體會作用在 Class
上。例如,下列陳述式會從 employee
表格中刪除所有列,並傳回已刪除的列數,如下所示
int numberDeleted = session.deletePersistentAll(Employee);
System.out.println("There used to be " + numberDeleted + " employees, but now there are none.");
剛才顯示的 deletePersistentAll()
呼叫相當於在 mysql 用戶端中發出 SQL 陳述式 DELETE FROM employee
。
deletePersistentAll()
也可以用來刪除列集合,如此範例所示
// Assemble the collection of rows to be deleted...
List<Employee> redundancies = new ArrayList<Employee>();
for (int i = 1000; i < 2000; i += 100) {
Employee redundant = session.newInstance(Employee.class);
redundant.setId(i);
redundancies.add(redundant);
}
numberDeleted = session.deletePersistentAll(redundancies);
System.out.println("Deleted " + numberDeleted + " rows.");
不需要在刪除執行個體之前,先在資料庫中尋找它們。
撰寫查詢。 ClusterJ QueryBuilder
介面是用來具現化查詢。此程序從取得 QueryBuilder
的執行個體開始,該執行個體由目前的 Session
提供;然後,我們可以取得 QueryDefinition
,如下所示
QueryBuilder builder = session.getQueryBuilder();
QueryDomainType<Employee> domain = builder.createQueryDefinition(Employee.class);
然後,這會被用來設定查詢的比較欄位。在這裡,我們展示如何準備一個查詢,將 employee
資料表的 department
欄位的值與常數值 8
進行比較。
domain.where( domain.get("department").equal(domain.param("department") );
Query<Employee> query = session.createQuery(domain);
query.setParameter("department", 8);
要從查詢中獲取結果,請調用 Query
的 getResultList()
方法,如下所示:
List<Employee> results = query.getResultList();
傳回值是一個 List
,您可以迭代它來檢索和處理其中的列,就像平常一樣。
交易處理(Transactions)。 Transaction
介面可以選擇性地用於界定交易,透過以下方法:
begin()
:開始一個交易。commit()
:提交一個交易。rollback()
:回滾一個交易。
也可以使用 Transaction
來檢查交易是否處於活動狀態(透過 isActive()
方法),以及獲取和設定僅回滾標誌(分別使用 getRollbackOnly()
和 setRollbackOnly()
)。
如果您不使用 Transaction
介面,Session
中影響資料庫的方法,例如 persist()
、deletePersistent()
、updatePersistent()
等,會自動被包覆在資料庫交易中。