Mapping Entity Classes

See Also

An entity class is used to represent a table in a database, and the fields in an entity class correspond to columns in that table. In an entity class, you can use annotations to specify how fields in an entity class are mapped to the corresponding database columns and tables.

For example, the following @Column annotation marking the field address maps the field to the column named CUSTOMER_ADDRESS in the database table.

    @Column(name = "CUSTOMER_ADDRESS")
    private String address;
        

The following annotations are commonly used when mapping entity classes.

Annotation Description
@Id Specifies the primary key property or field of an entity.
@GeneratedValue Allows you to specify the strategy that automatically generates the values of primary keys. Used with @Id.
@Column Specifies a mapped column for a persistent property or field.
@ManyToMany Defines a many-valued association with many-to-many multiplicity.
@ManyToOne Defines a single-valued association to another entity class that has many-to-one multiplicity.
@OneToMany Defines a many-valued association with one-to-many multiplicity.

For more on using annotations and annotation elements to map entities in an enterprise application, see the Java EE 6 Tutorial:

For more on the specifications on annotations and annotation elements, see the Java EE 6 API specifications for javax.persistence:

See Also
Working with Enterprise Applications
About Java Persistence
Creating an Entity Class
Generating Persistent Entity Classes from a Database

Legal Notices