休眠异常:缺少列(存在列) [英] Hibernate Exception: Missing Column (column exists)
问题描述
好的,在数据库中我们有一个名为distributionCompanies的表,如下所示:
CREATE TABLE`distributionCompanies`(
`distributionCompanyID` INT(11)NOT NULL,
`````````````
我试图用Hibernate将这张表映射到一个类:
@Entity
@Table(name =distributionCompanies)
public class DistributionCompany实现DatabaseObject {
@Id
@GeneratedValue
@Column(name =distributionCompanyID,length = 11,unique = true,nullable = false)
private int distributionCompanyID;
....
然而,在运行时,我遇到了这个问题:
创建初始SessionFactory failedorg.hibernate.HibernateException:缺少database2.distributionCompanies中的列:distributionCompanyID_distributionCompanyID
这不是数据库中唯一的表格,我已经设法使用相同的方法成功映射其他类,所以我有点难过至于为什么这是造成问题。
感谢您的时间,
塞缪尔史密斯
编辑:为了回应Xavi的评论,我暂时删除了该列的另一个映射,并且错误消失了,所以坏蛋可能在以下代码中:
@ManyToOne(targetEntity = DistributionCompany.class)
@JoinTable(name =distributionCompanies,joinColumns = {@JoinColumn(name =distributionCompanyID,nullable = false)})
private int distributionCompanyID;
Hibernate正在寻找名为 distributionCompanyID_distributionCompanyID
放入您的 distributionCompanies
表中。
这可能是由于 ToOne
关联映射到此表而没有 @JoinColum
。
从 Hibernate文档:
@JoinColumn属性是可选的,默认值是一对一的,并置所有者方的关系名称,_(下划线)以及所有权方主键列的名称。在这个示例中,company_id是因为属性名称是company,并且company的列id是id。
如果您有 @ManyToOne
或 @OneToOne
在另一个实体中关联映射,这可以解释为什么Hibernate正在寻找这样一个列。
编辑看到您发布的关联映射,它看起来应该是:
@ManyToOne(targetEntity = DistributionCompany.class)
@JoinColumn(name =distributionCompanyID)
private DistributionCompany distributionCompany;
@JoinTable
注释用于指定一个连接表(这意味着一个用于模拟多对多关联的中间表)。而映射关联的要点是处理映射的对象实例(在本例中为 DistributionCompany
,而不仅仅是 distributionCompanyId
)。
Okay, so within the database we have a table called distributionCompanies, created like so:
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:
@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:
@ManyToOne(targetEntity = DistributionCompany.class)
@JoinTable(name = "distributionCompanies", joinColumns = { @JoinColumn(name = "distributionCompanyID", nullable = false) })
private int distributionCompanyID;
Hibernate is looking for a column named distributionCompanyID_distributionCompanyID
in your distributionCompanies
table.
This is probably due to a ToOne
association mapping towards this table without @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.
If you've got a @ManyToOne
or @OneToOne
association mapping in another entity, this would explain why Hibernate is looking for such a column.
EDIT Seeing the association mapping you posted, it looks like it should be:
@ManyToOne(targetEntity = DistributionCompany.class)
@JoinColumn(name = "distributionCompanyID")
private DistributionCompany distributionCompany;
The @JoinTable
annotation 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
).
这篇关于休眠异常:缺少列(存在列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!