Java Reference
In-Depth Information
Another limitation of the EJB 3 O/R mapping is that although it supports lists it
does not guarantee to preserve the ordering unless you use the @OrderBy annota-
tion. The @OrderBy annotation specifies how to sort the list when it is retrieved
from the database. You can either sort by the primary key of the element or by a
field or property. This means that, for example, in order to persist Restau-
rant.menuItems , which is a list of MenuItem s, you must add an index field to the
MenuItem class and write code to maintain it:
@Entity(access=AccessType.FIELD)
public class Restaurant {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OrderBy("index")
private List<MenuItem> menuItems;
}
@Entity (access=AccessType.FIELD)
@Table(name="MENU_ITEM")
public class MenuItem implements Serializable {
private int index;
}
In this example, the @OrderBy annotation on the Restaurant.menuItems specifies
that the list should be sorted by the MenuItem.index field. This is a minor change,
but it's a shame that you need to do this given that Hibernate and JDO will auto-
matically maintain the ordering.
If you are willing to sacrifice portability, then you can use a vendor-specific
extension such as JB oss's @OrderBy annotation:
@Entity(access=AccessType.FIELD)
public class Restaurant {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@IndexColumn(name="MENU_ITEM_INDEX")
private List<MenuItem> menuItems;
}
The @IndexColumn annotation tells JBoss EJB 3 to maintain the index of each Menu-
Item in the MENU_ITEM_INDEX column of the MENU_ITEM table.
Another feature missing from EJB 3 is the ability to automatically delete a child
entity when it is removed from its parent's collection. It might sound harsh, but
Hibernate and JDO can be configured to automatically delete such orphans. This
 
 
 
 
Search WWH ::




Custom Search