I have an aggregate that has a one to one relationship, and in the second level, the child entity has a one to many relationship (a List). Currently when the entity gets saved it does what is expected, and is saving it OK. But when I try to get the entity from the database the SQL that gets generated is wrong, resulting in an error. I cant really tell what Im missing here, any help will be greatly appreciated. Im using Lombok, spring-data-jdbc, and spring-boot. Maybe Lombok has something to do but I cant tell. Heres Code to exemplify what Im talking about: The interface
public interface ChargeRepository extends CrudRepository<Charge, Long> {
}
Entities
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Charge {
@Id
private Long id;
private Double someData;
private Summary summary;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Summary {
@Id
private Long id;
private Double otherData;
private List<Multiplier> details = new ArrayList<>();
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Multiplier {
@Id
private Long id;
private Double detailData;
}
Heres the DDL
CREATE TABLE CHARGE(
ID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
SOME_DATA DOUBLE(2) NOT NULL,
PRIMARY KEY(ID)
);
CREATE TABLE SUMMARY(
ID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
OTHER_DATA DOUBLE(2) NOT NULL,
CHARGE BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(ID)
);
CREATE TABLE Multiplier(
ID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
DETAIL_DATA DOUBLE(2) NOT NULL,
SUMMARY BIGINT UNSIGNED NOT NULL,
SUMMARY_KEY INTEGER NOT NULL,
PRIMARY KEY(ID)
);
The problem is that when I save a new entity it does allright, but when I try to fetch it from the database the sql generated makes a join between the tables with a column "details" that exists in the entity but obviously its not part of the table. This is the error I get
PreparedStatementCallback; bad SQL grammar [SELECT charge.id AS id, charge.some_data AS some_data, summary.id AS summary_id, summary.other_data AS summary_other_data, summary.details AS summary_details FROM charge LEFT OUTER JOIN summary AS summary ON summary.charge = charge.id WHERE charge.id = ?]; nested exception is org.h2.jdbc.JdbcSQLException: Column \"SUMMARY.DETAILS\" not found \nColumn \"SUMMARY.DETAILS\" not found; SQL statement:\nSELECT charge.id AS id, charge.some_data AS some_data, summary.id AS summary_id, summary.other_data AS summary_other_data, summary.details AS summary_details FROM charge LEFT OUTER JOIN summary AS summary ON summary.charge = charge.id WHERE charge.id = ?
What I am missing here? EDIT: Forgot to add, if the list is in the root aggregate, it works fine.