2 Replies Latest reply on Jan 18, 2012 12:49 AM by williamfitz

    OrderBy javax vs hibernate

    blabno

      I have two classes : abstract Location, Country and Province. Country has collection of provinces and it should be ordered by province name. Name attribute is defined in Location. If I used javax.persistence.OrderBy annotation on it then everything works fine in production and dev profile, but in test entityManager creates such query :


      select
              provinces0_.COUNTRY_ID as COUNTRY1_1_,
              provinces0_.ID as ID1_,
              provinces0_.ID as ID104_0_,
              provinces0_.EMBLEM as EMBLEM104_0_,
              provinces0_.NAME as NAME104_0_,
              provinces0_.COUNTRY_ID as COUNTRY1_105_0_ 
          from
              PROVINCE provinces0_ 
          where
              provinces0_.COUNTRY_ID=? 
          order by
              Location.NAME asc



      Please note order by clause. It uses tablename of parent abstract class !


      Problem can be solved by using @org.hibernate.annotations.OrderBy instead of @javax.persistence.OrderBy.
      Why is @javax.persistence.OrderBy treated different in test environment then in dev or production ?
      For dev and production I use jboss-4.2.2.GA.


      @TableGenerator(name = "LOCATION_TABLES_GENERATOR", allocationSize = 1)
      @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
      @Entity
      public abstract class Location implements Serializable {
      
          @Id
          @GeneratedValue
          @Column(name = "ID")
          private Long id;
      
          @NotNull
          @Column(name = "NAME", unique = true, nullable = false)
          private String name;
      
          @Column(length = 1000000, name = "EMBLEM")
          private byte[] emblem;
      
          //GETTERS AND SETTERS
      }



      @Entity
      @Table(name = "COUNTRY")
      public class Country extends Location {
      
          @OrderBy("name")
          @OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
          private List<Province> provinces = new ArrayList<Province>();
      
          //GETTERS AND SETTERS
      }



      @Entity
      @Table(name = "PROVINCE", uniqueConstraints = @UniqueConstraint(columnNames = {"COUNTRY_ID", "NAME"}))
      public class Province extends Location {
      
          @ManyToOne(optional = false)
          @JoinColumn(name = "COUNTRY_ID", nullable = false)
          private Country country;
          
          //GETTERS AND SETTERS
      }

        • 1. Re: OrderBy javax vs hibernate
          daxxy
          <caveat>Going out on a limb to answer something I don't know a heck of a lot about.</caveat>

          Off the top of my head....

          I think one of the OrderBy's takes an actual column name as it's value and the other takes the entity attribute as its value.

          I ended up with

          OrderBy("dev_name asc")  <== column name

          instead of

          OrderBy("devName")  <== entity attribute

          by I had to tinker with it a bit to make it come out right.

          tdr
          • 2. Re: OrderBy javax vs hibernate
            williamfitz

            The smallest act of kindness is worth more than the grandest intention.
            Buy Essay Online