JBrown521
JBrown521

Reputation: 172

Unable to find column with logical name in org.hibernate.mapping.Table and its related supertables and secondary tables

When I try to run my Application Class it gives me the following error:

2021-04-26 16:20:47.551 ERROR 14416 --- [ main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: Unable to find column with logical name: property_id in org.hibernate.mapping.Table(property) and its related supertables and secondary tables

From looking at that it feels to me like I might've named it incorrectly, but even when I went into the table and copied field names directly it didn't fix the issue.

The Property entity is as follows:

@Entity
@Table(name = "property")
public class Property {

  @Id
  @OneToOne
  @JoinColumn(name = "property_id", referencedColumnName = "property_id")
  private Address address;
  // other fields

With its table structure being:

Structure of property table

While the Address entity it's connected to is:

@Entity
@Table(name = "address")
public class Address {

  @Id
  @GeneratedValue
  @Column(name = "property_id")
  private Long propertyID;
  // other fields

With table structure:

Structure of address table

As you can see I have the names correct, and I believe my datasource is correctly formatted, if I use a column name that isn't in the table IntelliJ throws a warning saying it cannot resolve the column, which I don't get as they're written now.

This is my first big project using JPA so I might be missing something obvious but any help would be much appreciated, I've looked at other solutions and tried some of the suggestions but they all seem to be slightly different and not applicable to what I'm trying. Any help would be greatly appreciated :)

Upvotes: 1

Views: 4000

Answers (1)

Davide D'Alto
Davide D'Alto

Reputation: 8216

    @Entity
    @Table(name = "property")
    public class Property {

        @Id
        private Long id;

        @OneToOne
        @MapsId
        @JoinColumn(name="property_id")
        private Address address;

        ...
    }

    @Entity
    @Table(name = "address")
    public class Address {

        @Id
        @GeneratedValue
        @Column(name = "property_id")
        private Long propertyID;

        ...
    }

See the Hibernate ORM documentation for Derived Identifiers.

Upvotes: 2

Related Questions