使用JPQL“WHERE TYPE(x)= X”的MySQLSyntaxErrorException

时间:2015-01-25 10:14:06

标签: hibernate jpql

我有这个抽象的兽医课程:

    @Entity
    @Table(name = "vet")
    @Inheritance(strategy = InheritanceType.JOINED)
    @DiscriminatorColumn(name = "vet_type")
    public abstract class Vet implements Serializable {

      @Id
      @Column(name = "vet_id")
      @TableGenerator(name = "idGenTable", table = "id_gen", pkColumnName = "gen_name", valueColumnName = "gen_value", pkColumnValue = "vet_id_gen", initialValue = 1)
      @GeneratedValue(generator = "idGenTable")
      private Integer id;

      ...
    }

然后是这两个扩展类,InHouseVetExternalVet

@Entity
@Table(name = "in_house_vet")
@DiscriminatorValue("in_vet")
public class InHouseVet extends Vet {
  ...
}

@Entity
@Table(name = "external_vet")
@DiscriminatorValue("ext_vet")
public class ExternalVet extends Vet {
   ...
}

获取InHouseVet

的查询
Query query = em.createQuery("SELECT v FROM Vet v WHERE TYPE(v) = InHouseVet");
List<Vet> vets = query.getResultList();

这导致

  

引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:   未知列&#39; \ text_vet&#39;在&#39; where子句&#39;

有人能看到这里出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

从上面的代码片段中,Hibernate(版本4.3.7 Final)生成以下内容..(注意第1和第2个when子句中缺少的引号)。

select
    vet0_.vet_id as vet_id2_10_,
    vet0_.name as name3_10_,
    vet0_.qualification as qualific4_10_,
    vet0_1_.salary as salary1_8_,
    vet0_2_.country as country1_6_,
    vet0_2_.visiting_fees as visiting2_6_,
    vet0_.vet_type as vet_type1_10_ 
from
    vet vet0_ 
left outer join
    in_house_vet vet0_1_ 
        on vet0_.vet_id=vet0_1_.vet_id 
left outer join
    external_vet vet0_2_ 
        on vet0_.vet_id=vet0_2_.vet_id 
where
    case 
        when vet0_1_.vet_id is not null then in_vet 
        when vet0_2_.vet_id is not null then ext_vet 
        when vet0_.vet_id is not null then 'Vet' 
    end='in_vet'

据报道这是bug