[ Team LiB ] |
5.5 Mapping a Single-Valued Field to a ColumnA primitive or single-valued Java field usually is mapped to a single column of a table. Some implementations allow a field to be mapped to multiple columns, but such a feature is not supported by most implementations or needed in most applications. When mapping a Java field to a relational column, you need to consider the name and the type to be used for the associated column. The types are always different, since Java and SQL have their own distinct type systems. The name of the field and column can be either the same or different. 5.5.1 Name-MappingWhen you're mapping a field in Java to a relational column, you can use different names. In some cases, you may have to use a different name, because some names in Java may not be allowable as a column name in the relational database. In Java, class and field names are case-sensitive Unicode characters. Some relational databases and JDBC drivers may have restrictions on the names that are used (e.g., the table and column names must be US ASCII, names are case-insensitive, or names must be uppercase). Using a field or class name that is a keyword in SQL or the relational database also necessitates a mapping to a different name in the datastore. You may wish to map the firstName field of the Customer class to a column named fname: <class name="Customer" > <field name="firstName" > <extension vendor-name="vendorX" key="column" value="fname"/> <extension vendor-name="vendorY" key="sqlname" value="fname"/> </field> </class> If the firstName field does not already have a field element, you need to add one to specify the column name in a nested extension element. In this case, to specify the column to map the field to, vendorX uses a value of "column" and vendorY uses a value of "sqlname" for the key attribute. Again, the value for the key attribute is implementation-specific and you can provide extension elements for multiple implementations without any interference. 5.5.2 Type-MappingBesides specifying the name of the column, you may also want to indicate the column's datatype. The datatypes that can be used for a specific Java type vary across relational datastores and JDO implementations. The supported column types for each Java datatype in each underlying datastore should be specified in your JDO implementation's documentation. Table 5-2 provides a list of the relational column datatypes commonly supported for the Java types supported by JDO.
ANSI SQL defines some of these column types. Others are supported by specific relational databases and found in applications' schemas. Some implementations allow you to specify the maximum size of a String stored in the datastore.
5.5.3 IndexesJDO does not define the concept of an index. Indexes can be added to columns independent of the JDO environment. However, some implementations may allow you to specify indexes in the metadata, allowing you to provide the index information relative to the fields in your Java classes. An index on a single field is usually specified as a nested extension of a field element. If the index includes more than one column, it will likely be specified with an extension of the class element, so that you can specify the order of the fields in the index. |
[ Team LiB ] |