Sort (attribute of property)

From JRapid

(Redirected from Sort)
Jump to: navigation, search

The "sort" attribute is used for sortedset properties which are not embedded. It should be specified as the name of a class that implements java.util.Comparator or the special case "natural" can be used.

When using the sort attribute, the Hibernate mapping file (aka HBM) will add the sort attribute to the definition of this property.


Valid examples of sort

  • natural
  • com.example.MyClass (if MyClass implements java.util.Comparator)


 <property ... collection =  "sortedset" sort = "COMPARATOR_CLASS | natural" embedded = "inline | detail" ... />


Given the following "Product" entity, we want the images be ordered by size. As the "image" property is not an embedded property we can create a Comparator (which will compare "ProductImage" objects) to sort the product images.

<entity label="Product" menu="Products" name="Product">
     <property collection="sortedset" entity="ProductImage" extendable="extendable" height="100"
     label="Image" labelposition="top" name="image" sort="com.minicrm.sort.ImageComparator" width="100"/>

package com.minicrm.sort;

import java.util.Comparator;

import com.minicrm.entities.ProductImage;

public class ImageComparator implements Comparator<ProductImage> {

	public int compare(ProductImage image1, ProductImage image2) {
		return ((Integer) image1.getImage().length())
				.compareTo(((Integer) image2.getImage().length()));

See also

Personal tools