Annotation OneToMany
Target: Fields (including property get methods)
Defines a many-valued association with one-to-many
multiplicity.
If the collection is defined using
generics to specify the element type, the associated target entity type need
not be specified; otherwise the target entity class must be specified. If the
relationship is bidirectional, the mappedBy element must be
used to specify the relationship field or property of the entity that is the
owner of the relationship.
1.
Bidirectional
Customer.java
@OneToMany(mappedBy="customer")
private Set<Order> orders = new HashSet<Order>();
Order.java
@ManyToOne
@JoinColumn(name="CUST_ID", nullable=false)
private Customer customer;
2.
Unidirectional
Customer.java
@JoinColumn(name="CUST_ID")
// join column is in table for Order
public
Set<Order> getOrders() {return orders;}
Annotation ManyToOne
Target: Fields (including property get methods)
Defines a single-valued association to another entity class
that has many-to-one multiplicity. It is not normally necessary to specify the
target entity explicitly since it can usually be inferred from the type of the
object being referenced. If the relationship is bidirectional, the non-owning OneToMany entity side must use the mappedBy element to
specify the relationship field or property of the entity that is the owner of
the relationship.
UserList.java
@ManyToOne(optional=false)
@JoinColumn(name="Active_ID1", nullable=false, updatable=false)
private Active active;
@ManyToOne(optional=false)
@JoinColumn(name="Active_ID1", nullable=false, updatable=false)
public Active getActive() {
return active;
}
0 comments: