【问题标题】:Hibernate inheritance, Collections and @OrderedBy superclass attribute generates MySQL syntax ErrorHibernate 继承、Collections 和 @OrderedBy 超类属性生成 MySQL 语法错误
【发布时间】:2011-07-20 14:16:30
【问题描述】:

我使用 Hibernate 3.6.1 映射三个实体

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Entry {
   private Long id; 
   private Date publishedAt; 

   @Id
   public getId() {...}

   ...
}

@Entity
public class Category {
   private Long id; 

   List<Podcast> podcasts;

   @Id
   public getId() {...}

   @OneToMany(mappedBy = "category", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
   @OrderBy("publishedAt")
   public List<Podcast> getPodcasts() {
      return podcasts;
   }
}

@Entity
public class Podcast extends Entry {

   private Category category; 

   @ManyToOne(fetch = FetchType.EAGER)
   public PodcastsCategory getCategory() {
      return category;
   }
}

如果我尝试获取 Category 实例,我会得到一个异常

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'podcasts0_.Entry.publishedAt' in 'order clause'

是什么导致了这个异常?这个映射有什么问题?

【问题讨论】:

    标签: java mysql hibernate inheritance


    【解决方案1】:

    这是由以下错误引起的:HHH-3577 Wrong SQL in order by clause when using joined subclasses

    作为一种解决方法,您可以删除podcasts 上的@OrderByfetch = FetchType.EAGER,并使用以下查询而不是get() 加载类别:

    SELECT DISTINCT c 
    FROM Category c LEFT JOIN FETCH c.podcasts p
    WHERE c.id = ?
    ORDER BY p.publishedAt
    

    【讨论】:

      【解决方案2】:

      你可以试试注解@MappedSuperClass。请参阅休眠文档的2.2.4.4. Inherit properties from superclasses 部分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-14
        • 2015-05-08
        • 2017-10-07
        相关资源
        最近更新 更多