Class DefaultTableGenerator
java.lang.Object
org.eclipse.persistence.tools.schemaframework.DefaultTableGenerator
DefaultTableGenerator is a utility class used to generate a default table schema for a EclipseLink project object.
The utility can be used in EclipseLink CMP for OC4J to perform the table auto creation process, which can be triggered
at deployment time when EclipseLink project descriptor is absent (default mapping) or present.
The utility can also be used to any EclipseLink application to perform the table drop/creation at runtime.
The utility handles all direct/relational mappings, inheritance, multiple tables, interface with/without tables,
optimistic version/timestamp lockings, nested relationships, BLOB/CLOB generation.
The utility is platform-agnostic.
Usage:
- CMP
1. set "autocreate-tables=true|false, autodelete-tables=true|false" in oc4j application deployment
descriptor files (config/system-application.xml, config/application.xml, or orion-application.xml in an .ear)
2. Default Mapping: the same as CMP, plus system properties setting -Declipselink.defaultmapping.autocreate-tables='true|false'
and -Declipselink.defaultmapping.autodelete-tables='true|false'
- Non-CMP:
1. Configuration: through sessions.xml
2. Directly runtime call through schema framework:
SchemaManager mgr = new SchemaManager(session);
mgr.replaceDefaultTables(); //drop and create
mgr.createDefaultTables(); //create only
The utility currently only supports relational project.
- Author:
- King Wang
-
Field Summary
Modifier and TypeFieldDescriptionprotected Map<org.eclipse.persistence.internal.helper.DatabaseField,
org.eclipse.persistence.internal.helper.DatabaseField> DatabaseField pool (synchronized with above 'fieldMap')protected org.eclipse.persistence.internal.databaseaccess.DatabasePlatform
the target database platform.protected Map<org.eclipse.persistence.internal.helper.DatabaseField,
FieldDefinition> Used to track the field definition: keyed by the database field object, and valued by the field definition.protected boolean
When this flag is 'false' EclipseLink will not attempt to create fk constraints.protected Map<String,
TableDefinition> Used to track the table definition: keyed by the table name, and valued by the table definition object. -
Constructor Summary
ConstructorDescriptionDefaultTableGenerator
(Project project) Default constructorDefaultTableGenerator
(Project project, boolean generateFKConstraints) This constructor will create a DefaultTableGenerator that can be set to create fk constraints -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addFieldsForMappedKeyMapContainerPolicy
(org.eclipse.persistence.internal.queries.ContainerPolicy cp, TableDefinition table) The ContainerPolicy may contain some additional fields that should be added to the tableprotected void
addForeignKeyConstraint
(TableDefinition sourceTableDef, TableDefinition targetTableDef, List<String> fkFields, List<String> targetFields, boolean cascadeOnDelete) Add a foreign key constraint to the source table.protected void
protected void
protected void
addForeignMappingFkConstraint
(Map<org.eclipse.persistence.internal.helper.DatabaseField, org.eclipse.persistence.internal.helper.DatabaseField> srcFields, boolean cascadeOnDelete) protected void
addJoinColumnsFkConstraint
(List<org.eclipse.persistence.internal.helper.DatabaseField> fkFields, List<org.eclipse.persistence.internal.helper.DatabaseField> targetFields, boolean cascadeOnDelete) protected void
addUniqueKeyConstraints
(TableDefinition sourceTableDef, Map<String, List<List<String>>> uniqueConstraintsMap) protected void
buildDirectCollectionTableDefinition
(DirectCollectionMapping mapping, ClassDescriptor descriptor) Build direct collection table definitions in a EclipseLink descriptorprotected void
buildRelationTableDefinition
(ForeignReferenceMapping mapping, RelationTableMechanism relationTableMechanism, org.eclipse.persistence.internal.helper.DatabaseField listOrderField, org.eclipse.persistence.internal.queries.ContainerPolicy cp) Build relation table definitions for all many-to-many relationships in a EclipseLink descriptor.protected void
buildRelationTableFields
(ForeignReferenceMapping mapping, TableDefinition table, List<org.eclipse.persistence.internal.helper.DatabaseField> fkFields, List<org.eclipse.persistence.internal.helper.DatabaseField> targetFields) Build field definitions and foreign key constraints for all many-to-many relation table.protected void
Add the foreign key to the aggregate collection mapping target table.Generate a default TableCreator object from the EclipseLink project object.generateFilteredDefaultTableCreator
(org.eclipse.persistence.internal.sessions.AbstractSession session) Generate a default TableCreator object from the EclipseLink project object, and perform the table existence check through jdbc table metadata, and filter out tables which are already in the database.protected FieldDefinition
getFieldDefFromDBField
(org.eclipse.persistence.internal.helper.DatabaseField dbField) Build a field definition object from a database field.protected TableDefinition
getTableDefFromDBTable
(org.eclipse.persistence.internal.helper.DatabaseTable databaseTable) Build a table definition object from a database table objectprotected void
initTableSchema
(ClassDescriptor descriptor) Build tables/fields information into the table creator object from a EclipseLink descriptor.protected void
postInitTableSchema
(ClassDescriptor descriptor) Build additional table/field definitions for the descriptor, like relation table and direct-collection, direct-map table, as well as reset LOB type for serialized object mapping and type conversion mapping for LOB usageprotected void
processAdditionalTablePkFields
(ClassDescriptor descriptor) protected void
resetFieldTypeForLOB
(DirectToFieldMapping mapping) Reset field type to use BLOB/CLOB with type conversion mapping fix for 4k oracle thin driver bug.protected void
Reset the transformation mapping field typesprotected org.eclipse.persistence.internal.helper.DatabaseField
resolveDatabaseField
(org.eclipse.persistence.internal.helper.DatabaseField childField, org.eclipse.persistence.internal.helper.DatabaseField parentField) Resolve the foreign key database field metadata in relation table or direct collection/map table.protected void
setFieldToRelationTable
(org.eclipse.persistence.internal.helper.DatabaseField dbField, TableDefinition table) Build and add a field definition object to relation table
-
Field Details
-
databasePlatform
protected org.eclipse.persistence.internal.databaseaccess.DatabasePlatform databasePlatformthe target database platform. -
tableMap
Used to track the table definition: keyed by the table name, and valued by the table definition object. -
fieldMap
Used to track the field definition: keyed by the database field object, and valued by the field definition. -
databaseFields
protected Map<org.eclipse.persistence.internal.helper.DatabaseField,org.eclipse.persistence.internal.helper.DatabaseField> databaseFieldsDatabaseField pool (synchronized with above 'fieldMap') -
generateFKConstraints
protected boolean generateFKConstraintsWhen this flag is 'false' EclipseLink will not attempt to create fk constraints.
-
-
Constructor Details
-
DefaultTableGenerator
Default constructor -
DefaultTableGenerator
This constructor will create a DefaultTableGenerator that can be set to create fk constraints
-
-
Method Details
-
generateDefaultTableCreator
Generate a default TableCreator object from the EclipseLink project object. -
generateFilteredDefaultTableCreator
public TableCreator generateFilteredDefaultTableCreator(org.eclipse.persistence.internal.sessions.AbstractSession session) throws DatabaseException Generate a default TableCreator object from the EclipseLink project object, and perform the table existence check through jdbc table metadata, and filter out tables which are already in the database.- Throws:
DatabaseException
-
initTableSchema
Build tables/fields information into the table creator object from a EclipseLink descriptor. This should handle most of the direct/relational mappings except many-to-many and direct collection/map mappings, which must be down in postInit method. -
postInitTableSchema
Build additional table/field definitions for the descriptor, like relation table and direct-collection, direct-map table, as well as reset LOB type for serialized object mapping and type conversion mapping for LOB usage -
addFieldsForMappedKeyMapContainerPolicy
protected void addFieldsForMappedKeyMapContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy cp, TableDefinition table) The ContainerPolicy may contain some additional fields that should be added to the table- See Also:
-
MappedKeyMapContainerPolicy
-
buildRelationTableDefinition
protected void buildRelationTableDefinition(ForeignReferenceMapping mapping, RelationTableMechanism relationTableMechanism, org.eclipse.persistence.internal.helper.DatabaseField listOrderField, org.eclipse.persistence.internal.queries.ContainerPolicy cp) Build relation table definitions for all many-to-many relationships in a EclipseLink descriptor. -
buildRelationTableFields
protected void buildRelationTableFields(ForeignReferenceMapping mapping, TableDefinition table, List<org.eclipse.persistence.internal.helper.DatabaseField> fkFields, List<org.eclipse.persistence.internal.helper.DatabaseField> targetFields) Build field definitions and foreign key constraints for all many-to-many relation table. -
buildDirectCollectionTableDefinition
protected void buildDirectCollectionTableDefinition(DirectCollectionMapping mapping, ClassDescriptor descriptor) Build direct collection table definitions in a EclipseLink descriptor -
resetFieldTypeForLOB
Reset field type to use BLOB/CLOB with type conversion mapping fix for 4k oracle thin driver bug. -
resetTransformedFieldType
Reset the transformation mapping field types -
createAggregateTargetTable
Add the foreign key to the aggregate collection mapping target table. Also add listOrderField if specified. -
addForeignKeyFieldToSourceTargetTable
-
addForeignKeyFieldToSourceTargetTable
-
addForeignMappingFkConstraint
protected void addForeignMappingFkConstraint(Map<org.eclipse.persistence.internal.helper.DatabaseField, org.eclipse.persistence.internal.helper.DatabaseField> srcFields, boolean cascadeOnDelete) -
getTableDefFromDBTable
protected TableDefinition getTableDefFromDBTable(org.eclipse.persistence.internal.helper.DatabaseTable databaseTable) Build a table definition object from a database table object -
resolveDatabaseField
protected org.eclipse.persistence.internal.helper.DatabaseField resolveDatabaseField(org.eclipse.persistence.internal.helper.DatabaseField childField, org.eclipse.persistence.internal.helper.DatabaseField parentField) Resolve the foreign key database field metadata in relation table or direct collection/map table. Those metadata includes type, and maybe dbtype/size/subsize if DatabaseField carries those info. -
getFieldDefFromDBField
protected FieldDefinition getFieldDefFromDBField(org.eclipse.persistence.internal.helper.DatabaseField dbField) Build a field definition object from a database field. -
setFieldToRelationTable
protected void setFieldToRelationTable(org.eclipse.persistence.internal.helper.DatabaseField dbField, TableDefinition table) Build and add a field definition object to relation table -
processAdditionalTablePkFields
-
addJoinColumnsFkConstraint
-
addForeignKeyConstraint
protected void addForeignKeyConstraint(TableDefinition sourceTableDef, TableDefinition targetTableDef, List<String> fkFields, List<String> targetFields, boolean cascadeOnDelete) Add a foreign key constraint to the source table. -
addUniqueKeyConstraints
-