gpt4 book ai didi

java - org.hibernate.exception.SQLGrammarException : Invalid column name

转载 作者:行者123 更新时间:2023-11-30 08:55:12 25 4
gpt4 key购买 nike

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

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 会出现在查询中,即使在任何注释中都没有这样的名称。这是为什么?

最佳答案

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

It is used to join the primary table of an entity subclass in the JOINED mapping strategy to the primary table of its superclass; it is used within a SecondaryTable annotation to join a secondary table to a primary table; and it may be used in a OneToOne mapping in which the primary key of the referencing entity is used as a foreign key to the referenced entity.

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

您可能应该改用 @JoinColumn:

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

关于java - org.hibernate.exception.SQLGrammarException : Invalid column name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29165688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com