2

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.

Max Ene
  • 21
  • 2
  • I am wondering why you have not used @OneToMany and @JoinColumn annotation to define the relationship. `@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "summary") private Set multiplier = new HashSet();` – notionquest Mar 27 '19 at 18:23
  • 1
    Im kinda new to java in general, so I might be wrong, but spring-data-jdbc is not a JPA implementation, thats why I didn't used it, that annotation does not exist in this library – Max Ene Mar 27 '19 at 18:59
  • This seems to be a bug. Could you file an issue at https://jira.spring.io/projects/DATAJDBC, please? An executable test case attached to that or in a Github repo would be awesome. – Jens Schauder Mar 28 '19 at 09:54
  • Will do as soon as I get a little time Jens, thanks! – Max Ene Mar 28 '19 at 12:30

0 Answers0