5 Replies Latest reply on Jun 26, 2009 7:29 PM by daxxy

    How to sort @OneToMany mapping class?

    prasad_k

      Hi All,
      Any one help me how to use to sort mapping class column field by using seam annotations



       @OneToMany(fetch = FetchType.EAGER,
                   mappedBy="test",
                  targetEntity=com.test.Test.class)
      List<Test> ts = ArrayList<Test>();
      


      i want to sort test class column field........
      Please help me
      Thank you

        • 1. Re: How to sort @OneToMany mapping class?
          stefanotravelli

          You are using JPA annotations here, not seam.


          What you need is @OrderBy.


          Let's orderingField be a property of Test, then:



          @OneToMany(fetch = FetchType.EAGER, mappedBy = "test")
          @OrderBy("orderingField asc")
          List<Test> ts = new ArrayList<Test>();
          
          


          • 2. Re: How to sort @OneToMany mapping class?
            prasad_k

            Thank you for reply,
            but it gives exception

            org.hibernate.AnnotationException: property from @OrderBy clause not found:


            @OneToMany(fetch = FetchType.EAGER, mappedBy = "test")
            @OrderBy("orderingField asc")
            List<Test> ts = new ArrayList<Test>();
            


            when we use this

            • 3. Re: How to sort @OneToMany mapping class?
              stefanotravelli

              Make sure that the property you use in the annotation value is correct.


              If the collection is a collection of Test and Test has a "name" property, then you can annotate with @OrderBy("name asc").


              • 4. Re: How to sort @OneToMany mapping class?
                prasad_k

                Hi Stefano Travelli,
                Thank you for reply,


                I tried it and it was working but when i change the position of element and run the code it doesn't sort the list of element by order, and the list return zero size it doesn't map.


                please help me..
                Thank you
                     

                • 5. Re: How to sort @OneToMany mapping class?
                  daxxy

                  I just got this to work (yoohoo!)


                  There is a syntax error above. You want


                  @OrderBy(clause="column_name")


                  where column_name is the column in table associated with the joined table. In your example, in the table associated with the test entity.


                  Hope this is not too late!


                  TDR