How to point an OrderBy clause to two columns

We would like to order two columns in the Seam EntityQuery interface, as well as in the JPA model. How do we do this?

@Entity public class A{ @OrderBy(???) // should this be hardcoded here, is it database agnostic List<B> bobjects; } @Entity public class B { public short startTimeHrs; public short startTimeMins; } @Name("bList") public class B extends EntityQuery { setOrderColumn("startTimeHrs, startTimeMins"); // Is this correct? setOrderDirection("asc"); } 
+7
hibernate jpa seam
source share
2 answers

If you are talking about javax.persistence.OrderBy , this annotation takes a list of comma-separated properties (the target object) into the parameter and organizes the collection accordingly. For example, in your case startTimeHrs asc, startTimeMins asc .

From the JPA 1.0 specification:

9.1.28 OrderBy Abstract

OrderBy annotation indicates the ordering of the elements of a collective assessment of when an association is received.

 @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface OrderBy { String value() default ""; } 

The syntax for ordering element values ​​is orderby_list, as follows:

 orderby_list::= orderby_item [,orderby_item]* orderby_item::= property_or_field_name [ASC | DESC] 

If ASC or DESC not specified, ASC (ascending).

If the ordering element is not specified, ordering using the primary key of the associated object.

The property or field name must match a constant property or field of the associated class. The properties or fields used in the order must match the columns for which comparison operators are supported.

+11
source share

Yes, you can use @OrderBy to order your queries.

 @OrderBy("startTimeHrs, startTimeMins") @OneToMany(...) getBobjects() { return bobjects; } 

Now when you say A.getBobjects() , they will be ordered. However, if you use the EntityQuery method to get the result, you can override getEjbql() and place the order there.

 @Name("bList") public class B extends EntityQuery { @Override public String getEjbql() { return "select b from B b order by startTimeHrs, startTimeMins"; } } 

Or you can @Override getResultList() manipulate the collection there in your entities

+13
source share

All Articles