org.hibernate.exception.SQLGrammarException:列名无效

时间:2015-03-20 11:40:07

标签: java hibernate jpa

每当我使用我定义的实体时,我都会遇到下面的异常。

org.hibernate.exception.SQLGrammarException: Invalid column name 'coordinator_sycs_coord_id'.
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)

我将在相关实体和Hibernate生成的查询下面发布。上下文是两个在关联表中具有多对多关系的实体。我觉得有趣的是Hibernate生成的查询正在改变列名,即使我在我的注释中正确使用它。见下文:

@Entity
@Table(name = "sycs_coord")
public class SycsCoordinator {

    @Id
    @GeneratedValue
    @Column(name = "sycs_coord_id")
    Long id;

    @OneToMany(mappedBy = "club", fetch = FetchType.LAZY)
    Set<SycsCoordinatorClub> clubs;

    //Standard setters and getters below
}

@Entity
@Table(name = "sycs_coord_clb")
@IdClass(SycsCoordinatorClubPk.class)
public class SycsCoordinatorClub {

    @Id
    @Column(name = "sycs_coord_id")
    Long sycs_coord_id;

    @Id
    @Column(name = "clb_id")
    String clb_id;

     @ManyToOne
    @PrimaryKeyJoinColumn(name = "sycs_coord_id", referencedColumnName="sycs_coord_id")
    SycsCoordinator coordinator;

    @ManyToOne
    @PrimaryKeyJoinColumn(name = "clb_id", referencedColumnName = "Clb_id")
    Club club;

}

我暂时不包括课程ClubSycsCoordinatorClubPk,因为它们似乎与问题无关。 Hibernate生成的查询有几次:

select
    clubs0_.club_Clb_Id as club4_0_3_,
    clubs0_.clb_id_fk as clb1_3_,
    clubs0_.sycs_coord_id as sycs2_3_,
    clubs0_.clb_id_fk as clb1_2_2_,
    clubs0_.sycs_coord_id as sycs2_2_2_,
    clubs0_.club_Clb_Id as club4_2_2_,
    clubs0_.coordinator_sycs_coord_id as coordina5_2_2_,
    clubs0_.lst_updt_dt as lst3_2_2_,
    clubs0_.sycs_coord_secur_grp_cd as sycs6_2_2_,
    sycscoordi1_.sycs_coord_id as sycs1_0_0_,
    sycscoordi2_.sycs_coord_secur_level_id as sycs4_3_1_ 
from
    sycs_coord_clb clubs0_ 
left outer join
    sycs_coord sycscoordi1_ 
        on clubs0_.coordinator_sycs_coord_id=sycscoordi1_.sycs_coord_id 
where
    clubs0_.club_Clb_Id=?

请注意,有时列名coordinator_sycs_coord_id会出现在查询中,即使任何注释中没有此类名称也是如此。这是为什么?

1 个答案:

答案 0 :(得分:2)

您误用了@PrimaryKeyJoinColumn注释,因此产生了奇怪的结果:

  

它用于连接实体子类的主表   JOINED映射策略到其超类的主表;它是   在SecondaryTable注释中用于将辅助表连接到a   主表;它可以用在OneToOne映射中   引用实体的主键用作外键   引用实体。

http://docs.oracle.com/javaee/5/api/javax/persistence/PrimaryKeyJoinColumn.html

您可能应该使用@JoinColumn:

@JoinColumn(name = "sycs_coord_id", referencedColumnName = "sycs_coord_id")