Saving entity don't give correct value

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).

First, you need to implement equals and hashCode in EmbedddedCarKey.

same error the same error persist.

The error says that the absorption column was my. So it’s not related to the id.

Did you add not null constraints to embeddable columns?

i let db created them… every thing is nullable false, but that don’t explain problem

we can see there is no null value
cars

The code you pasted here does not indicate that.

Maybe the testCars refernce was null.

updated image, nothing is null

Since it’s not very clear what you are doing, it’s best to debug it and see why it does not work.

Maybe a bug, I replaced TestCars who was Embeddable by Entity annotation… and that resolved issue.

Feel free to create a replicating test case using our templates at

Feel free to open a JIRA and attach the test case and we can review it much easier.
If you find the culprit and wish to submit a PR with the fix, feel free as well.
Contributions are always welcomed :slight_smile: .