Which collection type can choose in mapping in hibernate
I have two entities i.e.,customer and order.A customer having mu开发者_如何转开发litple 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?
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 lists, where the order is not materialized in the database
- Indexed lists, where the order is materialized in the database
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.
精彩评论