Hibernate继承,Collections和@OrderedBy超类属性生成MySQL语法错误 [英] Hibernate inheritance, Collections and @OrderedBy superclass attribute generates MySQL syntax Error

查看:130
本文介绍了Hibernate继承,Collections和@OrderedBy超类属性生成MySQL语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Hibernate 3.6.1来映射三个实体

$ pre $ @ $ e
@Inheritance(strategy = InheritanceType .JOINED)
公共类条目{
私有长ID;
私人日期publishedAt;

@Id
public getId(){...}

...
}

@Entity
public class Category {
private Long id;

列表< Podcast>播客;

@Id
public getId(){...}

@OneToMany(mappedBy =category,cascade = {CascadeType.ALL},fetch = FetchType.EAGER)
@OrderBy(publishedAt)
public List< Podcast> getPodcasts(){
返回播客;


code




  @Entity 
public class Podcast extends Entry {

private Category category;

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




$ b如果我试图获取一个类别实例,我得到一个异常

  com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException :'订单子句'中的未知列'podcasts0_.Entry.publishedAt'

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

解决方案

这是由以下错误导致的: HHH-3577使用连接的子类时,按顺序错误地使用了SQL。



可以在 podcasts上删除 @OrderBy fetch = FetchType.EAGER 和使用下面的查询加载类别,而不是 get()

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


I' using Hibernate 3.6.1 to map three entities

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

and

@Entity
public class Podcast extends Entry {

   private Category category; 

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

If i try to fetch a Category instance, i get an Exception

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

What causes this exception? Whats wrong with this mapping?

解决方案

It's caused by the following bug: HHH-3577 Wrong SQL in order by clause when using joined subclasses.

As a workaround you can remove @OrderBy and fetch = FetchType.EAGER on podcasts and load category using the following query instead of get():

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

这篇关于Hibernate继承,Collections和@OrderedBy超类属性生成MySQL语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆