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

    OrderBy javax vs hibernate

    Bernard Labno Master

      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
      }