You would want to use the @OneToMany
, @ManyToOne
, and/or @ManyToMany
annotations to map relationships between all of the entities in your project.
The oracle documentation for those has some pretty good examples, like this one:
Example 1: One-to-Many association using generics
In Customer class:
@OneToMany(cascade=ALL, mappedBy="customer")
public Set<Order> getOrders() { return orders; }
In Order class:
@ManyToOne
@JoinColumn(name="CUST_ID", nullable=false)
public Customer getCustomer() { return customer; }
In this example, both Customer
and Order
would be classes annotated with @Entity
and @Table
that correspond to CUSTOMER
and ORDER
tables in your database. Any other columns in those tables would also be mapped in the corresponding entity class. The @OneToMany
, @ManyToOne
and @ManyToMany
are used to map foreign keys between your tables, and allow you to have direct references to parent/child rows/objects directly from your entity instances.
With these annotations, you can map as many foreign key references to other tables as you need.