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

标签 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)继承自一个拥有“name”属性的实体,@orderby 在该属性上被激活。

这是目标实体:

@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;
   }


}

和父类(super class):

@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

从查询中可以清楚地看出,排序依据应该在publisher1_1_上,我是做错了什么还是这是一个错误?

谢谢。

最佳答案

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

关于java - 使用@OrderBy注释生成错误的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3957773/

相关文章:

java - Spring Data JPA - 将@Query 与多对多关系/连接表结合使用

java - 读入和写出

java - 在 Java 中,什么时候我应该更喜欢 String 而不是 StringBuilder,反之亦然?

php - 约定表名(带下划线)

orm - Telerik OpenAccess 与 SubSonic 的简单速度测试(不是 "which is better")

Java从网络读取CSV文件

java - Netty 4.0 - StringDecoder 和 ChannelInboundMessageHandlerAdapter<String> 不工作

hibernate - 无法将实体与派生实体合并

java - org.hibernate.QueryException : could not resolve property: is_approved of: com

android - Sugar ORM 在初始化时阻塞 UI 线程