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!