Reputation: 141
I have two entities i.e.,customer and order.A customer having mulitple orders.I trying to bidirectional one to many and many to one.So,Which collection object can i choose i.e.,bag,map etc.So,what basis can i chose collection object using one-to-many and many-to-one bi directional relation ship?
Upvotes: 3
Views: 653
Reputation: 18639
If each Order is Unique and customer can't have the same Order twice in the same collection use Set otherwise use List
First of all, remember that you MUST override equals() and hashCode() functions for each class:
When using List you can map it in different way:
Ordered List is implemented the following way:
@OneToMany(mappedBy="customer")
@OrderBy("number")
public List<Order> getOrders() { return orders; }
Indexed List mapped the following way:
@OneToMany(mappedBy="customer")
@OrderColumn(name="orders_index")
public List<Order> getOrders() { return orders; }
To store the index value in a dedicated column, use the @javax.persistence.OrderColumn
annotation on your property. This annotations describes the column name and attributes of the column keeping the index value. This column is hosted on the table containing the association foreign key. If the column name is not specified, the default is the name of the referencing property, followed by underscore, followed by ORDER (in the following example, it would be orders_ORDER).
An if you want to use Set<Order>
this is very simple:
@OneToMany(mappedBy="customer")
public Set<Order> getOrders() { return orders; }
Hope it helps.
Upvotes: 2