Java Reference
In-Depth Information
Mapping an entity to a single table
@Table
specifies the table containing the columns to which the entity is mapped. The
name
parameter is most important. By default all the persistent data for the entity is
mapped to the table specified by the annotation's
name
parameter. As you can see from the
annotation's definition, it contains a few other parameters:
@Target(TYPE)
@Retention(RUNTIME)
public @interface Table {
String name() default "";
String catalog() default "";
String schema() default "";
Index[] indexes() default {};
UniqueConstraint[] uniqueConstraints() default {};
}
The
@Table
annotation is optional. By default, JPA will assume the name of the table is
JPA will assume the name of the database table is
Category
. The
@Table
annotation
allows you to override this conventional behavior. For example, if the database table is
named
ITEM_CATEGORY
, use the
@Table
annotation to configure JPA:
@Table(name="ITEM_CATEGORY")
public class Category
The
catalog
and
schema
elements are there if you need to further specify the location
of the table in the database. Schemas and catalogs are common database features and will
not be discussed further. But as an example, suppose the
ITEM_CATEGORY
table was in
the
ACTIONBAZAAR
schema. Use the
schema
parameter to configure JPA:
@Table(name="ITEM_CATEGORY", schema="ACTIONBAZAAR"))
public class Category
The
catalog
and
schema
elements aren't commonly used because those details are usu-
ally part of the data source configuration on your Java EE server. It's typically bad practice
for your code to know too much about the database because changes would require a re-
build of your code instead of a reconfiguration of the EE server.