Java 休眠异常:缺少列(列存在)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21237514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:32:53  来源:igfitidea点击:

Hibernate Exception: Missing Column (column exists)

javasqlhibernate

提问by Samuel Smith

Okay, so within the database we have a table called distributionCompanies, created like so:

好的,所以在数据库中我们有一个名为 distributionCompanies 的表,创建如下:

CREATE TABLE `distributionCompanies` (
    `distributionCompanyID` INT(11) NOT NULL,
    `distributionCompanyName` VARCHAR(255) NOT NULL,
     PRIMARY KEY (distributionCompanyID)
);

I'm trying to map this table to a class using Hibernate:

我正在尝试将此表映射到使用 Hibernate 的类:

@Entity
@Table(name = "distributionCompanies")
public class DistributionCompany implements DatabaseObject {
    @Id
    @GeneratedValue
    @Column(name = "distributionCompanyID", length = 11, unique = true, nullable = false)
    private int distributionCompanyID;
....

However, when running, I hit this issue:

但是,在运行时,我遇到了这个问题:

Initial SessionFactory creation failedorg.hibernate.HibernateException: Missing column: distributionCompanyID_distributionCompanyID in database2.distributionCompanies

This isn't the only table in the database, and I've managed to map other classes successfully using the same method, so I'm a little stumped as to why this is causing an issue.

这不是数据库中唯一的表,而且我已经设法使用相同的方法成功地映射了其他类,所以我有点不明白为什么这会导致问题。

Thank you for your time, Samuel Smith

谢谢你的时间,塞缪尔史密斯

EDIT: In response to Xavi's comment, I temporarily removed another mapping for the column, and the error went away, so the bad-egg probably lays in the following code:

编辑:为了回应 Xavi 的评论,我暂时删除了该列的另一个映射,错误消失了,因此坏蛋可能位于以下代码中:

@ManyToOne(targetEntity = DistributionCompany.class)
@JoinTable(name = "distributionCompanies", joinColumns = { @JoinColumn(name =    "distributionCompanyID", nullable = false) })
private int distributionCompanyID;

采纳答案by Xavi López

Hibernate is looking for a column named distributionCompanyID_distributionCompanyIDin your distributionCompaniestable.

Hibernate 正在寻找distributionCompanyID_distributionCompanyID在您的distributionCompanies表中命名的列。

This is probably due to a ToOneassociation mapping towards this table without @JoinColum.

这可能是由于ToOne没有@JoinColum.

From Hibernate Documentation:

休眠文档

The @JoinColumn attribute is optional, the default value(s) is like in one to one, the concatenation of the name of the relationship in the owner side, _ (underscore), and the name of the primary key column in the owned side. In this example company_id because the property name is company and the column id of Company is id.

@JoinColumn 属性是可选的,默认值就像一对一,所有者端的关系名称,_(下划线)和所有者端的主键列名称的串联. 在此示例中为 company_id,因为属性名称为 company,而 Company 的列 id 为 id。

If you've got a @ManyToOneor @OneToOneassociation mapping in another entity, this would explain why Hibernate is looking for such a column.

如果您在另一个实体中有 a @ManyToOneor@OneToOne关联映射,这将解释为什么 Hibernate 正在寻找这样的列。

EDITSeeing the association mapping you posted, it looks like it should be:

编辑看到您发布的关联映射,它看起来应该是:

@ManyToOne(targetEntity = DistributionCompany.class)
@JoinColumn(name = "distributionCompanyID")
private DistributionCompany distributionCompany;

The @JoinTableannotation is used to specify a join table (that means an intermediate table used to model many-to-many associations). And the point of mapping an association would be to dispose of the mapped object instance (in this case a DistributionCompany, not just a distributionCompanyId).

@JoinTable注释被用来指定一个连接表(即装置的中间表用于许多-to-many关联模型)。映射关联的目的是处理映射的对象实例(在这种情况下是 a DistributionCompany,而不仅仅是 a distributionCompanyId)。