JPA has become a standard industry approach for Object to Relational Mapping (ORM) in the Java Industry. JPA is just a specification (set of interfaces), and there are several open-source and commercial JPA implementations (persistence providers), such as DataNucleus, EclipseLink (formerly TopLink), Hibernate, ObjectDB, OpenJPA etc.
Mapping between database tables and Java objects is done via persistence metadata:
1) annotations in Java class file, or
2) XML configuration (it overwrites the annotations), or
Basic JPA annotations
At the class level:
@Entity - to mark an entity which need to be saved in database.
@Table(name="somename") - optional, present if we want to override some default setting, such as table name (need to be different from the name of the class) or other.
At the field level:
@Column(name="someName") - optional, present if we want to override some default setting, such as name of the column (needs to be different from the name of the field) or other.
@Transient - to mark the fields that should not be persisted.
@Id - specifies a primary key property/field; if @Id is placed on the field then only the fields are considered for persistence and the state is accessed via the field, but if @Id is placed on the getter, then only the getters are considered for persistence and the state is accessed via the getter/setter (recommended).
@GeneratedValue(strategy=GenerationType.AUTO) - used together with @Id for specifying generation strategy for the primery key values
AUTO - either id column, sequence or table depending on the underlying DB (preffered for the poratable applications - accross several DB vendors)
TABLE - table holding the id
IDENTITY - id column
SEQUENCE - sequence
identity copy - the identity copied from another entity
JPA Annotations that define an association to another entity
@OneToOne, @OneToMany, @ManyToOne, @ManyToMany
Names speak for temselves. First part of the name addresess the entity you are currently observing, and the second addresses second entity in the relation.
The relationship can be:
Unidirectional - only one class has a reference to the other class
Bidirectional - both classes have a reference to each other, but only one of the two classes in relation can be the owner i.e. responsible for the association column(s) update. Side that should not be responsible for the relationship is declared by using mappedBy attribute. This attribute refers to the property name of the association on the owner side. So, you don't need to declare join column on this side, since it's already declared on the owner side.
1) Reference (full set of explanations + hibernate annotation extensions): http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-mapping-association
2) OpenDB (well-organized annotation samples): http://www.objectdb.com/api/java/jpa/annotations
3) Basic JPA info (facts/history): http://en.wikibooks.org/wiki/Java_Persistence/What_is_JPA%3F