I use spring boot 2.0.2, spring-data-jpa, hibernate.
I try to save an entity with a complex structure, inheritance, idclass…
@Embeddable
public class EmbedddedCarKey implements Serializable {
private Integer id;
private int year;
public EmbedddedCarKey(Integer id, int year) {
this.id = id;
this.year=year;
}
}
@Entity
@DiscriminatorValue("TraditionalCars")
public class TraditionalCars extends Cars {
....
}
@Entity
@IdClass(EmbedddedCarKey.class)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Cars {
@Id
@GeneratedValue
private Integer id;
@Id
private int year;
@OneToMany(mappedBy = "car", cascade = CascadeType.PERSIST, orphanRemoval = true)
private List<Samples> samples = new ArrayList<>();
public void addSample(Samples sample) {
samples.add(sample);
sample.setCar(this);
}
public void removeSample(Samples sample) {
samples.remove(sample);
sample.setCar(null);
}
}
@Entity
public class Samples extends BaseEntity {
@Id
@SequenceGenerator(name = "samples_id_seq", sequenceName = "samples_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "samples_id_seq")
private Integer id;
@Embedded
private TestCars testCars;
}
@Embeddable
public class TestCars {
private boolean compression;
private boolean absorption;
private int compressionNumber;
private int absorptionNumber;
}
When i try to save a cars
Cars cars = new TraditionalCars();
....
Samples samples = new Samples();
...
TestSamplings testSamplings = new Samplings();
...
sample.setTestSamplings(testSamplings);
cars.addSamples(samples);
...
cars=carsRepository.save(cars);
I see in debug mode then
testCars has true, false and 0 value only, so no null…
Just after save I get this message
Hibernate:
select
nextval ('luxor.hibernate_sequence')
Hibernate:
select
nextval ('luxor.samples_id_seq')
Hibernate:
select
nextval ('luxor.samples_id_seq')
Hibernate:
insert
into
luxor.cars
(available_for_test, build_date, color_id, dimension_id, machine_id, print, product_id, reception_date, remark, special_try, test_done, to_print, delay_before_doing_test, press, quantity_received, dtype, id, year)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'TraditionalCars', ?, ?)
Hibernate:
insert
into
luxor.samples
(created_at, updated_at, absorption_printed, aen_remarque, certificat_include, cube, durability_printed, fresh_density, fresh_weigth, gen_remarque, label, position, sample_letter, sampling_id, sampling_year, absorption, absorption_number, coloration, coloration_number, compression, compression_number, density, draw_down, draw_down_number, durability, durability_number, granulometry, granulometry_number, organic_material, organic_material_number, scaling, scaling_number, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2018-05-22 22:23:58.816 WARN 18930 --- [nio-8080-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 23502
org.postgresql.util.PSQLException: ERROR: null value in column "absorption" violates not-null constraint
Detail : Failing row contains (12, 2018-05-22 22:23:58.76, null, f, 0, f, 0, f, null, null, 0, null, 0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null).