How to dynamically order many-to-many relationship with JPA or HQL?

被刻印的时光 ゝ 提交于 2019-12-20 14:15:36

问题


I have a mapping like this:

@ManyToMany(cascade = CascadeType.PERSIST)
    @JoinTable(
            name="product_product_catalog",
            joinColumns={@JoinColumn(name="product_catalog", referencedColumnName="product_catalog")},
            inverseJoinColumns={@JoinColumn(name="product", referencedColumnName="product")})
    public List<Product> products = new ArrayList<Product>();

I can fetch the products for the catalog nicely, but I can't (dynamically) order the products. How could I order them? I probably have to write a many-to-many HQL query with the order-by clause? I though of passing the orderBy field name string to the query, or is there a better solution?

Tables are: products, product_catalog, product_product_catalog (associative table)

P.S. Using Play! Framework JPASupport for my entities.


回答1:


I can fetch the products for the catalog nicely, but I can't (dynamically) order the products. How could I order them?

Not possible if you let JPA retrieve the association. Mappings are static, the best thing you can do with mappings is to specify the order at "retrieve time":

9.1.28 OrderBy Annotation

The OrderBy annotation specifies the ordering of the elements of a collection valued association at the point when the association is retrieved.

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

The syntax of the value ordering element is an orderby_list, as follows:

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

If ASC or DESC is not specified, ASC (ascending order) is assumed.

If the ordering element is not specified, ordering by the primary key of the associated entity is assumed.

The property or field name must correspond to that of a persistent property or field of the associated class. The properties or fields used in the ordering must correspond to columns for which comparison operators are supported.

Example:

@Entity public class Course {
  ...
  @ManyToMany
  @OrderBy("lastname ASC")
  public List<Student> getStudents() {...};
  ...
}

So either use a custom finder or sort your list from Java using a Comparator.



来源:https://stackoverflow.com/questions/3002135/how-to-dynamically-order-many-to-many-relationship-with-jpa-or-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!