Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Exception: Missing Column (column exists)

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;
like image 907
Samuel Smith Avatar asked Feb 14 '23 01:02

Samuel Smith


1 Answers

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).

like image 160
Xavi López Avatar answered Feb 16 '23 13:02

Xavi López