Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/Collectionordering"

(Downloading and running the Example)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 +
 +
<div style="border:1px solid #999999;background-color:#ffffff;align:center">
 +
<table border="0" cellpadding="5" align="center"><tr><td width="20">[[image:Catnicon.gif]]</td><td>This page is under construction. See bug {{bug|225026}} for more information and to provide feedback on this work-in-progress.
 +
<p>
 +
STATUS: Jan 15/09: Not all test cases are passing. Please use with caution.
 +
</td></tr></table>
 +
</div>
 +
  
 
== Maintain Collection Ordering ==
 
== Maintain Collection Ordering ==

Revision as of 09:00, 15 January 2009


Catnicon.gifThis page is under construction. See bug bug 225026 for more information and to provide feedback on this work-in-progress.

STATUS: Jan 15/09: Not all test cases are passing. Please use with caution. </td></tr></table> </div>

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.