使用@OrderBy注释生成错误的查询

时间:2010-10-18 09:12:53

标签: java hibernate orm annotations

我正在使用Hibernate3,我有一个包含以下集合的实体:

@ManyToMany  
@JoinTable(name = "buys_publishers", joinColumns=@JoinColumn(name="buy_id", referencedColumnName = "buy_id"), inverseJoinColumns=@JoinColumn(name = "publisher_id", referencedColumnName = "publisher_id"))  
@OrderBy("name")  
private List<Publisher> publishers;

(省略取和级联减速)

目标实体(Publisher)继承自保存激活了@orderby的“name”属性的实体。

这是目标实体:

@Entity
@Table(name="publishers")
@PrimaryKeyJoinColumn(name="account_id")
public class Publisher extends Account{

   /**
    * 
    */
   private static final long serialVersionUID = 1L;

   @Column(name = "publisher_id")
   private Long publisherId;

   public Long getPublisherId() {
      return publisherId;
   }

   public void setPublisherId(Long publisherId) {
      this.publisherId = publisherId;
   }


}

和超级班:

@Entity
@Table(name="accounts")
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Account implements Serializable{

   /**
    * 
    */
   private static final long serialVersionUID = 1L;


   @Id
   @Column(name="id",unique=true, nullable=false )
   @GeneratedValue( strategy = IDENTITY )
   private long id;

   @Column(name = "name")
   private String name;

   @Column(name = "account_type")
   @Enumerated(EnumType.ORDINAL)
   private AccountType accountType;

   public long getId() {
      return id;
   }

   public void setId(long id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public AccountType getAccountType() {
      return accountType;
   }

   public void setAccountType(AccountType accountType) {
      this.accountType = accountType;
   }

}

Hibernate生成的查询是:

select publishers0_.buy_id as buy1_1_, publishers0_.publisher_id as publisher2_1_, publisher1_.account_id as id6_0_, publisher1_1_.account_type as account2_6_0_, publisher1_1_.name as name6_0_, publisher1_.publisher_id as publisher1_18_0_ from buys_publishers publishers0_ left outer join publishers publisher1_ on publishers0_.publisher_id=publisher1_.publisher_id left outer join accounts publisher1_1_ on publisher1_.account_id=publisher1_1_.id where publishers0_.buy_id=? order by accounts.name asc

从查询中可以清楚地知道,order by应该在publisher1_1_上,我做错了什么或者这是一个错误?

谢谢。

1 个答案:

答案 0 :(得分:2)

这看起来像HHH-4260 - @OrderBy does not work with inherited properties(我不希望短期解决)。