Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/Examples/JPA/Collectionordering


Catnicon.gif This page is under construction. See bug bug 225026 for more information and to provide feedback on this work-in-progress. Catnicon.gif
STATUS: Jan 15/09: Not all test cases are passing. Please use with caution.


Maintain Collection Ordering

In some applications it is valuable to maintain collection ordering within the Java application. EclipseLink JPA provides support for ordering the target of a collection mapping (OneToMany, ManyToMany) but the order is not preserved in-memory. This example illustrates how an index column can be mapped in the target of a OneToMany mapping and used during the lifecycle of the entities to esnure the order set in the application is maintained when the collection is persisted and re-read.

In order to have a collection ordered when it is read the @OrderBy configuration must be specified on the collection mapping. EclipseLink JPA will then add ORDER BY into the SQL when the collection is read in and return the initial result with this ordering. After this EclipseLink will only ensure that the collection's order as maintained by the application in a transaction is preserved when merging into the shared cache. This means that any application code modifying a collection must ensure that order is the way they want it prior to committing the changes in a transaction.

Re-Ordering using Events

public class MaintainOrderLineItemsIndex extends DescriptorEventAdapter implements DescriptorCustomizer {
 
	private void setLineNumbers(Order order) {
		for (int index = 0; index < order.getLineItems().size(); index++) {
			LineItem item = order.getLineItems().get(index);
			item.setLineNumber(index);
		}
	}
 
	@Override
	public void preInsert(DescriptorEvent event) {
		setLineNumbers((Order) event.getSource());
	}
 
	@Override
	public void preUpdate(DescriptorEvent event) {
		setLineNumbers((Order) event.getSource());
	}
 
	@Override
	public void postMerge(DescriptorEvent event) {
		setLineNumbers((Order) event.getOriginalObject());
		setLineNumbers((Order) event.getSource());
	}
 
	@Override
	public void prePersist(DescriptorEvent event) {
		setLineNumbers((Order) event.getSource());
	}
 
	public void customize(ClassDescriptor descriptor) throws Exception {
		descriptor.getEventManager().addListener(this);
	}
 
}

Ordering by Index

Downloading and running the Example

The example is available for download here:

In order to run this example you must have EclipseLink installed on the machine and must have a relational database with its JDBC driver available. In order to customize the example you must:

  • To run using ANT you must configure the build.properties file
  • To customize the database you must edit src/META-INF/persistence.xml

In order to verify the database schema creation and population run testing.CreateDatabase. To perform all of the tests run testing.AllTests.

Back to the top