Foreign Key Value in Associated Entity Not Set

Running Hibernate 6.5.3. I have the following two entities:

Customer

    @Entity
    @Table(name = "Customers", schema = "dbo")
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonFilter("IncludesFilter")
    @Data
    public class Customer {

        @Id
        @CustomSequence(seriesType = "Customers")
        @Column(name = "CustomerID")
        private String customerID;

        //

        @OneToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.EAGER, mappedBy = "customer", orphanRemoval = true)
        private List<CustomerAddress> customerAddress;

    }

CustomerAddress

    @Entity
    @Table(name = "CustomerAddresses", schema = "dbo")
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonFilter("IncludesFilter")
    @Data
    public class CustomerAddress {

        @Id
        @CustomSequence(seriesType = "Customer Addresses")
        @Column(name = "CustomerAddressID")
        private String id;

        @ManyToOne
        private Customer customer;

        //

    }

The application is able to start but GET requests fail with the following stack trace:

    2024-10-18 05:57:39 WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 207, SQLState: S0001
    2024-10-18 05:57:39 ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Invalid column name 'customer_CustomerID'.
    2024-10-18 05:57:39 ERROR dev.interceptor.RequestInterceptor - An exception occurred with message: JDBC exception executing SQL [select car1_0.customer_CustomerID,car1_0.CustomerAddressID,car1_0.Address1,car1_0.Address2,car1_0.City,car1_0.PostalCode,car1_0.AreaID,psr1_0.AreaID,psr1_0.AreaCode,psr1_0.CountryID,cr1_0.CountryID,cr1_0.CountryCode,cr1_0.Description,psr1_0.Description from dbo.CustomerAddresses car1_0 left join dbo.Areas psr1_0 on psr1_0.AreaID=car1_0.AreaID left join dbo.Countries cr1_0 on cr1_0.CountryID=psr1_0.CountryID where car1_0.customer_CustomerID=?] [Invalid column name 'customer_CustomerID'.] [n/a]; SQL [n/a]
    2024-10-18 05:57:39 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQL [select car1_0.customer_CustomerID,car1_0.CustomerAddressID,car1_0.Address1,car1_0.Address2,car1_0.City,car1_0.PostalCode,car1_0.AreaID,psr1_0.AreaID,psr1_0.AreaCode,psr1_0.CountryID,cr1_0.CountryID,cr1_0.CountryCode,cr1_0.Description,psr1_0.Description from dbo.CustomerAddresses car1_0 left join dbo.Areas psr1_0 on psr1_0.AreaID=car1_0.AreaID left join dbo.Countries cr1_0 on cr1_0.CountryID=psr1_0.CountryID where car1_0.customer_CustomerID=?] [Invalid column name 'customer_CustomerID'.] [n/a]; SQL [n/a]] with root cause com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'customer_CustomerID'.

If I remove the ‘@ManyToOne private Customer customer;’ from the CustomerAddress entity, and remove ‘mappedBy = “customer”’ and add '@JoinColumn(name = “CustomerID”, referencedColumnName = “CustomerID”) to the Customer entity the requests work, but sending a PUT request with a new customer address fails to set the CustomerID column for the new CustomerAddress entity.

How can I correct the entity relationship and ensure that the foreign key value is set correctly on the child when a new child is added?

Thanks!

customer_CustomerID is the default column name that is assumed when you don’t specify an explicit column name. Apparently you want to use CustomerID, so do specify that then:

public class CustomerAddress {

    @Id
    @CustomSequence(seriesType = "Customer Addresses")
    @Column(name = "CustomerAddressID")
    private String id;

    @ManyToOne
    @JoinColumn(name = "CustomerID")
    private Customer customer;

    //

}

Hi,

I applied your suggestion and it worked. However, I have a further requirement to include the foreign key column in the JSON response. From research I understand the way to include a duplicated column is to add ‘insertable = false’ and ‘updatable = false’ to the @Column annotation.

Doing the above results in the foreign key being included in the GET response, but when adding a new child entity (though the db shows the foreign key has been assigned correctly), the PUT response does not show it.

How could I correct this?

Thanks!

I have no idea what you’re asking, but it seems to me that your problem is not with Hibernate ORM since you’re saying that the DB contains correct data.
If you have a real question about Hibernate ORM behavior, please open a new topic and explain clearly what you want to know with all the relevant details.

Apologies for not being clearer. When I submit a PUT JSON request with a new child entity it looks as follows:

        {
            "addressTypeRef": {
                "id": "AT0001"
            },
            "name": "Test Address Insert",
            "address1": "Address 1",
            "address2": "Address 2",
            "cityTown": "City",
            "areaRef": {
                "id": "A0001"
            },
            "postalZipCode": "12345",
            "isInactive": false
        }

The row is added to the database correctly, and the returned JSON response looks as follows:

        {
            "id": "<Generated ID from Database, Returning Correctly>",
            "addressTypeRef": {
                "id": "AT0001"
            },
            "name": "Test Address Insert",
            "address1": "Address 1",
            "address2": "Address 2",
            "cityTown": "City",
            "areaRef": {
                "id": "A0001"
            },
            "postalZipCode": "12345",
            "isInactive": false
        }

The database row itself shows the correct value has been assigned to the foreign key column (customerId), but customerId does not appear as a returned element in the JSON response. However, if I issue a subsequent GET, then the customerId appears in the response. What do I need to change in order to have the customerId element appear in the PUT response?

I don’t know how you are doing JSON serialization, but it sounds like you are not setting a field in your entity object but rely on the value for JSON serialization. When loading a fresh entity through Hibernate ORM, the value is set properly, so you see the value.
The issue is somewhere in your application.