开发者

In hibernate is there a way to retrieve a list of entities ordered by a property of a many-to-one relationship object

开发者 https://www.devze.com 2023-02-21 19:47 出处:网络
I have a many-to-one relationship as follows <hibernate-mapping default-lazy=\"false\" package=\"com.my.sample.data\">

I have a many-to-one relationship as follows

    <hibernate-mapping default-lazy="false" package="com.my.sample.data">
  <class name="Person" table="person_table">
<!-- other stuff -->
 <many-to-one class="Company" column="p_id" fetch="join" insert="false" name="company" update="false" not-found="ignore" not-null="false" />

Company maps to company_table and has a property company_id. Is there a way to fetch a list of Person ordered by Company.company_id via hibernate. So when I query

  items = (List<A>) getSession().createCriteria(Person.class)                        
                        .add(Restrictions.in("person_id", pIds)
                        ).list();

So I end up with a list of Person ordered by company_id. The only addOrder syntax I found took a property name like

addOrder(Order.asc("person_id").` 

开发者_StackOverflow社区I could not find how to refer to a property name of a foreign object something akin to

addOrder.Order.asc("company.company_id")

Thanks.


Try this. I think this will help. Base on the Criteria theory. Criteria cannot do implicit join. so we make it explicit.

Example:

items = (List<A>) getSession().createCriteria(Person.class)                        
                        .add(Restrictions.in("person_id", pIds)
                        .createCriteria("company")
                        .addOrder("company_id");
                        ).list();

Hope this useful for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号