在以 OpenJPA 為建置基礎的 jpa-2.0 特性中,您可以產生資料定義語言 (DDL),或直接與資料庫互動,以便利用 SchemaMapper 工具,根據 JPA 實體定義來定義表格綱目。在以 EclipseLink 為建置基礎的 jpa-2.1 特性中,則可以使用 JPA 2.1 規格新增的「綱目產生器」新特性,其功能類似於 OpenJPA SchemaMapper。
關於這項作業
如果您需要 OpenJPA SchemaMapper 類似功能,可以配置 JPA 2.1 規格中的「綱目產生器」特性。
程序
- 在 persistence.xml 檔的持續性單元定義中,指定資料庫動作內容,可能的值是:none、create、drop、drop-and-create。 每一個值各對應至針對資料庫採取的動作。下列範例會除去持續性單元中指定之實體的對應表格,並在其位置中建立新表格。
<persistence-unit name="pu">
<properties>
<property name="javax.persistence.schema-generation.database.action"
value="drop-and-create" />
...
</properties>
</persistence-unit>
- 指定 Script 動作內容,可能的值是:none、create、drop、drop-and-create。 如果指定 none 以外的任何值,也必須指定目標內容。也就是說,如果 Script 動作是 create(產生實體定義的 create 陳述式),您必須指定一個包含目標檔案(其中要寫入陳述式)的對應建立目標內容。
<persistence-unit name="pu">
<properties>
<property name="javax.persistence.schema-generation.scripts.action"
value="drop-and-create" />
<property name="javax.persistence.schema-generation.scripts.create-target"
value="createTargetFile.ddl"/>
<property name="javax.persistence.schema-generation.scripts.drop-target"
value="sampleDrop.ddl"/>
...
</properties>
</persistence-unit>