-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: hibernate criteria query order by join table field not work
PostPosted: Thu Sep 22, 2016 10:56 am 
Newbie

Joined: Tue Sep 20, 2016 11:54 am
Posts: 3
I use mysql database and have two tables `chapter` and `chapter_title_1`, there is a fk `chapter_id` in table `chapter_title_1` reference to table `chapter`.

Image Image

Two entity class are `ChapterEntity.class` refer to table `chapter` and `ChapterTitle1Entity.class` refer to table `chapter_title_1`. There is a List<ChapterTitle1Entity> chapterTitle1EntityList in `ChapterEntity.class` annotated with `@OneToMany`. I want to get a list of `ChapterEntity` ordered by ChapterEntity.sequence asc and a list of `ChapterTitle1Entity` in each item of `ChapterEntity` ordered by `ChapterTitle1Entity.sequence` desc.

In my ADO layer I use criteria to query my database. I use the following code to get the list:

Code:
Criteria criteria = session().createCriteria(ChapterEntity.class, "chapter");
criteria.createAlias("chapter.chapterTitle1EntityList", "title1List");
criteria.addOrder(Order.asc("chapter.sequence")).
        addOrder(Order.desc("title1List.sequence"));
List<ChapterEntity> chapterEntityList = criteria.list();
return chapterEntityList;


but no matter it is `addOrder(Order.desc("title1List.sequence")` or `addOrder(Order.asc("title1List.sequence")`, the order of List `ChapterTitle1Entity` remain the same.


Top
 Profile  
 
 Post subject: Re: hibernate criteria query order by join table field not work
PostPosted: Thu Sep 22, 2016 2:53 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
If you to fetch the one-to-many association according to a given order, you can use the @OrderBy or the @OrderColumn annotation.

The Criteria query can order the parent entities, but I'm not sure if that applies to child collections as well.


Top
 Profile  
 
 Post subject: Re: hibernate criteria query order by join table field not work
PostPosted: Fri Sep 23, 2016 11:19 am 
Newbie

Joined: Tue Sep 20, 2016 11:54 am
Posts: 3
Thank you a lot. I have learned a lot form you answer and https://vladmihalcea.com/how-to-optimize-unidirectional-collections-with-jpa-and-hibernate/, but I still don't kown how to solve my problem.

Can you post a example or give relevent materials that I can get a list of "ChapterEntity", and lists of "ChapterTitle1Entity" obder by "ChapterTitle1Entity.sequence" desc in each ChapterEntity. Thanks again!

the following is my ChapterEntity.java and ChapterTitle1Entity.java files:

ChapterEntity.java

Code:
@Entity
@Table(name = "chapter")
public class ChapterEntity {
    private List<ChapterTitle1Entity> chapterTitle1EntityList;
    private long id;
    private int sequence;
    private String content;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "chapterEntity")
    @Fetch(FetchMode.SUBSELECT)
    public List<ChapterTitle1Entity> getChapterTitle1EntityList() {
        return chapterTitle1EntityList;
    }

    public void setChapterTitle1EntityList(List<ChapterTitle1Entity> chapterTitle1EntityList) {
        this.chapterTitle1EntityList = chapterTitle1EntityList;
    }

    ......
}



ChapterTitle1Entity.java

Code:
@Entity
@Table(name = "chapter_title_1", schema = "tutorial2")
public class ChapterTitle1Entity {
    private long id;
    private int sequence;
    private String content;
    private int type;
    private ChapterEntity chapterEntity;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "chapter_id", nullable = false)
    public ChapterEntity getChapterEntity() {
        return chapterEntity;
    }

    public void setChapterEntity(ChapterEntity chapterEntity) {
        this.chapterEntity = chapterEntity;
    }

    @Basic
    @Column(name = "sequence", nullable = false)
    public int getSequence() {
        return sequence;
    }
    ......
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.